
这可能吗?
我发现您可以对照正则表达式检查requiredProperty
https://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html
但是当我通过eclipse构建原型时,给定的值将被忽略,这可能是由于eclipse中使用了旧版本的maven-archetype-plugin(而且不适用于诸如groupId或artifactId).
<requiredProperties>
<requiredProperty key=.. >
<defaultValue/>
<validationRegex/>
</requiredProperty>
</requiredProperties>
…是定义必需属性(带有默认值和验证)的方法.但是,IIRC是在原型插件的v3.0.0中引入的,因此也许您使用的是以前的版本.
编辑1:针对此问题“ validationRegex是否可以应用于artifactId和groupId”.是的,它可以.可以将其应用于requiredProperties中的任何条目,但需要注意以下几点:validationRegex仅适用于命令行提供的输入,因此提供defaultValue或通过命令行参数(-DgroupId = …,-DartifactId =定义值). ..)边步骤验证.这是一个具体示例,在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,如下所示:
Define value for property ‘username’ (should match expression ‘^[a-z]*$’): Whatever
Value does not match the expression, please try again: whatever
Define value for property…
到目前为止,一切都很好.
但是以下命令mvn archetype:generate -DarchetypeGroupId = … -DarchetypeArtifactId = … -DarchetypeVersion = … -DartifactId =无论如何,都会导致COM.XYZ.PQR用于groupId,即使这不符合validationRegex.
同样下面的命令mvn archetype:generate -DarchetypeGroupId = … -DarchetypeArtifactId = … -DarchetypeVersion = … -DartifactId = WHATEVER会导致COM.XYZ.PQR被用于groupId,而无论被用于artifactId,值不符合validationRegex.
因此,总而言之:validationRegex适用于任何requiredProperty(无论是保留属性(例如artifactId)还是定制属性),但仅适用于以交互方式提供的值,因此设置默认值或通过命令提供值行参数侧步骤验证.
注意:即使您确实使用validationRegex,您也可能要考虑使用Maven Enforcer插件的requireProperty rule,因为在使用原型创建项目后,可以更改要执行的项目属性.从文档:
This rule can enforce that a declared property is set and optionally evaluate it against a regular expression.
这是一个例子:
<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>
转载注明原文:java-Maven原型:验证artifactId或groupId - 乐贴网