2017-01-10 53 views
1

我有一个maven项目。我想将maven依赖版本外部化为外部属性文件。 我试着用属性文件插件我不读属性文件。如何从属性文件中读取依赖版本

config.properties

springframework.version=4.2.5.RELEASE 

pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>com.estuate.test</groupId> 
<artifactId>testPom</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>properties-maven-plugin</artifactId> 
      <version>1.0-alpha-1</version> 
      <executions> 
       <execution> 
        <phase>pre-clean</phase> 
        <goals> 
         <goal>read-project-properties</goal> 
        </goals> 
        <configuration> 
         <files> 
          <file>config.properties</file> 
         </files> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 
<dependencies> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 
</dependencies> 

还是我流汗错误消息

build.plugins.plugin[org.codehaus.mojo:properties-maven-plugin].dependencies.dependency.version' for org.springframework:spring-core:jar must be a valid version but is '${springframework.version}' 

请帮助我。

+0

为什么不使用最新的[属性 - Maven的插件]的版本(http://www.mojohaus.org/properties-maven-plugin/)哪个1.0.0? – khmarbaise

+0

我不明白解决方案。我只有一个pom.xml文件,并且我没有任何父maven项目或模块模块 –

回答

0

当你添加一个插件使用自定义执行,像你这样做,那么你必须认识到它只在你指定的阶段执行。您指定了预清洗阶段,这是清洁生命周期的一部分,而不是生命周期的一部分生命周期。

Maven由生命周期组成,执行到一个给定的阶段。 mvn compile的命令实际上意味着运行构建生命周期所有阶段直至并包括编译阶段。

标准的生命周期中有两个是(不完全名单):

clean :: pre-clean -> clean -> post-clean 

build :: validate -> compile -> test -> package -> verify -> install -> deploy 

指定很可能使用的标准插件的编译阶段的依赖关系,这样的属性需要可供当时。

当您声明预清理阶段时,当您执行mvn clean时,属性可用。

对于要在编译阶段可用的属性,您应该可以绑定到验证阶段。

尽管非常详细,但实际上有很多提示在调试模式下运行,其中有mvn -X,但起初可能太多了解。

关于Maven的一些详细信息生命周期的位置:https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

+0

我尝试了阶段:验证和编译。但它没有解决 –

-1

我想。 您应该为springframework.version语句指定一个Spring版本。 例如

 <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>4.3.1-REALESE</version> 
    </dependency> 

那么其他依赖

<dependency> 
    <groupId>...framework...</groupId> 
    <artifactId>spring-...</artifactId> 
    <version>${springframework.version}</version> 
</dependency> 
0

你可以使用Maven插件的属性这是建议的方式Maven项目来读取属性文件之一。下面是插件细节

<plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>properties-maven-plugin</artifactId> 
      <version>1.0-alpha-1</version> 
      <executions> 
       <execution> 
       <phase>initialize</phase> 
       <goals> 
        <goal>read-project-properties</goal> 
       </goals> 
       <configuration> 
        <files> 
        <file>Versions.properties</file> 
        </files> 
       </configuration> 
       </execution> 
      </executions> 
      </plugin> 
<dependencies> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 
</dependencies> 

Version.properties文件看起来像

springframework.version = 4.2.5.RELEASE

+0

那么,这实际上是他正在尝试..相同的插件相同的目标。你错过了他的问题,这是他所处的阶段。 –

+0

是的尼尔森是正确的,我已经改变了执行阶段的差异。 –

+0

其不工作 –