views:

161

answers:

1

Hi,

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

+2  A: 

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;
Rich Seller
Thanks, using a bogus default value will work great.
Lehane
I wouldn't say it is great, keep an eye on that Jira as enums are a much better solution
Rich Seller