【问题标题】:Strip version number from certain dependencies in Gradle custom plugin从 Gradle 自定义插件中的某些依赖项中去除版本号
【发布时间】:2016-10-13 14:39:37
【问题描述】:

我们有一个自定义插件,我们想在其中构建一个用于部署的特殊 tar 文件。我们想去除某些依赖项的版本号。

插件代码:

void addTarTask(Project project) {
    Tar tar = project.tasks.create("tar", Tar)
    tar.classifier = "bin"
    project.afterEvaluate {
        tar.into("${project.name}-${project.version}"){
            into('lib'){
                from project.tasks.jar
                from project.configurations.runtime 
            }

            //snip more custom requirements
        }
    }
}

我尝试过使用正则表达式从文件中删除版本的方法 - 像这样:

into('lib'){
    from project.tasks.jar
    from project.configurations.runtime {
        rename '(.*)-[0-9]+\\..*.jar', '$1.jar' // not sure regex is okay - it is just to demo
    }
}

是否无法获取类型依赖项/工件对象,以便我可以跳过正则表达式并使代码更干净。类似(伪代码):

if(${artifact.extension} == "so") {
    return ${artifact.name}.${artifact.extension}
} else {
    // just return normal file
}

任何帮助表示赞赏:-)

【问题讨论】:

    标签: gradle groovy build.gradle


    【解决方案1】:

    您引用的runtime Configuration 有一个dependencies 属性,它是一个DependencySet 持有Dependency 对象。 这意味着您可以遍历依赖项以获取它们的名称。 您可以使用它来编写特定的重命名规则。还有一些注意事项需要考虑。

    我生成了一个示例项目来举例说明:

    gradle init --type java-library
    

    然后将 build.gradle 编辑为:

    apply plugin: 'java'
    
    repositories {
        jcenter()
    }
    
    dependencies {
        compile 'org.slf4j:slf4j-api:1.7.21'
        testCompile 'junit:junit:4.12'
    }
    
    task log << {
        configurations.runtime.allDependencies.each { println it }
        println ""
        configurations.testCompile.allDependencies.each { println it }
    }
    
    task copy(type: Copy) {
        from configurations.runtime
        into  "$buildDir/lib"
        configurations.runtime.allDependencies.each {
            rename "-${it.version}", ""
        } 
    }
    

    这有以下结果:

    [:~/tmp] % ./gradlew log copy
    :log
    DefaultExternalModuleDependency{group='org.slf4j', name='slf4j-api', version='1.7.21', configuration='default'}
    
    DefaultExternalModuleDependency{group='junit', name='junit', version='4.12', configuration='default'}
    DefaultExternalModuleDependency{group='org.slf4j', name='slf4j-api', version='1.7.21', configuration='default'}
    [...]
    
    [:~/tmp] % ./gradlew dependencies    
    [...]
    testCompile - Dependencies for source set 'test'.
    +--- org.slf4j:slf4j-api:1.7.21
    \--- junit:junit:4.12
         \--- org.hamcrest:hamcrest-core:1.3
    
    [...]
    
    [:~/tmp] % ls build/lib
    slf4j-api.jar
    

    请注意,hamcrest 依赖项由 gradle dependencies 显示,但未打印。这是因为在配置时没有解决依赖关系,所以 gradle 还不知道传递依赖关系。换句话说,你可以在你的插件中实现类似于上面的复制任务,但它只适用于你在构建脚本中直接指定的依赖项。当然,如果您必须重命名其中的一些,您也可以显式指定传递依赖项,它仍然可以工作。 testCompile 配置正确打印了 slf4j 和 junit,因为遵循了 extendsFrom 规则。

    【讨论】:

    • 我最终以不同的方式做这件事。但是非常感谢您的回答。从中学到了很多。
    【解决方案2】:

    这段代码 sn-p 还将复制传递依赖,并且已经准备好 gradle 7:

    task copyDependencies(type: Copy) {
      from configurations.runtimeClasspath 
      into "$buildDir/lib"
      configurations.runtimeClasspath.resolvedConfiguration.resolvedArtifacts.each {
          rename "${it.artifact.name}-${it.artifactId.componentIdentifier.version}", "${it.artifact.name}"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多