Maven 原型:验证 artifactId 或 groupId

JF *_*ier 5 java maven maven-archetype

我想构建一个 Maven 原型来检查提供的 artifactId 和 groupId 是否与给定的正则表达式匹配。通过这种方式,我想强制执行我们组织的命名约定,例如,ear 文件的名称-app以 de.companyname结尾,所有 groupId 以 de.companyname 开头。

这可能吗?

我发现你可以检查正则表达式 requiredProperty

https://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html

但是当我通过 eclipse 构建原型时,给定的值被忽略,这可能是由于 eclipse 中使用的 maven-archetype-plugin 的旧版本(这不适用于 groupId 等“内置”属性或工件 ID)。

gly*_*ing 5

这个:

  <requiredProperties>
    <requiredProperty key=.. >
      <defaultValue/>
      <validationRegex/>
    </requiredProperty>
  </requiredProperties>
Run Code Online (Sandbox Code Playgroud)

...定义必需属性的方法(带有默认值和验证)。但是,IIRC,它是在原型插件的 v3.0.0 中引入的,所以也许您使用的是以前的版本。

编辑 1:在回答这个问题时“可以将validationRegex应用于artifactId和groupId”。是的,它可以。它可以应用于任何条目,requiredProperties但有一个警告:validationRegex仅适用于命令行提供的输入,因此defaultValue通过命令行参数 ( -DgroupId=..., -DartifactId=...) 侧步骤验证提供或定义值。这里有一个具体的例子,下面给出requiredPropertiesarchetype-descriptor.xml

<requiredProperties>
  <requiredProperty key="artifactId">
    <validationRegex>^[a-z]*$</validationRegex>
  </requiredProperty>
  <requiredProperty key="groupId">
    <defaultValue>COM.XYZ.PQR</defaultValue>
    <validationRegex>^[a-z]*$</validationRegex>
  </requiredProperty>
</requiredProperties>
Run Code Online (Sandbox Code Playgroud)

以下命令:mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DgroupId=com.foo.bar将导致com.foo.bar用于 groupId 并且将提示用户提供一个 artifactId 像这样:

定义属性 'username' 的值(应该匹配表达式 '^[az]*$'):无论如何

值与表达式不匹配,请重试:whatever

定义属性值...

到目前为止很好(有点)。

但是以下命令mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DartifactId=whatever将导致COM.XYZ.PQR被用于 groupId,即使它不符合validationRegex.

相似地; 以下命令mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DartifactId=WHATEVER将导致COM.XYZ.PQR用于 groupId 和WHATEVER用于 artifactId 即使这些值不符合validationRegex.

因此,总而言之:validationRegex适用于任何 requiredProperty(无论是保留属性- 例如 artifactId - 还是定制属性),但它仅适用于交互式提供的值,因此设置默认值或通过命令行提供值参数侧步骤验证。

注意:即使您确实使用了,validationRegex您也可能需要考虑使用 Maven Enforcer 插件的requireProperty 规则,因为在使用原型创建项目后,您要强制执行的项目属性可能会更改。从文档:

此规则可以强制设置已声明的属性,并可选择根据正则表达式对其进行评估。

下面是一个例子:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
      <execution>
        <id>enforce-property</id>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <requireProperty>
              <property>project.artifactId</property>
              <message>"Project artifactId must match ...some naming convention..."</message>
              <regex>...naming convention regex...</regex>
              <regexMessage>"Project artifactId must ..."</regexMessage>
            </requireProperty>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
    </executions>
  </plugin>
Run Code Online (Sandbox Code Playgroud)