2013-08-06 38 views
0

我们为我们的主项目提供了POM。我会说10到15个配置文件在里面定义。依赖是通用的,大概在20左右。基于配置文件添加令牌的Maven POM依赖关系

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

<version>1.0.3.RELEASE</version> 

的依赖版本而开发和分期部署采取

<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> 

到每个配置文件,以适当的释放改为分期哪里。

这是行不通的。错误是无法找到版本的库

1.0.3.${lib-artifactId.version} 

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

我该如何解决这个问题?

+0

我已经在一个项目中使用了这个解决方案(配置文件+属性具有不同的值),它的工作完美。你确定配置文件已正确激活吗? –

+0

我非常确定,因为所有配置文件按预期和预期工作。我考虑的一件事是,因为所有我的配置文件都在pom.xml中,所以maven可能无法解析该令牌,因为它已被多次定义(每个配置文件一次)。 – Zaan

回答

0

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

保持依赖性每个配置文件中,你有过。根据需要用标记$ {lib-artifactId.release_version}或$ {lib-artifactId.staging_version}替换版本,并在顶级pom文件中定义这两个标记。

+0

这并不坏,但它需要增加更多的信息给我比我真正想要的。 – Zaan

0

从行家

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

理想情况下,你应该使用分类器将解析为1.0.3-RELEASE

+0

我喜欢这个,但对我来说这不可行。事实上,它解决了 - (连字符)RELEASE是一个破坏者:它需要改变被引用的lib的名称(它的名字是只有点,由Spring发布启发,我估计)。 – Zaan

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> 

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