Maven Compiler Plugin Fork Option Not Working - windows

I have a maven pom file with the following compiler-plugin:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<executable>${path_to_JDK6}</executable>
<fork>true</fork>
<compilerVerison>1.6</compilerVerison>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
My system's JAVA_HOME is set to a 1.5 JDK. When I run mvn package, maven throws the following error message:
Failure executing javac, but could not parse the error:
javac: invalid target release: 1.6
Usage: javac <options> <source files>
Does anyone have any ideas about why Maven is using the 1.5 JDK instead of forking to the 1.6 executable? Is there any debugging option that I could use? Are the fork and executable options broken in Maven 2?
Note: My system admin would not allow me to change the value of JAVA_HOME and some of my new libraries are written for java 6. So I would like to find a workaround to make maven compile with JDK 1.6.

My ${path_to_JDK6} variable only included the path to the folder containing the JDK. It did not include the /bin/javac. When I added /bin/javac, the 1.6 compiler was invoked.

Related

Maven does not find Java compiler despite having set JAVA_HOME

I've set
stefan#stefan:~$ echo $JAVA_HOME
/usr/lib/jvm/java-8-openjdk-amd64
(I've also tried /usr/lib/jvm/default-java)
but if I run mvn clean install on my project I see
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.0:compile (default-compile) on project shared: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
The respective entry in the pom.xml for the maven-compiler-plugin is set to Java 8:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
I don't know what I am missing out here. How can I tell Maven where the compiler is?
Please try following,
On console, run java -version and see if it works fine.
Run javac -version and see if it works
If any of the above does not work, it means
Your installation is not correct. Run sudo apt-get install openjdk-8-jdk again.
There is a broken previous java installation. Step 1 should again solve this problem.
Set JAVA_HOME to JDK instead of JRE.
You need to add JDK_PATH/bin to the PATH.

Sonarqube-Analysis on Jenkins fails due to PMD Plugin throwing UnsupportedClassVersionError: Unsupported major.minor version 52.0

Java-Version Configuration
Java-Version used for compiling classes with ant: 1.8
pom.xml für Sonarqube-Analysis defines:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
and
<!-- Set the sonar.java.source property to tell PMD which version of Java your source code complies to. The default value is 1.5. -->
<sonar.java.source>1.8</sonar.java.source>
Sonarqube-Version: 4.5.4
Sonarqube-PMD-Plugin-Version: 2.4.1
JDK configured in Jenkins-Job: 1.8
Sonarqube-Server runs using Java 8
There is only Java8 installed on both Jenkins- and Sonarqube-Server.
Jenkins executes the following maven-comand to start the analysis:
mvn.bat -f "D:\Jenkins\workspace\DVLP MW2.0 Coverage und Codeanalyse\sonar-pom.xml" -e -B sonar:sonar -Dsonar.jdbc.driver=com.mysql.jdbc.Driver "-Dsonar.jdbc.url=jdbc:mysql://192.168.58.121:3306/sonar?autoReconnect=true&useUnicode=true&characterEncoding=utf8" -Dsonar.host.url=http://192.168.58.121/sonar
What are we missing?
You should remove property sonar.java.source. SonarQube gets Java Class Version from maven-compiler-plugin configuration. It is not a problem if you compile with JDK7 and run analysis with JDK8 (or do both actions with JDK7/JDK8).

Maven/Jenkins java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0

I have a Jenkins server having JDK & JRE 6 and 7 installed together.
All of the projects are built on 1.6 except one which is 1.7 dependent.
I've configured the maven pom file to use the Java compiler from the JAVA_HOME_7 environment PATH.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
**<executable>${env.JAVA_HOME_7}/bin/javac</executable>**
<fork>true</fork>
<verbose>false</verbose>
</configuration>
</plugin>
During mvn install I'm getting the following error:
java.lang.RuntimeException: There was an error in the forked process
java.lang.UnsupportedClassVersionError: : Unsupported major.minor version 51.0
which I think means that the server is using JRE 1.6.
How to keep the JRE 1.6 together with 1.7 in order to keep the compatibility with the old 1.6 projects and the new 1.7 one?
Many Thanks,
Atanas
You will need to run surefire tests with java 7 too. By default surefire will use same jvm as that running maven - Java6 in your case.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
...
<jvm>${env.JAVA_HOME_7}/bin/java</jvm>
</configuration>
</plugin>
</plugins>
I remember I also struggled to this problem. Follow below steps to resolve the problem
Cause: when multiple JRE is installed then multiple java.exe is also installed to many location of system.
Solution: Modify your environment PATH variable and change the order of java.exe. put location of java.exe on first position like below code
PATH = C:\Program Files\Java\jdk1.6.0\; other;other;other
Change above location according to your use and installation location.
I have encountered this problem more than once, it is because you have more than one versions of jdk(jre) on your system, so just set the JAVA_HOME to the proper jdk you compile your project with and the running would be fine.
Have a look at your target/lib directory, you might have two versions of same jar. For me it was creating the pro

Maven : error: generics are not supported in -source 1.3 , I am using 1.6

I have imported an existing Maven project into Eclipse IDE .
I have modified some code in it , it compiled successfully ,
I am using Java 1.6 as compiler
and when i am trying to run maven clean install -X
Its giving the following error
could not parse error message: (use -source 5 or higher to enable generics)
D:\bayer\util\src\main\java\com\tata\bayer\util\BrokerageCalendar.java:179: error: generics are not supported in -source 1.3
private static Hashtable<String, Boolean> nyseHolidays = new Hashtable<String, Boolean>();
^
could not parse error message: (use -source 5 or higher to enable generics)
D:\bayer\util\src\main\java\com\tata\bayer\util\APIHttpXmlClient.java:27: error: generics are not supported in -source 1.3
Class<? extends APIResponse> responseClass) {
^
Please suggest any ideas as how to resolve this ??
Did you declare that you want to use java 1.6 in your project pom.xml?:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument></compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Configuring the Maven Compiler Plugin will fix the problem. It turns out the problem was caused by the Maven3 package in the Ubuntu repository. An alternate fix is to download Maven 3 from the Apache website which uses a more up to date Compiler plugin.
I wanted to know why this was happening when the documentation states the default Java source is 1.5. To see what mvn is using for your compiler plugin use:
mvn help:effective-pom
My Maven Compiler Plugin was 2.0.2 even though I was using Maven 3.0.4 from the Ubuntu packages.
When I run the same command using Maven 3.0.4 from Apache I have a plugin version 2.3.2, which defaults to Java 1.5 as expected.
You have to configure the Maven Compiler Plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Another way that doesn't involve modifying the pom is to specify the source and target in the command line:
mvn clean install -Dmaven.compiler.source=1.6 -Dmaven.compiler.target=1.6
Note that this should be avoided generally as the build cannot be guaranteed to be repeatable this way.
I'd prefer this:
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
...

maven prevent denpendency compile

I have a custom jar which including java sources; Maven tries to compile when it builds. How do I skip source compile in the jar file? I have tried such as exclude with some pattern in the compiler-plug in and source directory define but I have not get any luck. Thanks!
C05
This is a normal behavior of javac that searches the whole classpath for source files to compile unless the -sourcepath option is given (and this would be the solution here).
Unfortunately, there is a Jira issue about -sourcepath not being passed to javac by the Maven Compiler Plugin (see MCOMPILER-98). But there is a workaround:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<sourcepath>${project.basedir}/src/main/java</sourcepath>
</compilerArguments>
</configuration>
</plugin>

Resources