这个:
<requiredProperties>
<requiredProperty key=.. >
<defaultValue/>
<validationRegex/>
</requiredProperty>
</requiredProperties>
... 是定义所需属性的方式(具有默认值和验证)。但是,IIRC,它是在原型插件的 v3.0.0 中引入的,所以您可能使用的是以前的版本。
编辑 1:针对这个问题“可以将validationRegex 应用于artifactId 和groupId”。是的,它可以。它可以应用于requiredProperties 中的任何条目,但需要注意的是:validationRegex 仅适用于命令行提供的输入,因此提供defaultValue 或通过命令行参数定义值(-DgroupId=...、@ 987654327@ ) 侧步骤验证。这是一个具体的例子,在archetype-descriptor.xml 中给出以下requiredProperties:
<requiredProperties>
<requiredProperty key="artifactId">
<validationRegex>^[a-z]*$</validationRegex>
</requiredProperty>
<requiredProperty key="groupId">
<defaultValue>COM.XYZ.PQR</defaultValue>
<validationRegex>^[a-z]*$</validationRegex>
</requiredProperty>
</requiredProperties>
以下命令:mvn archetype:generate -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DgroupId=com.foo.bar 将导致 com.foo.bar 用于 groupId,并且将提示用户提供 artifactId,如下所示:
定义属性“用户名”的值(应匹配表达式“^[a-z]*$”):随便
值与表达式不匹配,请重试: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 rule,因为在使用原型创建项目后,您想要强制执行的项目属性可能会发生变化。来自文档:
此规则可以强制设置已声明的属性,并可选择根据正则表达式对其进行评估。
这是一个例子:
<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>