Gradle Maven Bom Properties

2019-09-05 07:56发布

问题:

I'm looking into migrating from maven to gradle and in our current setup we have a master pom that defines all of our version dependencies.

the project name is master-pom and has snippets like this:

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>${commons-beanutils-version}</version>
        </dependency>

.... and later down in the file we define something along these lines:

<properties>
    <commons-beanutils-version>1.9.1</commons-beanutils-version>
</properties>

Now, this is what I have so far:

 plugins {
    id "com.github.johnrengelman.shadow" version "1.2.4"
    id "nebula.dependency-recommender" version "3.7.0"
}


apply plugin: 'java'
apply plugin: 'war'

apply plugin: 'nebula.dependency-recommender'
apply plugin: "com.github.johnrengelman.shadow"




version = '1.0.0-SNAPSHOT'
sourceCompatibility =  '1.8'

description = """best-service-ever"""



repositories {
       mavenLocal()
       maven { url 'https://repo.server.com/nexus/content/groups/public'
          credentials {
               username 'username'
               password nexusPassword
          }
    }
       mavenCentral()
       jcenter()


}

dependencyRecommendations {
   mavenBom module: 'biz.company.name:master-pom:1.0.0-SNAPSHOT'
}



dependencies {
          ... some dependencies
          compile 'biz.company.name:db-schema'
}

At this point it'll pick up the version named ${db-version} and use the correctly defined one from the build-maven. The problem is, I need to override that version with a specific version.

I tried putting the value in gradle.properties, but there's an issue with that.

db-version is unsupported by gradle, since it interprets - as arithmetic operation. I instead tried defining the value as

db_version=0.0.1700

which seems to work, but how do I set it so it overrides the $db-version value. I would like to avoid having to explicitly set the version: in every artifact.

My ideal scenario would either allow me to simply override the properties that come from the bom file.

Has anyone run into this issue before? Or have a work around?

回答1:

This is probably a partial answer.

I did find this pattern that seems to work though it's a bit brittle and a bit too verbose. What I really would like to do is say.

${common-service-version} = MyVersionNumber  (which is defined in gradle.properties)

Example Fix:

dependencyRecommendations {
   mavenBom module: 'biz.company.name:master-pom:1.0.0-SNAPSHOT'
   /* Override properties from mavenBom */
   map recommendations: [ 'biz.company.name:shared-common-service': services_common_global_version,
    'biz.company.name:services-core': services_common_global_version,
    'biz.neustar.ms:test-services-common': services_common_global_version,
    'biz.company.name:mip-db-schema':  db_version,
    'biz.company.name:services-api': services_api_version
   ]
}

This does work though you have to re-define the dependencies in a way. If someone has a better pattern let me know.