String arrays as command line arguments for maven plugin

torbjornvatn picture torbjornvatn · Aug 26, 2009 · Viewed 14.8k times · Source

I'm writing a maven plugin that has a parameter that's a String[].

Like this:

/**
* @parameter expression="${args}"
*/
protected String[] args;

This can be utilized through the POM like this:

<args>
  <arg>arg1</arg>
  <arg>arg2</arg>
<args>

But I want to send it in from the command line

-Dargs={arg1, arg2}

Is this possible?

Answer

Rich Seller picture Rich Seller · Aug 27, 2009

You can't do it directly as far as I know, but it is pretty common practice to accept a delimited String and split that into an array yourself.

For example the maven-site-plugin allows you to specify a comma-delimited String of locales, while the maven-scala-plugin handles this by allowing you to define the arguments with a pipe separator. You can look at the relevant Mojos to see how the argument is processed.

Some example usages below:

site-plugin:

-Dlocales=enGB,frFR

scala-plugin:

-DaddArgs=arg1|arg2|arg3

Update: if you want to handle this more elegantly, you could use maven-shared-io to allow definition of an external descriptor file, then pass the descriptor location as a property. This means a single command-line argument can reference a structure of configuration.

If this sounds like it might work for you, have a look at this answer that describes how to use external descriptors in the properties plugin, or this answer that does similar for the xml-maven-plugin. Or you can just look at the assembly-plugin for ideas.