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

即使我已经定义了插件以从外部文件读取属性.当我进行 mvn 全新安装时,属性 ${common-version} 在运行时不会被替换

错误 - dependencies.dependency.version' for com.commone 一个有效的版本,但是是"${通用版本}"。@ 第 107 行,第 13 列

试图在网上搜索很多,但没有一个解决方案对我有用。请帮忙!

据我所知,不可能做你试图做的事情(即外部化工件的版本(。这仅仅是因为 Maven 需要在任何插件运行之前/从文件中读取内容之前设置整个依赖项(这是一种先有鸡还是先有蛋的情况(。

不确定为什么要外部化 jar 版本。

如果您希望所有jar文件的版本都在同一位置,并且应该在所有项目中保持一致,那么您可以使用我的maven提供的依赖项管理标签。

添加父 pom 中的所有常见依赖项,并且仅在子项目中指定依赖项而不指定版本

据我所知,不可能做你试图做的事情(即外部化工件的版本(。这仅仅是因为 Maven 需要在任何插件运行之前/从文件中读取内容之前设置整个依赖项(这是一种先有鸡还是先有蛋的情况(。

这个

线程似乎支持这个说法。

您可以使用 BOM POM 并将其用作主 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>

最新更新