0

我们的主要项目有一个 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}

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

我将如何解决这个问题?

4

3 回答 3

0

理想情况下,您应该使用 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

于 2013-08-06T10:30:37.903 回答
0

我最终做的是我最初认为不起作用的事情。虽然它抛出错误并使 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>

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

于 2013-08-08T14:00:40.047 回答
0

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

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

于 2013-08-06T10:19:47.253 回答