I have a project which use a specific JRE : that contains some additionnal lib and custo java.security entries... and i want to run test in other machine which i don't have access to update JRE with my specific configuration.
What I want to do :
get JRE distribution and put it in target folder : using Maven dependency
with Maven plugin : update this JRE with my specific configuration
set $JAVA_HOME with my custom JRE in target using arquillian.xml configuration file
It is possible to do that with Arquillian ?
Have you any other suggestion ?
Thank you !
For the first both steps you can use the Maven dependency plugin.
For the integration test you can use the failsafe plugin, there is a parameter called jvm. There is no need to override the jvm with the arquillian.xml.
Option to specify the jvm (or path to the java executable) to use with the forking options. For the default, the jvm will be a new instance of the same VM as the one used to run Maven. JVM settings are not inherited from MAVEN_OPTS.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<jvm>path to jvm or java executable</jvm>
</configuration>
</plugin>
Related
System.out.println(Charset.defaultCharset().displayName()); prints ISO-8859-1, so I want to set the defaultCharset to UTF-8.
When tried with java -Dfile.encoding=utf-8 -jar XXX.jar, I checked that default charset is set to UTF-8.
But is there a way to set Dfile.encoding=utf-8 option at maven packaging level, so that I can just run java -jar XXX.jar?
What I tried:
mvn clean package -Dfile.encoding=utf-8
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>-Dfile.encoding=utf-8</argLine>
</configuration>
</plugin>
MAVEN_OPTS="-Dfile.encoding=utf-8" mvn clean package
I prefer controlling with pom.xml file.
You can define JDK_JAVA_OPTIONS environment variable as -Dfile.encoding=utf-8 to avoid typing it every time. See the java command documentation for details.
In OpenJDK 18 UTF-8 will be default for all operating systems: JEP 400.
The question is how to pass JVM args for Maven build before runtime.
Starting with Maven 3.3.1+ you can define JVM configuration via ${maven.projectBasedir}/.mvn/jvm.config file which means you can define the options for your build on a per project base. This file will become part of your project and will be checked in along with your project.
So, create .mvn/jvm.config in your project root with content:
-Dfile.encoding=utf-8
See the reference: https://maven.apache.org/configure.html#mvn-jvm-config-file
I have an Eclipse Mars 2 Java8, Maven 3.5.4 based workspace.
I build the project files with mvn eclipse:clean eclipse:eclipse, and watch the following maven output:
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-**1.7**
The default JRE for the workspace isn't Java 7, it is Java 8, like you can see looking at the following Eclipse config file:
<workspace>\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.launching.prefs.
that contains the following data:
org.eclipse.jdt.launching.PREF_VM_XML=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?>
<vmSettings defaultVM\="57,org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType13,1538040823497*">
<vmType id\="org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType">
<vm id\="1431694854640" name\="jdk1.7.0_40" path\="C\:\\Program Files\\Java\\jdk1.7.0_40"/>
<vm id\="1447417000092" name\="jdk1.6.0_45" path\="C\:\\Program Files\\Java\\jdk1.6.0_45"/>
<vm id\="1538040823497" name\="jdk1.8.0_65" path\="C\:\\Program Files\\Java\\jdk1.8.0_65"/>
</vmType>
</vmSettings>
As you may notice by looking at the above configuration, the default VM has the vm id "1538040823497", which is named jdk1.8.0_65, and resides in C\:\\Program Files\\Java\\jdk1.8.0_65.
This VM is correctly registered as Workspace Default in Eclipse Preferences, Java/Installed JREs, is marked as a "perfect match" within the Execution Environment JAVASE-1.8.
I cannot see anything why Maven Eclipse Plugin considers
org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7 as the correct choice, and not JAVASE-1.8.
I already deleted all JREs and registered them newly.
Now there's exactly one 1.6, 1.7 and 1.8 Environment, each with a "perfect match", and 1.8 is checked as default (as you can see above).
I have to manually correct the project each time I generated it (Edit Build Path, change JRE library from 7 to 8), since all files using Java 8 features like streams or lambda functions signal compiler errors unless I assign the correct JRE manually.
I already tried and manipulate org.eclipse.jdt.launching.prefs, redefine all JREs, pray, curse or ask an Ouija board, to no avail, always Java 7 is assigned by the eclipse plugin (version 2.10, by the way).
Any ideas, anyone?
In your pom file, try to use this:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
You may also reference this link:
https://dzone.com/articles/maven-eclipse-and-java-9
Or if all fails, try eclipse oxygen, a newer version (not the newest release but much better than Mars IMO) that has Maven Integration plugin pre-installed, all you need is to add a m2e-connector plugin.
Two different maven Java projects (let's call them ProjectA and ProjectB) depend on the same library (also a maven Java project, all three projects are self-written). ProjectA needs the library to be compiled for Java 1.7, ProjectB for 1.8. So far I had the following configuration in my library's pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Installing the library like this then allows me to build ProjectB but not ProjectA, as and are both set to 1.8. I can change those two fields to 1.7 to allow ProjectA to be built but not B.
I want to do proper version control, automated builds, etc. and hence can't always change the lib's source/target version before building a project. Maintaining two library release branches, so solving the issue via the version control system, is also annoying and does not seem elegant (that's my current approach which I'd like to change).
If I replace the version with a variable (e.g. ${jdk.version}) and use the command line flag -Djdk.version=1.7 when installing the lib ProjectA will, for some reason, still not build, claiming the lib is targeted at 1.8. So apparently this is not equivalent to manually putting the 1.7 into the pom.xml file.
How can one solve this problem elegantly? Why will maven/Java not properly target the library at 1.7 if I try to set the corresponding property via the command line?
How can I find which Java options (Xmx, Xms, Xss, etc) are being used by Maven?
I've found out that a way to set them is via the environment MAVEN_OPTS. Now I want a way to be assured it's getting the right settings.
EDIT: I believe it´s different to this question as I don´t want to see the value of an environment variable. I rather want to see which settings are actually being used by Maven, whether it comes from a env var, settings.xml or other means.
EDIT2: I'm also interested in other ways of setting Java options for a Maven build
You can set Java options for Maven in different points and level (globally or via plugins configuration):
Plugin configuration: just for Compilation
Using the Maven Compiler Plugin configuration for compiling application code and test code, you can set the required Xmx, Xms, Xss options via the compileArgs configuration entry, available for both compile and testCompile goals. An official example is available here and on other SO answers like this one.
An example is also shown below on the third point.
Plugin configuration: just for Tests execution
Using the Maven Surefire Plugin configuration for tests executions, you can set the required Java options to be used at runtime via the argLine configuration entry of the test goal. An official example is available here.
An example is also shown below on the third point.
Plugin configuration: via Properties (and profiles)
You can combine the two options above (in case of common Java options) as a property value to pass to both compileArgs and argLine configuration entry or have different properties per configuration (according to your needs).
<property>
<jvm.options>-Xmx256M</jvm.options>
</property>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<compilerArgs>
<arg>${jvm.options}</arg>
</compilerArgs>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>${jvm.options}</argLine>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
Using properties gives you also an two extra advantages (on top of centralization): you can use profiles then to personalize it based on different desired behaviours (and example in this SO answer) and you can override them via command line as well, like:
mvn clean install -Djvm.options=-Xmx512
Global/Project configuration: Options file
Since Maven 3.3.1, you can specify your options in a .mvn/jvm.config file per project. It's an alternative to MAVEN_OPTS and a more narrowed scope (per project). However, since it sis a new Maven feature, people should be aware of it, otherwise troubleshooting and maintenance may be impacted.
Global/Env configuration: MAVEN_OPTS
Maven well-known environment variable to set global execution options, however applied to all Maven builds sharing that environment (i.e. per logged user).
When running Maven using the -X option (debug enabled), you will have the following output as part of your build:
[DEBUG] properties used {java.vendor=Oracle Corporation, ... , env.MAVEN_OPTS=-Xmx256M, ...
Update
After all, the executed mvn command is an OS script. Having a look at it in Windows, I found the possibility of using the MAVEN_BATCH_ECHO option which, if enabled (value set to on), will echo any command executed by the script and as such also the invocation of the java command, where you can see if your options (the MAVEN_OPTS) are picked up correctly together with the full list of parameters passed to it.
Here is an execution I tested on Windows:
set MAVEN_BATCH_ECHO=on
set MAVEN_OPTS=-Xmx256M
mvn compile > output.txt
NOTE: the output.txt will contain quite a lot of text, providing build output and additional echos executions. As part of it, it provided:
>"path_to_\jdk1.7\bin\java.exe" -Xmx256M -classpath "path_to\apache-maven-3.1.1\bin\..\boot\plexus-classworlds-2.5.1.jar" "-Dclassworlds.conf=path_to\apache-maven-3.1.1\bin\..\bin\m2.conf" "-Dmaven.home=path_to\apache-maven-3.1.1\bin\.." org.codehaus.plexus.classworlds.launcher.Launcher compile
As you can see, the -Xmx256M option was picked up correctly. If the maven script for other OS doesn't provide this option, then you can simply add it to the script (a simple echo before the java execution, showing the command, would also be enough).
You can find the maven script in your maven installation folder, under the bin subfolder.
Update2
Furthermore, since Maven is a Java tool after all and as you can see from its script it invokes the java command, you could see all the available options as suggested in this SO answer by slightly changing the Maven script and use the jinfo command which should really give you the answer according to this other SO answer.
Maybe it helps to start Maven with verbose debug output (-debug, I think?). Otherwise just do a ps aux | grep java and check the arguments of the process (assuming *nix).
After starting Eclipse, Mven seems to set the compiler settings to 1.5 and forget all the other global code style settings to ensure a higher code quality.
Is there some way to disable this feature? Or can I specify all compiler and code style checks in my POM?
It is very annoying because Ecplise can't run the app because of not allowed override annotations for interfaces. The tick in Java compiler -> Enable project specific settings is always set after a restart.
You can set the compiler source and target (byte-code) versions in your pom.
See http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
Code style checks can be configured in the pom as part of the maven reports, see http://maven.apache.org/plugins/maven-checkstyle-plugin/
but I'm not sure whether the integration will pick these up.
The simplest way is to add to your POM
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
See default maven compiler setting for another solutions.
If you don't want the m2e eclipse plugin actively messing with your project settings, use the maven-eclipse-plugin's eclipse goal to generate your eclipse settings.
It'll generate your eclipse settings based off of what you have in your pom, so you'll still need to set the maven compiler settings in your pom if you don't want to set them every time you regenerate your eclipse project files when you update your pom.
If you take a look at the detailed configuration for that plugin, there are instructions for how to generate various pieces of eclipse metadata.