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?
Usage of a Maven Plugin xml you can use the shorthand notation to execute the plugin: mvn <prefix>:<goal> , commonly the “prefix” is the artifact ID minus the “-maven-plugin”.
To refer to environment variables from the pom. xml, we can use the ${env. VARIABLE_NAME} syntax. We should remember to pass the Java version information via environment variables.
Passing an Argument to Maven Maven will use the value (2.5) passed as an argument to replace the COMMON_VERSION_CMD property set in our pom. xml. This is not limited to the package command — we can pass arguments together with any Maven command, such as install, test, or build.
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.
Latest maven (3.0.3) should works with:
-DaddArgs=arg1,arg2,arg3
To update on @nybon’s answer a bit, it seems
@Parameter(property="your.param")
private List<String> yourParam;
works, at least when using maven-plugin-annotations:3.5
in Maven 3.5.0. Running with
-Dyour.param=val1,val2
sets the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With