Java 如何从属性文件中读取依赖性版本
我有一个maven项目。我想将maven依赖版本外部化为外部属性文件。 我尝试了属性文件插件,但我没有读取属性文件 config.propertiesJava 如何从属性文件中读取依赖性版本,java,maven,maven-plugin,Java,Maven,Maven Plugin,我有一个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:schemaLocatio
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>
请帮帮我。我想。
您应该为springframework.version语句分配一个Spring版本。
比如说
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.1-REALESE</version>
</dependency>
org.springframework
弹簧芯
4.3.1-REALESE
然后是其他依赖项
<dependency>
<groupId>...framework...</groupId>
<artifactId>spring-...</artifactId>
<version>${springframework.version}</version>
</dependency>
框架
春天。。。
${springframework.version}
您可以使用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>
当你像你一样添加一个自定义执行的插件时,你必须意识到它只在你指定的阶段执行。您可以指定预清理阶段,该阶段是清理生命周期的一部分,而不是构建生命周期的一部分 Maven由生命周期组成,生命周期执行到给定的阶段。
mvn compile
命令实际上意味着运行构建生命周期的所有阶段,包括编译阶段
其中两个标准生命周期为(不完整列表):
指定的依赖项可能由标准插件用于编译阶段,因此属性需要在那时可用
当您声明预清理阶段时,当您执行mvn clean
时,属性可用
对于在编译阶段可用的属性,您可能应该绑定到验证阶段
虽然非常详细,但实际上有相当多的提示是在调试模式下运行的mvn-X
,但一开始可能太难理解
关于maven生命周期的更多信息,请点击此处:好吧,这正是他正在尝试的。。同样的插件,同样的目标。你忽略了他的问题,这是他所处的阶段。是的,尼尔森是正确的,我改变了执行阶段,这是不同的。它不起作用。它起作用了吗?它起作用了吗?为什么不使用最新版本的which 1.0.0?我不明白解决方案。我只有一个pom.xml文件,我没有任何父maven项目或模块我尝试过的阶段:验证和编译。但它不起作用,我正试图解决这个问题,但得到了同样的问题@SantoshkumarKalasa你的问题解决了吗?
<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>
springframework.version=4.2.5.RELEASE
clean :: pre-clean -> clean -> post-clean
build :: validate -> compile -> test -> package -> verify -> install -> deploy