如何从属性文件中读取依赖版本

How to read depedency version from properties file

我有一个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}'

请帮帮我。

我觉得。 您应该将 Spring 版本分配给 springframework.version 语句。 例如

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

您可以使用 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

当您像您一样添加具有自定义执行的插件时,您必须意识到它只在您指定的阶段执行。您指定 pre-clean 阶段,它是 clean 生命周期的一部分,而不是 build[=39= 的一部分] 生命周期。

Maven 由生命周期组成,执行到给定的阶段。 mvn compile 命令实际上意味着 运行 build 生命周期的所有阶段,包括 compile 阶段。

两个标准生命周期是(不完整列表):

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

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

指定的依赖项可能被标准插件用于 编译 阶段,因此属性需要在那个时候可用。

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

为了在 compile 阶段可用的属性,您可能应该绑定到 validate 阶段。

虽然很冗长,但实际上有相当多的提示 运行在调试模式下使用 mvn -X,但一开始可能太难理解了。

这里有一些关于 maven 生命周期的更多信息:https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html