How to iterate gradle dependencies in custom gradle plugin?

I followed this guide:

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

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 am using / applying 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 can I iterate over the dependencies of houses in the apply method in MyGradlePlugin.groovy and print their coordinates (artifactId, groupId, version)?

+4
source share
2 answers

I know this is an old question, but since there is no answer, I will give an example that I used. This is based on the example in section 49.8.2.2 of the gradle docs.

I use it to do custom dependency resolution, but you can do whatever you want in the dependency iteration. Note that this works because it skips the closure that is performed 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"] } } 
+5
source

The problem is that the dependency graph is only available after a full evaluation of the project. Therefore, you cannot rely on this directly in the apply method. You must delay 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 . After comments and discussions in the comments and chats, you can also do the following:

 class MyGradlePlugin implements Plugin<Project> { void apply(Project project) { project.tasks.findByName('install')?.doLast { ... } } } 
+3
source

Source: https://habr.com/ru/post/1443735/


All Articles