2017-06-12 162 views
2

我有一个pom.xml属性和我定义它正在从一个外部文件中提到它的属性 -pom.xml的是无法读取外部属性文件

请看下面我的pom.xml : -

<?xml version="1.0" encoding="UTF-8"?> 
<project 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.src</groupId> 
    <artifactId>common</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>common</name> 
    <url>http://maven.apache.org</url> 
    <scm> 
     <connection>scm:svn:http://127.0.0.1/dummy</connection> 
     <developerConnection>scm:svn:https://127.0.0.1/dummy</developerConnection> 
     <tag>HEAD</tag> 
     <url>http://127.0.0.1/dummy</url> 
    </scm> 

    <properties> 
     <salcommon.version>${common-version}</salcommon.version> 
    </properties> 
    <repositories> 
     <repository> 
      <id>secureaccesslnk-maven</id> 
      <url>https://nexus-maven/</url> 
      <releases> 
       <enabled>true</enabled> 
      </releases> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </repository> 
    </repositories> 
     <distributionManagement> 
     <repository> 
      <id>secureaccesslnk-maven-release</id> 
      <url>https://nexus-maven-release/</url> 
     </repository> 
     <snapshotRepository> 
      <id>secureaccesslnk-maven-snapshot</id> 
      <url>https://nexus-maven-release/</url> 
     </snapshotRepository> 
    </distributionManagement> 


    <dependencies> 
     <dependency> 
      <groupId>com.common</groupId> 
      <artifactId>sal-logging</artifactId> 
      <version>${salcommon.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.common</groupId> 
      <artifactId>sal-auditor</artifactId> 
      <version>${salcommon.version}</version> 
     </dependency> 
    </dependencies> 

    <build> 
     <resources> 
      <resource> 
       <directory>src/main/java</directory> 
       <includes> 
        <include>**/*.properties</include> 
        <include>**/*.tmpl</include> 
       </includes> 
      </resource> 
      <resource> 
       <directory>src/main/resources</directory> 
       <includes> 
        <include>**/*.properties</include> 
        <include>**/*.tmpl</include> 
       </includes> 
      </resource> 
     </resources> 
     <extensions> 
      <extension> 
       <groupId>org.apache.maven.wagon</groupId> 
       <artifactId>wagon-webdav-jackrabbit</artifactId> 
       <version>1.0-beta-7</version> 
      </extension> 
     </extensions> 

     <plugins> 
      <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>properties-maven-plugin</artifactId> 
     <version>1.0.0</version> 
     <executions> 
      <execution> 
      <phase>initialize</phase> 
      <goals> 
       <goal>read-project-properties</goal> 
      </goals> 
      <configuration> 
       <files> 
       <file>common.version.properties</file> 
       </files> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.5.1</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.jacoco</groupId> 
       <artifactId>jacoco-maven-plugin</artifactId> 
       <version>0.7.4.201502262128</version> 
       <configuration> 
        <destFile>${sonar.jacoco.reportPath}</destFile> 
        <append>true</append> 
       </configuration> 
       <executions> 
        <execution> 
         <id>agent</id> 
         <goals> 
          <goal>prepare-agent</goal> 
         </goals> 
         <phase>test</phase> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-source-plugin</artifactId> 
       <version>2.4</version> 
       <executions> 
        <execution> 
         <id>attach-sources</id> 
         <goals> 
          <goal>jar</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 

     </plugins> 
    </build> 
</project> 

的common.version.properties详情如下: -

common-version=3.0.1.0-SNAPSHOT 

即使我定义的插件来读取外部文件.The属性$ {普通版}属性一世当我在执行mvn clean install时,没有被替换为

错误 - 依赖关系。 @第107行第13列

我试图在网上搜索很多,但没有一个解决方案为我工作。请帮助!

+0

不把版本放入pom文件的原因是什么? – khmarbaise

回答

1

据我所知,这是不可能做你试图做什么(即将工件的版本外化)。这很简单,因为Maven需要在插件运行之前/在从文件中读取内容之前设置整个依赖关系(这是鸡与鸡蛋的情况)。

0

据我所知,不可能做你正在尝试做的事情(即外化一个神器的版本)。 这只是因为Maven需要在任何插件运行之前/在从文件中读取内容之前设置整个依赖关系(这是鸡与鸡蛋的情况)。

thread似乎支持这种说法。

1

不知道你为什么要外化jar版本。

如果您希望所有jar文件的版本位于同一位置,并且应该是所有项目的一致性,那么您可以使用提供我的maven的dependencyManagement标记。

添加你的公共依赖于父POM和儿童项目仅指定的依赖,而不版

0

您可以使用一个BOM POM,并把它作为你的主POM父。这样你就拥有了外部文件中的属性。

BOM POM:

<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.test</groupId> 
    <artifactId>bom</artifactId> 
    <version>1.0.0</version> 
    <packaging>pom</packaging> 
    <properties> 
    <common-version>3.0.1.0-SNAPSHOT</common-version> 
    </properties> 
</project> 

在你的主POM,你添加父声明:

<parent> 
    <groupId>com.test</groupId> 
    <version>1.0.0</version> 
    <artifactId>bom</artifactId> 
    </parent>