【发布时间】:2016-08-01 06:08:24
【问题描述】:
以下是我的项目结构。
test-proj
|_ src
|_main
| |_java
|_test
|_java
|_prop.properties
|_pom.xml
还有我的 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.sample</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<!-- The resources tag will be used if prop file is under src location. -->
<!-- <resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources> -->
<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>${basedir}/prop.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
</project>
Prop.properties 文件有
spring-version=3.1.0.RELEASE
我尝试使用 maven-read 属性插件来读取属性并用它代替 spring 版本。但是它会抛出错误,说 org.springframework:spring-beans:jar 的 'dependencies.dependency.version' 必须是有效版本,但是是 '${spring-version}'。
我尝试在 Maven 执行阶段使用验证阶段而不是初始化。但问题仍然存在。我尝试将属性文件位置替换为配置中的绝对路径 D:\test-proj\prop.properties 而不是上下文路径,这对我也没有帮助。我正在使用 maven 编译器插件版本 2.3.2。我错过了什么吗?请让我知道用其他插件替换依赖版本是否可行。
注意:我将无法使用父子 pom 关系,因为我所有的项目都是模块化的,它们不依赖于同一个父项
【问题讨论】:
-
你的属性真的在 basedir 文件夹上吗? basedir 表示项目的根文件夹,即 pom.xml 文件所在的位置。我认为您的属性可能在资源文件夹中,对吧?
-
@dambros 我尝试将它放在根目录和资源中。如您所见,prop.properties 的当前位置是 pom.xml 可用的根文件夹,我将其称为 ${basedir.properties}/prop.properties
-
有什么特别的理由不在 pom 内部声明依赖版本吗?这里只是想了解一下插件的使用方法,因为看起来有点矫枉过正
-
@dambros 我的大多数项目都是独立的,但很少有共同的属性,如 sql 版本、spring 版本等。所以我想在属性文件中声明这些公共属性并在需要的地方使用它们.父子无法工作,因为我的项目结构不是这样设计的。
-
@Poppy 好点;告诉我们你想要实现什么。版本保存在 pom.xml 中有什么问题
标签: java spring maven dependencies pom.xml