简体   繁体   English

Gradle Maven Bom 属性

[英]Gradle Maven Bom Properties

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.我正在考虑从 maven 迁移到 gradle,在我们当前的设置中,我们有一个 master pom 来定义我们所有的版本依赖项。

the project name is master-pom and has snippets like this:项目名称是 master-pom 并具有如下片段:

        <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.此时,它将选择名为 ${db-version} 的版本并使用 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.我尝试将值放在 gradle.properties 中,但存在问题。

db-version is unsupported by gradle, since it interprets - as arithmetic operation. db-version 不受 gradle 支持,因为它将 - 解释为算术运算。 I instead tried defining the value as我改为尝试将值定义为

db_version=0.0.1700 db_version=0.0.1700

which seems to work, but how do I set it so it overrides the $db-version value.这似乎有效,但我如何设置它以覆盖 $db-version 值。 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.我的理想方案要么允许我简单地覆盖来自 bom 文件的属性。

Has anyone run into this issue before?有没有人遇到过这个问题? Or have a work around?或者有解决办法?

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.如果有人有更好的模式,请告诉我。

Found the idea here: https://dzone.com/articles/specifying-gradle-build , adapted a little in order to make it work on the my current version of gradle (not sure if that was the problem, but anyway…)在这里找到了这个想法: https ://dzone.com/articles/specifying-gradle-build ,进行了一些调整以使其在我当前版本的 gradle 上工作(不确定这是否是问题,但无论如何......)

Specifying common version in gradle comes down to basically two simple steps:在 gradle 中指定 common 版本基本上可以归结为两个简单的步骤:

  1. Create a file gradle.properties if it doesn't exists yet.如果尚不存在,则创建一个文件gradle.properties Here arbitrary configuration may be stored, eg jackson.version = 2.10.0 (please, do not copy it, always check for actual fresh versions on maven central or alike).这里可以存储任意配置,例如jackson.version = 2.10.0 (请不要复制它,始终检查 maven central 或类似版本上的实际新版本)。

  2. Use defined properties via project.properties map inside double-quoted template string which specifies dependency.在指定依赖项的双引号模板字符串中通过project.properties映射使用定义的属性。 For example:例如:

dependencies {
    implementation "com.fasterxml.jackson.core:jackson-core:${project.properties['jackson.version']}"
    implementation "com.fasterxml.jackson.core:jackson-databind:${project.properties['jackson.version']}"
    //...
}

Of course, alternative syntax can be used insted:当然,可以使用替代语法:

dependencies {
    implementation(group: "com.fasterxml.jackson.core", name: "jackson-core", version: project.properties['jackson.version'])
    implementation group: "com.fasterxml.jackson.core", name: "jackson-databind", version: project.properties['jackson.version']
    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM