限制Java maven插件配置参数

发布于 2024-08-02 15:25:10 字数 679 浏览 3 评论 0原文

我正在编写一个带有许多可配置参数的 Maven 插件。 Mojo 类中指定了许多参数。这些参数之一是必需的,并且必须包含某些值(例如,“Atwood”或“Spolsky”)。目前它带有注释。 @required 字段如下所示:

public class GenerateMojo extends AbstractMojo{
   ...
   ...

   /**
   *@parameter
   *@required
   */
   private String someParameter;
   ...
   ...
}

这一切都很好,但是如果有人忘记包含参数,他们会收到如下通用错误消息:

Inside the definition for plugin 'xyz' specify the following:
<configuration>    
    ...   
    <someParameter>VALUE</someParameter>
</configuration>

如果可以 (1) 限制可以输入到 someParmeter 字段的值给出更好的错误消息,或者 (2) 自己指定错误消息,以便我可以编写类似“'someParameter' 的值必须是 'Atwood' 或 'Spolsky' ??

谢谢

I’m writing a maven plugin with a number of configurable parameters. There are a number of parameters specified in the Mojo class. One of these parameters is required and must contain certain values (let’s say, either ‘Atwood’ or ‘Spolsky’). At the moment it is annotated with a. @required field as shows here:

public class GenerateMojo extends AbstractMojo{
   ...
   ...

   /**
   *@parameter
   *@required
   */
   private String someParameter;
   ...
   ...
}

Which is all well and good, but if someone forgets to include the parameter they get a generic error message like so:

Inside the definition for plugin 'xyz' specify the following:
<configuration>    
    ...   
    <someParameter>VALUE</someParameter>
</configuration>

If is possible to either (1) restrict the values that can be inputted to the someParmeter field to give a better error message, or (2) specify the error message myself so that I can write something like “The value for ‘someParameter’ needs to be either ‘Atwood’ or ‘Spolsky’ ??

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮子 2024-08-09 15:25:10

有一个开放的 Jira 添加对 Maven 2.2 中参数值枚举的支持(它Java 5 上的 Plexus 已支持)。

您可以指定一个默认值,这样至少 Mojo 不会在初始化时失败。然后,您可以验证execute() 方法中参数的值并输出更有用的消息。

如果没有合理的默认值,您可以将默认值设置为将在execute()方法中无效的值,这实际上意味着用户必须定义​​它,并且他们会收到有意义的错误消息。例如:

/**
 * @parameter expression="${someParameter}" default-value="_"
 */
private String someParameter;

There is an open Jira to add support for enumerations to parameter values in Maven 2.2 (it is already supported in Plexus on Java 5).

You can specify a default value so at least the Mojo won't fail in initialisation. You can then validate the value of the parameter in the execute() method and output a more helpful message.

If there is no sensible default, you can set the default to a value that will be invalidated in the execute() method, this effectively means the user will have to define it, and they get a meaningful error message. For example:

/**
 * @parameter expression="${someParameter}" default-value="_"
 */
private String someParameter;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文