有没有办法验证提供给Maven原型的属性?如果是这样的话?
我的archetype-metadata.xml文件中有一个自定义的requiredProperty。值必须与某个正则表达式匹配,我想在archetype:generate。
期间验证它答案 0 :(得分:3)
我已在maven-archetype中实现了此功能,并将其作为拉取请求提交:
https://issues.apache.org/jira/browse/ARCHETYPE-487
示例:
<requiredProperties>
<requiredProperty key="pluginPackage">
<validationRegex><![CDATA[^[a-z][a-z0-9_]*(\.[a-z0-9_]+)+[0-9a-z_]$]]></validationRegex>
</requiredProperty>
<requiredProperty key="pluginId">
<validationRegex><![CDATA[^[a-zA-Z0-9-]+$]]></validationRegex>
</requiredProperty>
<requiredProperty key="pluginName">
<validationRegex><![CDATA[^([a-zA-Z_$][a-zA-Z\d_$]*\.)*[a-zA-Z_$][a-zA-Z\d_$]*$]]></validationRegex>
</requiredProperty>
<requiredProperty key="pluginProvider">
</requiredProperty>
<requiredProperty key="pluginZipFileName">
<validationRegex><![CDATA[^[^*&%\s]+$]]></validationRegex>
</requiredProperty>
<requiredProperty key="pluginVersion">
<validationRegex><![CDATA[^[0-9]+\.[0-9]+\.[0-9]+$]]></validationRegex>
</requiredProperty>
</requiredProperties>
然后您将获得经过验证的输入:
[INFO] Using property: groupId = com.nick
Define value for property 'artifactId': abc
Define value for property 'version' 1.0-SNAPSHOT: :
[INFO] Using property: package = com.nick
Define value for property 'pluginId' (should match expression '^[a-zA-Z0-9-]+$'): test-plugin525
Define value for property 'pluginName' (should match expression '^([a-zA-Z_$][a-zA-Z\d_$]*\.)*[a-zA-Z_$][a-zA-Z\d_$]*$'): NickTest101
Define value for property 'pluginPackage' (should match expression '^[a-z][a-z0-9_]*(\.[a-z0-9_]+)+[0-9a-z_]$'): com.nicholas.fun
Define value for property 'pluginProvider': Nicholas DiPiazza
Define value for property 'pluginVersion' (should match expression '^[0-9]+\.[0-9]+\.[0-9]+$'): 1.0
Value does not match the expression, please try again:
注意:它仍然不会正则表达式验证批输入。请参阅:https://issues.apache.org/jira/browse/ARCHETYPE-532
答案 1 :(得分:0)
我现在正在看这个,虽然在archetype插件中没有明确的支持,或者在archetype-metadata.xml中设置验证规则/类型的方法,因为这使用了速度,你可以用它来获取它做一些基本的验证。正如评论所说 - 它很漂亮,但它有效...
请参阅此帖子Maven archetype required property number
更新:现在可以了。请参阅上面的其他答案,或https://issues.apache.org/jira/browse/ARCHETYPE-487