pass a java parameter from maven - java

I need to execute some tests with maven, and pass a parameter from the command line.
My java code should get the parameter as:
System.getenv("my_parameter1");
and I define the parameter in the pom.xml file as the example below:
(and latter, I'd modify the pom.xml to get the parameter from the common line mvn clean install -Dmy_parameter1=value1)
but it does not work; System.getenv("my_parameter1") returns null.
how should I define the parameter in the pom.xml file?
pom.xml
<project>
...
<profiles>
<profile>
<properties>
<my_parameter1>value1</my_parameter1>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>slowTest</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/*Test.java</include>
<include>**/*TestSlow.java</include>
</includes>
<properties>
<my_parameter1>value1</my_parameter1>
</properties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

System.getenv() reads environment variables, such as PATH. What you want is to read a system property instead. The -D[system property name]=[value] is for system properties, not environment variables.
You have two options:
If you want to use environment variables, use the OS-specific method of setting the environment variable my_parameter1 before you launch Maven. In Windows, use set my_parameter1=<value>, in 'nix use export my_parameter1=<value>.
You can use System.getProperty() to read the system property value from within your code.
example:
String param = System.getProperty("my_parameter1");
In you surefire plugin configuration, you can use:
<configuration>
<systemPropertyVariables>
<my_property1>${my_property1}</my_property1>
</systemPropertyVariables>
</configuration>
Which takes the Maven property _my_property1_ and sets it also in your tests.
More details about this here.
I'm not sure if system properties from Maven are automatically passed to tests and/or whether fork mode affects whether this happens, so it's probably a good idea to pass them in explicitly.

Use
${env.my_parameter}
to access the environment variable in the pom.xml.
You can use the help plugin to see which variables are set with
mvn help:system
However the normal properties usage should work too. In the large context however I am wondering... what do you want to do? There might be a simpler solution.

The maven surefire plugin also has an option to set environment variables, just add this to your plugin configuration.
<environmentVariables>
<my_parameter1>value</my_parameter1>
</environmentVariables>
I think this requires that the plugin operates in fork mode, which is the default.

Related

How to exclude some directory in maven command line for test? [duplicate]

Something like the following.
I would like a way to skip my dao tests in surefire. Trying to avoid overhead of defining Suites.
With CI I'd like to have one nightly that runs all tests and another 5 minute poll of SCM that runs only 'fast' tests.
mvn -DskipPattern=**.dao.** test
Let me extend Sean's answer. This is what you set in pom.xml:
<properties>
<exclude.tests>nothing-to-exclude</exclude.tests>
</properties>
<profiles>
<profile>
<id>fast</id>
<properties>
<exclude.tests>**/*Dao*.java</exclude.tests>
</properties>
</profile>
</profiles>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>${exclude.tests}</exclude>
</excludes>
</configuration>
</plugin>
Then in CI you start them like this:
mvn -Pfast test
That's it.
Sure, no problem:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<excludes>
<!-- classes that include the name Dao -->
<exclude>**/*Dao*.java</exclude>
<!-- classes in a package whose last segment is named dao -->
<exclude>**/dao/*.java</exclude>
</excludes>
</configuration>
</plugin>
Reference:
Maven Surefire Plugin > Inclusions and Exclusions of Tests
(The excludes can not be configured via command line, so if you want to turn this behavior on conditionally, you will have to define a profile and activate that on the command line)
It is possible to exclude tests using the commandline; using ! to exclude.
Note: I'm not sure but possibly needs 2.19.1 or later version of surefire to work.
Examples:
This will not run TestHCatLoaderEncryption
mvn install '-Dtest=!TestHCatLoaderEncryption'
Exclude a package:
mvn install '-Dtest=!org.apache.hadoop.**'
This can be combined with positive filters as well. The following will run 0 tests:
mvn install '-Dtest=Test*CatLoaderEncryption,!TestHCatLoaderEncryption'
See the Maven Surefire docs.

run mvn release with several profiles

Is it possible to run maven with several profiles?
I have a java class annotated with #WebService. Depending on the maven profile the targetNamespace will change. If I run the
mvn release:prepare release:perform
twice, each time with a different profile, I will achieve what I want but the jar versions will not be same regarding the pom version.
So I thought running the release with 2 profiles could do it. Unfortunately when I enter
-P profile-1, -P profile-2
or
-P profile-1,profile-2
only one profile gets executed.
Here ary ma profiles:
<profiles>
<profile>
<id>profile-1</id>
<properties>
<target-namespace>sample-1.org</target-namespace>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>clean</phase>
<configuration>
<target>
<echo>${target-namespace}</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>profile-2</id>
<properties>
<target-namespace>sample-2.org</target-namespace>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>clean</phase>
<configuration>
<target>
<echo>${target-namespace}</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
I do print out the target-namespace properties to verify that in fact both profiles are running which is not the case.
Thanks
The second command-line (comma-separated profiles) is more correct, but won't quite do what you're trying to do, given your pom.
When you specify multiple profiles, they are all active in the same maven process -- it doesn't mean "run each goal with the first profile and then again with the second". So in the command that you are running, maven attempts to run with both profiles active.
Unfortunately, the contents of your profiles are overlapping. Well, the properties are. The <build> section is identical in each, which means that it could even be moved outside of the profiles and just declared once.
But the <properties> are completely overlapping ... hopefully it's clear that the target-namespace property cannot have two different values at once.
I wrote an answer below for some ways to address this properties question, which was posed in a very general way. But honestly, for anyone who ever reads this: there might easily be other, better ways to accomplish whatever you're actually trying to do. The OP was clearly attempting to do something more specific and this multiple profile thing was the method they were deciding to try, but they were confused when it wasn't working. A question about how to best solve their actual specific problem would probably have been a more productive question. But just in case this is a good way to solve your problem, here goes.
For the purposes of discussion I'm going to ignore the <build> contents above, which appear completely nonsensical, and assume that you need separate profiles because you'll sometimes want to activate each one independently of the other (maybe when not running release).
You can't use two values at once for the same property. You need to have multiple executions occur instead. You have a few different options for this...
Multiple commands. No changes needed to your pom, but this requires that your profiles have no real applicability to the actions in release:prepare, which you'll only run once. If that's not the case, this won't work. The first two commands could be combined into one but the third one has to be separate:
mvn release:prepare
mvn release:perform -P profile-1
mvn release:perform -P profile-2
Multiple <executions> of the release plugin itself, with different releaseProfiles in each one. This is a minor variation on option 1, but might be more appealing depending on your needs:
...
<artifactId>maven-release-plugin</artifactId>
<executions>
<execution>
<id>id-1</id>
<configuration>
<releaseProfiles>profile-1</releaseProfiles>
</configuration>
</execution>
<execution>
<id>id-2</id>
<configuration>
<releaseProfiles>profile-2</releaseProfiles>
</configuration>
</execution>
...
Because the release plugin isn't bound to any phase (you run it directly), you'll have to specify both executions separately on the command line, like mvn release:prepare release:perform#id-1 release:perform#id-2.
Use unique plugin execution ids within each profile -- then run mvn release:prepare release:perform -P profile-1,profile-2 as usual. You'd also have to change the profiles so that they use different property names. e.g.
...
<profile>
<id>profile-1</id>
<properties>
<namespace-1>sample-1.org</namespace-1>
...
<execution>
<id>antrun-1</id>
<configuration>
<target>
<echo>${namespace-1}</echo>
...
<profile>
<id>profile-2</id>
<properties>
<namespace-2>sample-2.org</namespace-2>
...
<id>antrun-2</id>
<configuration>
<target>
<echo>${namespace-2}</echo>
...
... this assumes that the all the plugin executions are such that they would be fine to run in a round-robin order like this for each phase of the overall build (clean phase = antrun 1 then antrun 2; test phase = surefire 1 then surefire 2; etc).
Due to Maven Documentation your second option is correct:
Profiles can be explicitly specified using the -P CLI option.
This option takes an argument that is a comma-delimited list of profile-ids to use. When this option is specified, the profile(s) specified in the option argument will be activated in addition to any profiles which are activated by their activation configuration or the section in settings.xml.
Hence you need to use next command format:
mvn <goals_list> -P profile-1,profile-2
If behaviour is incorrect there could be wrong profile configuration in your POM file. Could you please provide its content?
-P profile-1,profile-2 should be the right form, if it doesn't work it might be due to some conflicts etc. in the profiles themselves.
As an alternative you could try and play with the <activation> tags in settings.xml to activate the profiles based on some property, e.g.
<activation>
<property>
<name>releaseProfile</name>
</property>
</activation>
Then set the property in your mvn call: mvn -DreleaseProfile release:prepare release:perform. (You might have to pass a value for the property, it's been a while since I used that).

How to specify JAR location of a Maven dependency as plugin parameter?

I've tried to follow the configuration from this response on SO to use the jar location in my local repo as a plugin parameter, but it doesn't seem to work. I don't know if this due to a newer Maven version than the response (I'm using Maven 3.2.5).
In my pom.xml, I need to add a javaagent to my surefire plugin definition. The javaagent jar file is a dependency in my project.
I have tried the following:
<dependencies>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
...
<!-- Configuration to use jmockit on IBM J9 -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-javaagent:${org.jmockit:jmockit:jar}</argLine>
</configuration>
</plugin>
I was expecting that the ${org.jmockit:jmockit:jar} would be expanded to the location of the jar, but in my mvn console I see the following error:
[ERROR] Command wascmd.exe /X /C "C:\IBM\SDP\jdk\jre\bin\java -javaagent:${org.jmockit:jmockit:jar} -jar C:\dev\Eclipse\rtc-connector\target\surefire\surefirebooter1389906134960134.jar C:\dev\Eclipse\rtc-connector\target\surefire\surefire5488684370604495471tmp C:\dev\Eclipse\rtc-connector\target\surefire\surefire_05402037720997438783tmp"
So obviously the parameter is not getting expanded. I was hoping/expecting to see something like -javaagent:c:\users\eric\.m2\repository\org.jmockit\1.20\jmockit-1.20.jar or something similar.
Is there a clean way I can reference the jar from my dependency in my plugin configuration? I know I can use the dependency-plugin to copy the jar to a known location in my target folder and then point to that, but I was hoping there would be an easier solution that doesn't require the intermediary step.
To be variable expanded need to add maven-dependency-plugin:
<!-- obtain ${*:*:jar} properties -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-javaagent:${org.jmockit:jmockit:jar}</argLine>
</configuration>
</plugin>
You need to set the "argLine" tag value as follow :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<argLine>
-javaagent:"${localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar"
</argLine>
</configuration>
</plugin>
You need to set the .m2 repo path where the jmockit jar is present. It's working for me.
The maven-dependency-plugin contains a goal build-classpath which would solve this problem.
On command line you can do things like this:
mvn dependency:build-classpath -DincludeArtifactIds=testng -DincludeGroupIds=testng
which results in:
C:\repository\org\testng\testng\6.8.21\testng-6.8.21.jar
The resulting classpath can also be put into a property outputProperty ....
This could be configured into pom as well...

Maven exec plugin - user args from console after hardcoded args

I'm implementing a simple RMI server and client. I wanted to speed up the tedious task of adding server codebase each time (lots of terminal-bloating text), so I decided to use the maven exec plugin. Here's how a part of my pom.xml looks now:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<argument>/media/files/EclipseWorkspace/JavaSE/rozprochy/lab2/RmiServer/target/classes</argument>
<argument>-Djava.rmi.server.codebase=file:/media/files/EclipseWorkspace/JavaSE/rozprochy/lab2/RmiServer/target/classes/</argument>
<argument>engine.ComputeEngine</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
Everything's fine when i run mvn exec:exec in the console. The problem arises when I want to let the user specify the rmiregistry port for instance as an argument to the program. Basically, I'd like to add extra arguments from console, in addition to those specified in the POM file. All the solutions I've found overwrote the hardcoded args, when specifying new args from console, and this undesirable. Is it possible to do this somehow?
This is kind of a twisted workaround but I couldn't think of any other way to achieve what you want
Define a property in your pom with the default value for your additional parameter
<properties>
<extra.argument.from.console>extra.argument.from.console.default.value</extra.argument.from.console>
</properties>
In your execution add that property as an argument
<argument>${extra.argument.from.console}</argument>
When invoking maven give value to that property if you don't want to use the default value
mvn exec:exec -Dextra.argument.from.console=value.you.want

Is there a way to make maven build class files with UTF-8 without using the external JAVA_TOOL_OPTIONS?

I don't want to be dependable on a external environment variable to force maven to build my classes with UTF-8. On Mac, I was getting all sorts of problems when building with maven. Only the option below solved the problem:
export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8
mvn clean install
However I am distributing my project and it does NOT make sense to rely on the user to set this environment variable to build the project correctly.
Tried everything as described here: enabling UTF-8 encoding for clojure source files
Anyone has a light on that awesome Maven issue?
#Joop Eggen gave the right answer here: https://stackoverflow.com/a/10367745/962872
It is not enough to define that property. You MUST pass it inside the appropriate plugins. It won't go by magic inside there.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>...</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>...</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Yes there is, define
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
I was running into this problem, but only when running the compile from Emacs. I could not change the project's poms. What worked for me was to put the following in ~/.mavenrc
LANG=en_US.UTF-8

Categories

Resources