java - 在没有公共(public)父模块的子模块中重用 Maven 属性

标签 java maven dry maven-module

在maven多模块项目中,有一个子模块需要从外部父项目继承。因此,它不能像其他子模块一样从父模块继承,并且那些子模块也不能继承该外部父模块(因此不能选择使外部项目成为整个层次结构的父模块)。

有没有办法消除此类模块与层次结构其余部分之间的属性重复?

父级 pom.xml

    <properties>
        <foo>bar</foo>
    </properties>

    <modules>
        <module>child</module>
        <module>stepchild</module>
    </modules>

pom.xml

    <parent>
        <groupId>my</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <description>foo can be used: ${foo}</description>

继子 pom.xml

    <parent>
        <groupId>external</groupId>
        <artifactId>parent</artifactId>
        <version>35</version>
        <relativePath/>
    </parent>
    <description>foo does not get substituted: ${foo}</description>

最佳答案

除了其他用户在上面的评论中发现的所有问题之外,您还可以使用可重用文件“my.properties”来设置属性,其中包含 foo=酒吧

并添加到maven:

<build>    
    <plugins>
      <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>${basedir}/../project-parent/my.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
   </plugins>

关于java - 在没有公共(public)父模块的子模块中重用 Maven 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59842319/

相关文章:

java - Java中线程完成时关闭资源

java - Eclipse 打开项目中包含的文件

java - 如何调试 loadLibrary 以了解为什么不加载 DLL?

spring - Maven/Spring 未正确运行 jmeter 测试

java - 如何遵循 Java 对象 setter 中的 DRY?

java - Java中的温度转换代码无法运行?

maven - 如何将第 3 方 JAR 添加到我的 Travis-CI maven 构建中?

java - 范围 "provided"不添加到类路径中的 jars

html - 复制 Youtube 的导航栏,但是,我习惯使用位置 : absolute; I need a more effective way of doing this

javascript - 重构重复代码-js