【问题标题】:Require Maven property to be defined in child modules要求在子模块中定义 Maven 属性
【发布时间】:2013-06-25 18:59:40
【问题描述】:

我最近使用Maven Enforcer Plugin 来强制要求所有 POM 都定义一个foo.bar 属性。我将此声明放在我的公司 POM 中,并假设它将适用于我的子项目。

令我沮丧(但并不意外),该规则也在我的公司 POM 中强制执行。结果,我尽职尽责地定义了一个占位符foo.bar 属性,并认为我已经完成了。

不幸的是,所有子项目都继承了这个属性,从而通过了强制测试。我无法确定孩子是明确定义了这个属性还是只是继承了一个无意义的值。任何人都可以提出一种方法:

  • 确保此(特定)规则不适用于我的公司 POM;

  • 确保我的占位符属性不被子项目继承;

  • 用另一种方式解决我的问题?

如果有帮助,我的实施者规则的定义如下所示。这个 sn-p 来自我公司的 POM。

<!-- Enforce good behaviour in child POMs -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>1.2</version>
  <executions>
    <execution>
      <id>enforce-good-behaviour</id>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <requireProperty>
            <property>foo.bar</property>
            <message>NAUGHTY!</message>
            <regex>.+</regex>
            <regexMessage>The property must contain at least one character.</regexMessage>
          </requireProperty>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>

我的目标是自动将此属性值用作 SCM 标记指令的一部分。我的公司 POM 中有以下 sn-p,它为我的子项目定义了一个很好的标记方案:

<!-- Ensure we use a consistent tagging scheme for releases -->
<plugin>
  <artifactId>maven-release-plugin</artifactId>
  <configuration>
    <tagNameFormat>a.b.c.${foo.bar}</tagNameFormat>
    <useReleaseProfile>false</useReleaseProfile>
  </configuration>
</plugin>

【问题讨论】:

  • 小心,因为任何人都可以将 enforcer.skip 属性设置为 true 以绕过您要完成的任务。
  • @Michael 感谢您的警告。在我的环境中,人们不会积极尝试颠覆系统,所以希望一切都会好起来。如果他们这样做,那么……我是老板,他们注定要失败:-)
  • 同意 - 如果他们确实决定积极颠覆,SCM 系统可能会以某种方式阻塞${} 字符,让他们清理得一团糟(并且必须向邓肯解释为什么他们正在这样做)。
  • 邓肯 - 很划算。我只提到它是因为我有几个人将“maven.test.skip”设置为true,因为他们不喜欢junit运行每个构建。

标签: maven pom.xml maven-enforcer-plugin


【解决方案1】:

我也有同样的困境。这是我解决它的方法。

在公司 POM 中,我添加了这样的配置文件:

    <profile>
    <!-- When we are building the corporate base projects we don't want 
         to require that all of the properties that should be provided by 
         child projects (inheriters) are defined. So we activate this 
         profile when building the corporate projects to bypass anything 
         that only applies to children (the leaf projects). 
         Add "-Dcorporate.build=true" on the maven cmd line when building
         and releasing the corporate POMs to accomplish this. -->
        <id>corporate-pom-build</id>
        <activation>
            <property>
                <name>corporate.build</name>
                <value>true</value>
            </property>
        </activation>
        <properties>
            <enforcer.skip>true</enforcer.skip>
            <remoteresources.skip>true</remoteresources.skip>
            <assembly.skipAssembly>true</assembly.skipAssembly>
        </properties>
    </profile>

然后,正如评论所说,我使用

构建公司 POM

mvn -Dcorporate.build=true clean deploy

您想跳过的其他内容也可以放在该个人资料中。像魅力一样工作。

【讨论】:

    【解决方案2】:

    extra-enforcer-rulesrequirePropertyDiverges 规则允许您检查在父 POM 中定义的属性是否在子 POM 中被覆盖。

    对于您的用例,配置应该是这样的(尚未测试):

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <version>1.2</version>
      <executions>
        <execution>
          <id>enforce-property-overridden-in-child</id>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requirePropertyDiverges>
                <property>foo.bar</property>
              </requirePropertyDiverges>
            </rules>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>extra-enforcer-rules</artifactId>
          <version>1.0-alpha-5</version>
        </dependency>
      </dependencies>
    </plugin>
    

    【讨论】:

    • 我无法让它工作。我一直收到错误消息:找不到定义当前规则的父 POM
    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2013-10-17
    • 2014-10-19
    • 1970-01-01
    • 2013-10-19
    • 2018-08-31
    • 1970-01-01
    相关资源
    最近更新 更多