Set VM option from maven config - java

Is there any way to set an argument for the command line from the maven config? Maybe some plugin?
I need to run my service on spring boot with the following VM option:
-Dspring.config.additional-location=classpath:/config/business-config.yml
I have tried using maven-surefire-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-Dspring.config.additional-location=classpath:/config/business-config.yml
</argLine>
</configuration>
</plugin>
and spring-boot-maven-plugin:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>
-Dspring.config.additional-location=classpath:/config/business-config.yml
</jvmArguments>
</configuration>
</plugin>
But that doesn't work for me. I test by running my maven project locally from IDEA Intellij as a Spring Boot application. If I set this option in IDEA configuration then it works properly.
Any ideas why?

Maven is a build tool, not a runtime environment.
For the simplest see The java Command:
java ... -Dproperty=value ...
Sets a system property value. The property variable is a string with no spaces that represents the name of the property. The value variable is a string that represents the value of the property. If value is a string with spaces, then enclose it in quotation marks (for example -Dfoo="foo bar").
Otherwise the container your application is running in has to have a possibility to configure java command line parameters.

Related

Add vm options -Djava.util.Arrays.useLegacyMergeSort=true in pom.xml

I got this error while running a junit-test in my application. Which I later found out because of declared field size
java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:899)
at java.util.TimSort.mergeAt(TimSort.java:516)
at java.util.TimSort.mergeCollapse(TimSort.java:441)
...
org.mockito.internal.configuration.injection.PropertyAndSetterInjection.orderedInstanceFieldsFrom(PropertyAndSetterInjection.java:125)
I found that probable solution would be adding this flag.
-Djava.util.Arrays.useLegacyMergeSort=true in VM args. But I wanted to add in pom.xml
I referred to this how to add VM args using pom xml but it refers mostly for -X flags, what would be an appropriate placement in here?
I updated the pom.xml with the surefire plugin and used argLine parameter as suggested here
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>-Djava.util.Arrays.useLegacyMergeSort=true</argLine>
</configuration>
</plugin>
Trere are 2 ways to configure [System Properties in surefire plugin] (https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html)
Option 1: systemPropertyVariables
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<systemPropertyVariables>
<java.util.Arrays.useLegacyMergeSort>true</java.util.Arrays.useLegacyMergeSort>
</systemPropertyVariables>
</configuration>
</plugin>
Option 2: argLine
Some system properties must be set on the command line of the forked VM, and cannot be set after the VM has been started. These properties must be added to the argLine parameter of the Surefire plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>-Djava.util.Arrays.useLegacyMergeSort=true</argLine>
</configuration>
</plugin>
Upgrade your dependencies
On the other hand, I really think that the right solution is to upgrade Mockito to 2.1 or later
2.1 was released in October, 2016, and contains the fix to your issue: Make PropertyAndSetterInjection field sorting consistent #176

How to get argLine to work in maven surefire plugin

I'm having difficulty trying to add an argument to the jvm. It looks like using surefire is the only way to do this. My current code in the pom.xml is
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<argLine>-Xmx512m</argLine>
<systemPropertyVariables>
<all.clusters>${all.clusters}</all.clusters>
<branding.token>${brandingToken}</branding.token>
</systemPropertyVariables>
</configuration>
</plugin>
Maven is new to me so perhaps I'm missing something simple.
Running in a Netbeans 8.1 environment.
Turns out that what I needed to do was not to get surefire to run with special arguments, because that only covers tests. The trick to getting it working was the fact that this project is a NetBeans application which uses the
nbm-maven-plugin.
The following blog post describes how to modify the arguments.
Blogpost

get pom parameters in a maven plugin

I am developing a maven plugin. It is used at build time by other projects. This plugin uses the username and password in order to connect to an svn repository/server. This is part of this maven plugin's pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<useReleaseProfile>true</useReleaseProfile>
<releaseProfiles>complete</releaseProfiles>
<username>${username}</username>
<password>${password}</password>
</configuration>
</plugin>
I need to get the values of username and password dynamically when the plugin is running (In other words when other projects are being built). How can I do that?
Thanks!
Edit:
When other users build their project with this plugin, They don't pass their username/password through command line. (Like: -DuserName=userNameVal). But the plugin works fine and can connect to svn repository/server using their username and password. The problem is that I don't understand how this plugin get these values.
And one more thing: I need to get values in my java code. Something like this:
String username = getUserName();
Pass the values through commandline like below:-
mvn install "-DuserName=userNameValue"
Then in your pom.xml you can do like below:-
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<useReleaseProfile>true</useReleaseProfile>
<releaseProfiles>complete</releaseProfiles>
**<username>${userName}</username>**
<password>${password}</password>
</configuration>
</plugin>

Maven-Surefire-Plugin + systemPropertyVariables + Resource File = java.io.FileNotFoundException

I have a resource file located in folder src/test/java/resources/framework.properties with this content:
felix.auto.start.1=\
"file:${test.archive.directory}/agenda.test-1.0.0.jar"
And I use (or pretend to use) the maven-surefire-plugin to replace that variable (${test.archive.directory}) by the correct value see sample ballow:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${skipTests}</skipTests>
<systemPropertyVariables>
<test.archive.directory>${project.build.directory}</test.archive.directory>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
It just fails miserably and the test returns this message error:
java.io.FileNotFoundException: ${test.archive.directory}\testbundle.test-1.0.0.jar (The system cannot find the path specified))
Obviously the maven-surefire-plugin is not replacing the property value. I was digging all over internet and cannot find a decent answer to this, any idea of what am I doing wrong?
UPDATE
Consider also that I am running this code for an Arquillian OSGi test case. The parameters to replace above is for the Arquillian to locate the necessary bundles to run the tests so the replacement of the parameter must be made before the framework starts.
<test.archive.directory>${project.build.directory}</test.archive.directory>
is available as System property to the Java process it spans off for surefire, you can read them by
System.getProperty("test.archive.directory");
in your Java class, you can't expect it to magically replace it in properties file

How to set system properties using Netbeans 7.2 and Maven?

I'm running a unit test in NetBeans 7.2. using Maven.
How to set a system property?
I've tried adding the property using:
Project Properties > Run > JVM arguments
but it doesn't make a difference. I think it may have something to do with JUnit running in a different JVM or something?
Since the NetBeans integrates to the Maven quite well, It will use the maven configuration (POM) for handling the lifecycle, e.g. clean, build(install) and test. For example, when you right click at the project and select "Clean and Build", you may see the something like the following:
cd D:\temp\prj\netbeans\dummy;
JAVA_HOME=C:\\Java.Application\\Sun\\Java\\jdk1.6.0_31 "\"
C:\\Java.Application\\Sun\\NetBeans 7.1\\java\\maven\\bin\\mvn.bat\""
clean install
I'm using the maven-surefire-plugin for setting/passing the system properties as the following:-
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<encoding>UTF-8</encoding>
<systemProperties>
<property>
<name>DEF</name>
<value>456</value>
</property>
</systemProperties>
<argLine>-DABC=123</argLine>
</configuration>
</plugin>
</plugins>
</build>
You may see that there are 2 positions for passing the system properties as the following:
The systemProperties tag
The argLine tag
Regarding to the argLine tag, you can pass, not only the system properties, but also any further JVM arguments, e.g. -Xms, -Xmx as well.
You may see further information about the system properties here and the argLine here.

Categories