2

I have a multi module gradle project which contains core and client. client depends on core which is declared like this:

dependencies {
    compile project(':core')
}

If I publish core and client to Ivy or Maven the dependency from client to core uses the exact version that is currently defined for the core (e.g. 1.0.0).

Is there a way to change that? Let's say the core is guaranteed to be compatible between minor releases. So instead of 1.0.0 i'd like the dependency to be to version 1.+.

Werzi2001
  • 2,035
  • 1
  • 18
  • 41
  • 1
    https://docs.gradle.org/current/userguide/publishing_maven.html#sec:modifying_the_generated_pom – JB Nizet Nov 18 '17 at 16:12
  • Then don't hesitate to put your solution in an answer. – JB Nizet Nov 18 '17 at 23:17
  • I wonder why do you want to do that, as they are tightly coupled between the multiple modules. – chenrui Nov 21 '17 at 13:59
  • @chenrui Like I said the modules are guaranteed to be compatible between major versions. So you don't have to update the client to use a newer version of the core (within one major). With the fixed version numbers this would not be possible. Furthermore the `core` and `client` example is just an example. The real project has many more modules. – Werzi2001 Dec 04 '17 at 11:32

1 Answers1

1

In order to replace the version in the generated pom.xml I created a helper function:

// helper function to replace dependency version in maven pom.xml
def replaceDependencyVersion(root, groupId, artifactId, version) {
    // replace version
    root.dependencies.dependency.findAll() { node ->
        node.groupId.text() == groupId && node.artifactId.text() == artifactId
    }.each() { node ->
        node.version*.value = version
    }
}

This function can then be used in publishing to replace the version number:

// publishing
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

            pom.withXml {
                replaceDependencyVersion(asNode(), 'com.test', 'core', '1.+')
            }
        }
    }
}
Werzi2001
  • 2,035
  • 1
  • 18
  • 41