有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaMaven读取属性插件不能用值替换依赖项

以下是我的项目结构

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>

道具。属性文件具有

spring-version=3.1.0.RELEASE

我曾尝试使用maven read properties插件来读取属性,并将其替换为spring版本。但它抛出了一个错误,即“依赖性”。附属国。组织的“版本”。springframework:SpringBeans:jar必须是有效版本,但为“${SpringVersion}”

我尝试在maven执行阶段使用验证阶段而不是初始化。但问题依然存在。我尝试将属性文件位置替换为绝对路径D:\test proj\prop,而不是上下文路径。配置中的属性对我也没有帮助。我使用的是maven编译器插件版本2.3.2。我错过什么了吗?请让我知道用其他插件替代依赖版本是否可行

注意:我将不能使用父子pom关系,因为我的所有项目都是模块化的,它们不依赖于同一个父项目


共 (2) 个答案

  1. # 1 楼答案

    如果需要多个不相关的项目共享依赖项版本,可以使用物料清单概念http://howtodoinjava.com/maven/maven-bom-bill-of-materials-dependency/

    1. 使用packagingpom创建不相关的BOM项目。将共享依赖项添加到项目中
    2. 每个项目都必须导入BOM表项目

    下面是示例页面的片段

    How to add BOM [Bill Of Materials] dependency

    Maven provides a tag dependencyManagement for this purpose. You need to add the bom information in this tag as follows. I am taking the example of Spring bom file.

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-framework-bom</artifactId>
          <version>4.0.1.RELEASE</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    

    An added benefit of using the BOM is that you no longer need to specify the version attribute when depending on Spring Framework artifacts. So it will work perfectly fine.

    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    
  2. # 2 楼答案

    我无法想象这会起作用。AFAIU Maven必须在任何构建执行之前解决依赖关系,否则它应该如何为执行创建正确的类路径

    回复:

    I will not be able to use parent-child pom relationship as all my projects are modular and they don't depend on the same parent.

    我认为你有能力。建立一个super-parentPOM,其中包含所有人都要继承的适当设置,并使所有项目的父项都是以下内容的子项:

    +- super-parent
      +- pom.xml ... containing setting(s) for all
      +- parent-project-1
         +- ...
      +- parent-project-2
         +- ...
      +- ...