views:

177

answers:

2

I'm writing a Maven plugin and I am using default values for all parameters such as this:

/**
 * The file with the site structure.
 * 
 * @parameter expression="${generateSite.siteFile}" default-value="${basedir}/src/oda/site.xml"
 */
private File siteFile;

Now I am adding a new parameter which is a collection. Is there a way to set default values for a parameter like the following one?

/**
 * A list of file/directory names to exclude in the processing.
 * 
 * @parameter ????
 */
private Set<String> excludes;
A: 

I don't think that Set is explicitly supported but the following will work:

/**
 * A list of file/directory names to exclude in the processing.
 *
 * @parameter
 */
private String[] myFiles;

You can then configure it using:

<myFiles>
  <param>value1</param>
  <param>value2</param>
</myFiles>

BTW this was taken from the Parameter Types With Multiple Values section on this page which also details other ways to allow parameters with multiple values.

Chris
According to the documentation you link to yourself any java.util.Collection should work -- this is how I came up with the original plan. It definitely does work with a Set in the way you describe it. But that is not setting default values, it is the normal project-specific configuration. The documentation does not mention anything about setting default values on the multi-valued parameters (or not being able to).
Peter Becker
Sorry, I obviously didn't read the question properly!However, I believe the answer is no. A few years ago I was seeking a similar answer on the Maven mailing lists, and I only found "I don't think so" answers. Digging around the Plexus code (which Maven uses a container) and this supported that answer.In the places that we wanted to do this, we ended up just setting the defaults in the execute() method.
Chris
+1  A: 

To my knowledge, this is actually not possible, there is no real way to specify default values for Parameter Types With Multiple Values (like Arrays, Collections, or Maps), at least not as parameter. I had to do this in the past too and, having read threads like array (or collecton) as a default-value of a mojo configuration parameter or configuring a list as default value for a plugin parameter, I ended up setting defaults in the execute() method, like Chris mentioned in a comment to his answer (see for example the flexmojos:wrapper plugin sources and the parameters parameter).

Pascal Thivent
That's the answer I suspected I would get. I had a brief glimpse at the Maven source code, but then it seemed I needed the Plexus source, too -- which is where I stopped. Let's just assume you are right ;-)
Peter Becker
Well, I'm still not 100% sure but flexmojos being developed by Sonatype, I guess they are representative of state of the art and are a trustful reference.
Pascal Thivent