Search code examples
javagradledependenciesdependency-managementivy

Versionless gradle dependencies


When I do this

dependencies {
    file('libs/something.jar')
}

I have something.jar in my distribution. But, when I to push the dependency to remote repository (ivy or maven) and want to use it from there, gradle always adds a version postfix to the jar.

dependencies {
    compile group: 'org.company', name: 'something', version: '1.0'
}

results in something-1.0.jar.

Even this

dependencies {
    compile group: 'org.company', name: 'something'
}

results in something-.jar (note the dash).

Can I somehow prevent the repository dependency to have a version?

(My motivation is that the jar is a 3rd party jar, I don't want to have it in git repo but I also don't want its name to change.)


Solution

  • As you've said, you're ok with giving the artifact a version in the repository. You just don't want a version in the jar in your application. You could do

    configurations {
        something { transitive = false }
    }
    dependencies {
        something 'org.company:something:1.0'
        something 'org.company:something-else:1.0'
        compile files(tasks['dummyTask'])
        compile 'org.foo:some-normal-dep:1.1'
    }
    task copySomething(type:Copy) {
        from configurations.something
        into "$buildDir/something"
        rename '(.+)-.+?\\.jar', '$1.jar'
    }
    task dummyTask {
        dependsOn copySomething
        inputs.dir "$buildDir/something"
        outputs.files fileTree("$buildDir/something")
    }
    

    The main enabler for this is that Project.files(...) can accept a Task