Strip version number from certain dependencies in

2019-06-09 04:33发布

We have a custom plugin where we would like to build a special tar file used for deployment. We would like to strip the version number on certain dependencies.

The code for the plugin:

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

I have tried something where we use a regex to remove the version from the file - like this:

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

Is it not possible to get a type dependency/artifact object so i can skip the regex and make the code more clean. Something like (pseducode):

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

Any help is appreciated :-)

1条回答
戒情不戒烟
2楼-- · 2019-06-09 04:47

The runtime Configuration you are referencing has an dependencies property that is a DependencySet holding Dependency objects. That means you can iterate trough the dependencies to to get their name. You can use that to write specific rename rules. There's also some caveats to consider.

I generated a sample project to exemplify with:

gradle init --type java-library

Then edited build.gradle to:

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

This has the following result:

[:~/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

Note that the hamcrest dependency is shown by gradle dependencies but it's not printed. This is because the dependencies are not resolved at configuration time, so gradle does not yet know about the transitive dependencies. In other words you can implement something like the copy task above in your plugin but it will only work for the dependencies you specify directly in your build script. Of course you could specify the transitive dependencies explicitly too if you have to rename some of those and it will still work. The testCompile configuration correctly prints both slf4j and junit because the extendsFrom rules are followed.

查看更多
登录 后发表回答