I have a project configured to build and run with Maven. The project depends on platform specific native libraries, and I'm using the strategy found here to manage those dependencies.
Essentially, the .dll
or .so
files for a particular platform are packaged into a jar, and pushed to the Maven server with a classifier identifying the target platform. The maven-dependency-plugin then unpacks the platform specific jar, and copies the native libraries to the target folder.
Normally I would use mvn exec:java
to run a Java program, but exec:java
runs applications in the same JVM as Maven, which prevents me from modifying the classpath. Since the native dependencies must be added to the classpath, I am forced to use mvn exec:exec
instead. This is the relevant snippet of the pom:
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Djava.library.path=target/lib</argument>
<argument>-classpath</argument>
<classpath />
<argument>com.example.app.MainClass</argument>
</arguments>
</configuration>
</plugin>
...
This works fine for the default configuration of the application, but I want to be able to specify some optional parameters at the command line. Ideally, I'd like to do something like this:
mvn exec:exec -Dexec.args="-a <an argument> -b <another argument>"
Unfortunately, specifying the exec.args
variable overwrites the arguments I have in the pom (which are required to set up the classpath and run the application). Is there a way around this? What's the best way to specify some optional arguments at the command line without overwriting what I have in the pom?
You could run: mvn exec:exec -Dexec. args="arg1". This will pass the argument arg1 to your program. By using the -f parameter, you can also run it from other directories.
Maven exec plugin allows us to execute system and Java programs from the maven command. There are two goals of the maven exec plugin: exec:exec - can be used to execute any program in a separate process. exec:java - can be used to run a Java program in the same VM.
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”. For example mvn example:version .
To provide System Properties to the tests from command line, you just need to configure maven surefire plugin and use -D{systemproperty}={propertyvalue} parameter in commandline. Run Single Test with Maven : $ mvn test -Dtest=MessageUtilTest#msg_add_test -Dmy_message="Hello, Developer!"
I managed to find a reasonably elegant solution to my problem using Maven environment variables.
The default values are defined as properties in the pom, and added to the exec plugin as arguments:
...
<properties>
<argumentA>defaultA</argumentA>
<argumentB>defaultB</argumentB>
</properties>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Djava.library.path=${project.build.directory}/lib</argument>
<argument>-classpath</argument>
<classpath />
<argument>com.example.app.MainClass</argument>
<argument>-a</argument>
<argument>${argumentA}</argument>
<argument>-b</argument>
<argument>${argumentB}</argument>
</arguments>
</configuration>
</plugin>
...
Now I can run with default parameters exactly as I did before:
mvn exec:exec
And I can easily override the defaults for each argument at the command line using:
mvn exec:exec -DargumentA=alternateA -DargumentB=alternateB
I spent some time understanding the meaning of the tag "<classpath />". Sharing the details for benefit of the community. It adds the dependent libraries which are defined in the pom.xml, to the JVM. For more details, check https://www.mojohaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html
.... If specified as part of the exec.args argument, the special string %classpath will be replaced by the project classpath as computed by Maven. Same couunts for %modulepath
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