2

有没有办法验证提供给 Maven 原型的属性?如果有怎么办?

我的archetype-metadata.xml 文件中有一个自定义的requiredProperty。这些值必须与某个正则表达式匹配,我想在原型:生成期间验证这一点。

4

2 回答 2

5

我已经在 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

于 2015-09-04T12:20:07.040 回答
0

我现在正在看这个,尽管在原型插件中没有明确支持这一点,也没有在原型-metadata.xml 中设置验证规则/类型的方法,因为它使用速度,你可以操纵它并让它做一些基本验证。正如评论所说 - 它并不漂亮,但它有效......

请参阅这篇文章Maven 原型所需的属性编号

更新:这现在是可能的。请参阅上面的其他答案,或https://issues.apache.org/jira/browse/ARCHETYPE-487

于 2014-06-26T15:47:06.500 回答