基于配置文件添加令牌的Maven POM依赖关系

时间:2013-08-06 09:22:59

标签: maven pom.xml

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

换句话说,它不是替代令牌。

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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

答案 2 :(得分: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>

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