Java Maven插件参数验证

huangapple 未分类评论59阅读模式
英文:

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 {...}

}

huangapple
  • 本文由 发表于 2020年5月29日 19:33:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/62085019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定