【问题标题】:Maven POM dependency with added token based on profile基于配置文件添加令牌的 Maven POM 依赖项
【发布时间】:2013-08-07 05:31:37
【问题描述】:

我们的主要项目有一个 POM。我会说其中定义了 10 到 15 个配置文件。依赖项是通用的,大概有 20 个左右。

我们(至少)有一个依赖项,其版本取决于配置文件是针对测试还是生产。生产部署需要:

<version>1.0.3.RELEASE</version>

作为依赖版本,而 dev 和 staging 部署采用

<version>1.0.3.STAGING</version>

我想进行一些设置,这样我们就不必再手动切换了。一种明显的解决方案是在配置文件中定义依赖关系。问题在于我们拥有的配置文件数量。每次版本号增加时,我们都必须小心不要错过更新某个地方的版本。

我阅读了有关标记化的内容,并尝试像这样声明通用依赖项:

    <dependency>
        <groupId>org.groupId</groupId>
        <artifactId>lib-artifactId</artifactId>
        <version>1.0.3.${lib-artifactId.version}</version>
    </dependency>

然后添加

        <properties>
            <lib-artifactId.version>RELEASE</lib-artifactId.version>
        </properties>

到每个配置文件,在适当的地方将 RELEASE 更改为 STAGING。

这行不通。该错误的大意是它找不到具有版本的库

1.0.3.${lib-artifactId.version}

换句话说,它没有替换令牌。

我将如何解决这个问题?

【问题讨论】:

  • 我已经在一个项目中使用了这个解决方案(配置文件+具有不同值的属性)并且效果很好。您确定配置文件已正确激活吗?
  • 我很确定所有配置文件都按预期和预期工作。我考虑的一件事是,由于我的所有配置文件都在 pom.xml 中,maven 可能无法解析令牌,因为它被定义了很多次(每个配置文件一次)。

标签: maven pom.xml


【解决方案1】:

代替在配置文件中定义令牌,代替主文件中的依赖项,您可以尝试:

像以前一样保持每个配置文件中的依赖关系。酌情用标记 ${lib-artifactId.release_version} 或 ${lib-artifactId.staging_version} 替换版本,并在顶级 pom 文件中定义这两个标记。

【讨论】:

  • 这还不错,但它需要向 pom 添加比我真正想要的更多的信息。
【解决方案2】:

理想情况下,您应该使用来自 maven 的 CLASSIFIERS

<dependency>
 <groupId>org.groupId</groupId>
 <artifactId>lib-artifactId</artifactId>
 <version>1.0.3</version>
 <classifier>${lib-artifactId.version}</classifier>
</dependency>

它将解析为 1.0.3-RELEASE

【讨论】:

  • 我喜欢这个,但它对我来说不可行。它解析为 - (连字符)RELEASE 的事实是一个破坏者:它需要更改所引用库的名称(我认为它的名称只有点,受 Spring 版本的启发)。
【解决方案3】:

我最终做的是我最初认为行不通的事情。虽然它抛出错误并使 Eclipse 爬行,但它确实编译并运行了。然后我也能够解决 Eclipse 错误,所以现在我有了我想要的情况。

以这种方式定义泛型依赖的问题:

<dependency>
    <groupId>org.groupId</groupId>
    <artifactId>lib-artifactId</artifactId>
    <version>1.0.3.${lib-artifactId.version}</version>
</dependency>

当您只是编码而不是“内部”某个配置文件时,Eclipse(或至少 m2e)无法找到实际的依赖关系。所以它会抛出令人讨厌的错误。很多红色。具体来说:

ArtifactDescriptorException: Failed to read artifact descriptor org.groupId:lib-artifactId:jar:1.0.3.${lib-artifactId.version}: ArtifactResolutionException: Failure to transfer org.groupId:lib-artifactId:jar:1.0.3.${lib-artifactId.version} from http://xxx.xxx.xxx was cached in the local repository, resolution will not be reattempted until the update interval of xxx has elapsed or updates are forced. Original error: Could not transfer artifact org.groupId:lib-artifactId:jar:1.0.3.${lib-artifactId.version} from/to xxx (http://xxx.xxx.xxx): IllegalArgumentException

考虑到问题后,解决方案并不难。我只需要在通用部分的属性中指定一个“默认”版本。所以我加了

<lib-artifactId.version>RELEASE</lib-artifactId.version>

到顶部的部分,一切都很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 2017-07-07
    相关资源
    最近更新 更多