java artifact_java-Maven原型:验证artifactId或groupId

这个:

…是定义必需属性(带有默认值和验证)的方法.但是,IIRC是在原型插件的v3.0.0中引入的,因此也许您使用的是以前的版本.

编辑1:针对此问题“ validationRegex是否可以应用于artifactId和groupId”.是的,它可以.可以将其应用于requiredProperties中的任何条目,但需要注意以下几点:validationRegex仅适用于命令行提供的输入,因此提供defaultValue或通过命令行参数(-DgroupId = …,-DartifactId =定义值). ..)边步骤验证.这是一个具体示例,在archetype-descriptor.xml中提供了以下requiredProperties:

^[a-z]*$

COM.XYZ.PQR

^[a-z]*$

以下命令: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.

这是一个例子:

org.apache.maven.plugins

maven-enforcer-plugin

3.0.0

enforce-property

enforce

project.artifactId

"Project artifactId must match ...some naming convention..."

...naming convention regex...

"Project artifactId must ..."

true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值