6

我已遵循本指南:

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

创建具有以下结构/文件的独立 gradle 插件:

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

构建.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()

}
   }
  }

在另一个项目中,我使用/应用这个插件:

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'

}

但是如何从MyGradlePlugin.groovy中的 apply 方法迭代公共依赖项并打印它们的坐标(artifactId、groupId、version)?

4

2 回答 2

6

我知道这是一个老问题,但由于没有选定的答案,我将举一个我用过的例子。这是基于 gradle docs 第 49.8.2.2 节中的示例。

我用它来做自定义依赖解析,但你可以在依赖迭代中做任何你想做的事情。请注意,这是有效的,因为它传递了一个在配置阶段之后执行的闭包。

插件代码:

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"]
    }
}
于 2013-06-06T20:03:12.833 回答
4

问题是依赖关系图只有在项目完全评估后才可用。这就是为什么你不能在apply方法中直接依赖它。您必须使用该afterEvaluate方法推迟执行。以下代码可以解决问题:

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

}

更新:在评论和聊天中的问题更新和讨论之后,您还可以执行以下操作:

class MyGradlePlugin implements Plugin<Project> {

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

}
于 2012-11-05T08:00:35.747 回答