u123
u123

Reputation: 16267

How to iterate gradle dependencies in custom gradle plugin?

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)?

Upvotes: 6

Views: 13633

Answers (2)

Michael Larsen
Michael Larsen

Reputation: 608

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"]
    }
}

Upvotes: 6

Andrey Adamovich
Andrey Adamovich

Reputation: 20663

The problem is that dependency graph is only available after project is fully evaluated. That's why you can't rely on that directly in the apply method. You have to postpone the execution using the afterEvaluate method. The following code will do the trick:

class MyGradlePlugin implements Plugin<Project> {

  void apply(Project project) {
    project.afterEvaluate {
      println "  Project:" + project.name
      project.configurations.each { conf ->
        println "    Configuration: ${conf.name}"
        conf.allDependencies.each { dep ->
          println "      ${dep.group}:${dep.name}:${dep.version}"
        }
      }
    }
  }

}

UPDATE: Following question updates and discussions in the comments and chat you can also do the following:

class MyGradlePlugin implements Plugin<Project> {

  void apply(Project project) {
    project.tasks.findByName('install')?.doLast {
      ...
    }
  }

}

Upvotes: 4

Related Questions