Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate gradle dependencies in custom gradle plugin?

Tags:

plugins

gradle

I have followed this guide:

http://www.gradle.org/docs/current/userguide/custom_plugins.html

to create a standalone gradle plugin with the following structure/files:

  my-gradle-plugin
    > src
     > main
      > java
       > com
        > mygroup
         > MyGradlePlugin.groovy
    > build.gradle
    > settings.gradle

build.gradle :

apply plugin: 'groovy'

dependencies {
  compile gradleApi()
  groovy localGroovy()
}

apply plugin: 'maven'
repositories {
  mavenCentral()
}

group = 'com.mygroup'
version = '1.0.0-SNAPSHOT'

MyGradlePlugin.groovy :

package com.mygroup

import org.gradle.api.*

class MyGradlePlugin implements Plugin<Project> {

  void apply(Project project) {
    print " project.name " + project.name + "\n"
    print " project.dependencies " + project.dependencies + "\n"
     // How do we iterate each dependency and print artifactId, group, version??
     // project.dependencies.each {
     //        compile(it) {
     //        print it.next()
     //        print it.name
     //      }
     //    }
   project.configurations.each {
  print it.dump()

}
   }
  }

In another project I use/apply this plugin:

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'

repositories {
  mavenLocal()
}

buildscript {
  repositories {
    mavenLocal()
  }
  dependencies {
    classpath group: 'com.mygroup', name: 'my-gradle-plugin', version: '1.0.0-SNAPSHOT'
  }
}



dependencies {
  compile group: 'commons-codec', name: 'commons-codec', version: '1.4'
  compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.7.0'
}

install.doLast {
 apply plugin: 'my-gradle-plugin'

}

But how do I iterate the commons dependencies from the apply method in MyGradlePlugin.groovy and print their coordinates (artifactId, groupId, version)?

like image 601
u123 Avatar asked Nov 02 '12 20:11

u123


People also ask

How do you add Gradle dependency in build Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.


1 Answers

I know this is an old question but since there is not a selected answer I'll throw in an example that I have used. This is based on the example in section 49.8.2.2 of the gradle docs.

I'm using it to do custom dependency resolution, but you can do whatever you'd like inside the dependency iteration. Note that this works because its passing a closure that is executed after the configuration phase.

Plugin code:

package com.overtherainbow

import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.artifacts.DependencyResolveDetails

class DefaultVersionPlugin implements Plugin<Project> {

    // This is where dependency versions are defined
    def defaultVersionsMap = [
        'javax.servlet:servlet-api' : '2.5',
        'log4j:log4j' : '1.2.16']

    void apply(Project project) {
        project.configurations.all {
            resolutionStrategy.eachDependency {
                DependencyResolveDetails details -> resolveDependencyVersion(project, details)
            }
        }
    }

    def resolveDependencyVersion(Project project, DependencyResolveDetails details) {
        if (details.requested.version == 'default') {
            def version = resolveDefaultVersion(project, details.requested.group, details.requested.name)
            details.useVersion version
        }
    }

    def resolveDefaultVersion(Project project, String group, String name) {
        project.logger.debug("Resolving default dependency for $group:$name")
        println "Resolving default dependency for $group:$name"
        defaultVersionsMap["$group:$name"]
    }
}
like image 176
Michael Larsen Avatar answered Sep 23 '22 22:09

Michael Larsen