Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String arrays as command line arguments for maven plugin

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?

like image 943
torbjornvatn Avatar asked Aug 26 '09 14:08

torbjornvatn


People also ask

What is the correct syntax for executing a Maven plugin?

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”.

How do you pass variables in POM xml?

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.

Which property value arguments are passed to Maven POM xml?

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.


3 Answers

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.

like image 119
Rich Seller Avatar answered Sep 24 '22 16:09

Rich Seller


Latest maven (3.0.3) should works with:

-DaddArgs=arg1,arg2,arg3

like image 8
mrduguo Avatar answered Sep 20 '22 16:09

mrduguo


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.

like image 3
Jesse Glick Avatar answered Sep 20 '22 16:09

Jesse Glick