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?
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:
scala-plugin:
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.
According to Sonatype's blog here, if you are a plugin developer and
and annotate your array/collection type plugin parameter using annotation like:
/** @parameter expression="${args}" */
In this way, the plugin parameter can be processed by Maven automatically and plugin users can provide the plugin array/collection type parameters via CLI using a comma separated system property like
mvn myplugin:mygoal -Dargs=a,b,c
The way to specify a list of values via system property, for a plugin depends on how up to date the plugin is.
However, if you are dealing with a proper implemented plugin that is up to date, then the correct way of specifying an array of values to a plugin is via comma separated strings.
Here is a reference: http://blog.sonatype.com/2011/03/configuring-plugin-goals-in-maven-3/
Here is a quote from the reference:
Going a little bit further, we can look at more concrete example. Consider, the Wildfly maven plugin. This plugin has a deprecated configuration property called: jvmArgs.
This was expected to be passed in as a space separated list of values. As we all know, in the command line, messing around with spaces is not adorable. So if we look at the definition of this paramter in the plugin mojo code, you will find something like this (here goes another quote).
So this is the old way of doing stuff.
Now, if you are using the latest version of this plugin (e.g. Alpha6). Then the source code will have a nice new field called javaOpts. Let us look at what the field looks like in the code.
So what we see is that we have a nice array field in the StartMojo. This array field is properly annoted. And the maven engine will do the heavy lifting of setting the values into the Mojo.
When you want to pump data into this field via the command line, you would in you batch file specify something of the form:
If you try the samething using sapces instead of commans. I will show you what happens:
So you see, maven when it with swallowed my system property full of spaces it did not do a string split. So Wildfly tried to setup the jvm memory settings as if the max memory was that full string. On the other hand, when I use commas to separate it, the Mojo is properly enriched and I can take control of the memory settings of the app server when it starts up.
And of course, you want to use system properties and not pom.xml XML configuration, for tasks like setting up Jenkins jobs. With system properties you are rather more flexible.
That is it.
Latest maven (3.0.3) should works with:
-DaddArgs=arg1,arg2,arg3
To update on @nybon’s answer a bit, it seems
works, at least when using
maven-plugin-annotations:3.5
in Maven 3.5.0. Running withsets the list.