Narryel
Narryel

Reputation: 111

gradle suproject version is unspecified while publishing

i have two nexus repositories: one for releases, other for snapshots. i have code in publish task to define which repo to pick according to doc:

        repositories {
            repositories {
                maven {
                    credentials {
                        username "$nexusUser"
                        password "$nexusPassword"
                    }
                    def releasesRepoUrl = "https://xxx/repository/factoring-maven-release/"
                    def snapshotsRepoUrl = "https://xxx/repository/factoring-maven-snapshots/"
                    url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
                }
            }
            publications {
                create("default", MavenPublication.class) {
                    from(components["java"])
                }
            }
        }
    }

and subprojects included by this code :

rootProject.name = 'xxx-integration-lib'

include 'xxx-service1-dto'
include 'xxx-service2-dto'

subprojects build.gradle:

group = "xxx"
version = "0.0.6-SNAPSHOT"

but this doesnt work since subproject version is always unspecified. tried:

  1. making new allproject task to return version
  2. using project.property('propName') - but this seems like a workaround, not a solution

any thoughts?

Upvotes: 6

Views: 3236

Answers (4)

KnockKnock
KnockKnock

Reputation: 153

In subproject build.gradle add

version = '0.0.1'
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            version = project.version
        }
    }
}

Then in root build.gradle

allprojects {
    publishing {
        afterEvaluate {
            repositories {
                maven {
                    def releasesRepoUrl = 'https://.../releases/'
                    def snapshotsRepoUrl = 'https://.../snapshots/'
                    url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
    
                    credentials {
                        ...
                    }
                }
            }
        }
    }
}

only this works for me

Upvotes: 0

denizg
denizg

Reputation: 952

Instead of using this

publishing {
    publications {
        nexus(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            def releasesRepoUrl = 'https://.../releases/'
            def snapshotsRepoUrl = 'https://.../snapshots/'
            url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

            credentials {
                ...
            }
        }
    }
}

use afterEvaluate block outside of publications and repositories block.

publishing {
    afterEvaluate {
        publications {
            nexus(MavenPublication) {
                from components.java
            }
        }
        repositories {
            maven {
                def releasesRepoUrl = 'https://.../releases/'
                def snapshotsRepoUrl = 'https://.../snapshots/'
                url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

                credentials {
                    ...
                }
            }
        }
    }
}

Upvotes: -1

Narryel
Narryel

Reputation: 111

the only worked way for me was a workaround: placing subproject version management in root build.gradle like this

project(':subproject1') {
    version '0.0.6-SNAPSHOT'
}

project(':subproject2') {
    version '0.0.12-SNAPSHOT'
}

project(':subproject14) {
    version '0.0.5-SNAPSHOT'
}

and then project.version property is being injecting correctyl

Upvotes: 3

Dariusz Urbanek
Dariusz Urbanek

Reputation: 326

I am working on multimodule projects with gradle and we are not setting a version in submodules.All we do is setting it in base project in gradle.properties file.

group = "xxx"
version = "0.0.6-SNAPSHOT"

then you can use it:

url = project.version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

Upvotes: 0

Related Questions