验证 Maven 原型属性

Dav*_*d V 2 maven-2 maven maven-archetype

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

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

mry*_*yan 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>
Run Code Online (Sandbox Code Playgroud)

然后您将获得经过验证的输入:

[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: 
Run Code Online (Sandbox Code Playgroud)

注意:它仍然不会用正则表达式验证批量输入。请参阅:https ://issues.apache.org/jira/browse/ARCHETYPE-532

  • 这不是自我推销......我没有链接到我自己的库,我链接到我对问题作者使用的原始库所做的开源贡献。这是他请求的功能的精确实现,不需要进一步解释。 (5认同)