为什么这不起作用?如何从属性文件中选择版本号.
在pom.xml中读取属性
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
</execution>
<configuration>
<files>
<file>dev.properties</file>
</files>
</configuration>
</executions>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
在dev.properties中
org.aspectj.aspectjrt.version = 1.6.11
pom.xml中的依赖关系
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj.aspectjrt.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
错误:依赖项必须是有效版本
我们有一个项目布局,包含bom文件中的子模块和依赖项:
projectA
bom
module1
module2
Run Code Online (Sandbox Code Playgroud)
实际版本号被定义为bom文件中的属性,因此对于每个依赖项我们都有类似的东西
<properties>
<guice-version>4.1.0</guice-version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice-version}</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
projectA中的顶级pom导入dependecyManagement部分中的bom
<dependencyManagement>
<dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>bom</artifactId>
<version>1.0.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)
这一切都很好,我们有集中的依赖定义.
但是,在构建过程中的某个时刻,我们需要使用其中一个依赖项的版本.我希望在dependencyManagement部分中导入bom也会将属性导入顶级pom,但事实并非如此.也不可能将bom作为顶级pom的子节点,因为这会在pom文件之间创建循环依赖关系.
我考虑将属性放入外部文件,并在需要时使用maven属性插件读取它.那显然是在我们需要获取依赖版本的bom文件和pom文件中.但是,由于bom没有打包成jar,所以路径必须是硬编码的.
我可以通过将属性复制到两个地方来修复它,但我不想这样做.有没有办法获得依赖的版本,例如使用依赖项定义的属性?
问题似乎很常见,我想知道我们是否在项目结构中做错了.在这种情况下,集中属性的标准方法是什么?