Access current library project version string from library conventions plugin

Hey,

I use a custom library conventions plugin to share settings and build logics between all sub projects / libraries of my umbrella project.

As a LAN internal repository I use Apache Archiva and I have different repositories for releases (“internal”) and snapshots (“snapshots”). If “version” ends with “-SNAPSHOT”, I want the project to be published to the snapshots repository, otherwise to the releases repository, and vice versa.

I used the following article to configure my repository:

https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:complete_example

It has the snippet:

publishing {
    ...
    repositories {
        maven {
            // change URLs to point to your repos, e.g. http://my.org/repo
            def releasesRepoUrl = layout.buildDirectory.dir('repos/releases')
            def snapshotsRepoUrl = layout.buildDirectory.dir('repos/snapshots')
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
    }
    ...
 }

Setting the correct repository https-URLs is no problem. My problem is the “version”-check. It is evaluated to “unspecified”.

Can I access the version string of the project that applies my library conventions?

Or how would you solve the releases/snapshots repo URL selection based on looking for a “-SNAPSHOT” suffix in “version”?

Thank you for any advice!

Yours,
Scully

This is most probably because you set the version after that plugin is applied.
So when you read the version at plugin application time, it is not set yet.
I usually set group and version not programmatically, but in gradle.properties, then it would already be there when the plugin is applied.

Besides that, you can set the URL to a Provider too, that should then be lazily evaluated only when necessary and solve your problem

Btw. when you use project.version you need to call toString() on it, not assume it is a String.
It is intentionally Object, not String.
Unless of course you know for sure it will be a String like in the example you quoted where it is set before being read.