英文:
Java Maven plugin Parameter validation
问题
我正在创建一个Maven插件,该插件接受参数,被接受的参数是一个具有字段的对象,我想对这些字段强制执行一些约束,有哪些选项可以实现呢?我考虑添加 hibernate-validator
,但也许还有更轻量级的选项可以实现这一点。在这个示例中,我想对名称字段强制执行最大长度,并通过正则表达式验证其是否为正确的电子邮件地址。
这是我的参数类:
public class Person {
private String name;
private String email;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
以及使用此参数的Mojo类:
@Mojo(name = "test")
public class TestMojo extends AbstractMojo {
@Parameter(required = true, readonly = true)
private Person person;
public void execute() throws MojoExecutionException, MojoFailureException {...}
}
英文:
I'm creating a maven plugin which accepts parameters, the parameter which is being accepted is an object with fields, and I want to enforce some constraints on those fields, what would be options to do it ? I was thinking to add hibernate-validator
but maybe there are more lightweight options to achieve this. In this example I want to enforce max length on name field and validate if it is correct email address via regex.
Here's my Parameter class:
public class Person {
private String name;
private String email;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
And Mojo class using this Parameter:
@Mojo(name = "test")
public class TestMojo extends AbstractMojo {
@Parameter(required = true, readonly = true)
private Person person;
public void execute() throws MojoExecutionException, MojoFailureException {...}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论