Maven Compiler plugin -source is different than -target parameter - maven

I have the following in the mail pom.xml file:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.7</target>
</configuration>
Is it correct? Please tell me whether what are the actual usages of -source and -target tags?
Also, my requirement says "For the maven plugin, just make sure is it compiled in Java 7".

source and target parameters are propagated from maven compiler plugin to javac compiler from SDK.
Having source less than target is questionable. As #khmarbaise mentioned in comment, this means your source code is compiled JDK6 but you require JVM for Java 1.7.
However it makes sense to have target lesser than source. For example
<source>1.8</source>
<target>1.6</target>
should compile JDK 8 code and produce bytecode compatible with Java 1.6+ JVMs. Although I have never tried it - behaviour may even differentiate between different Java language implementations (OpenJDK, OracleJDK,...)

Related

Confusion on maven compiler plugin

I am quite confused about maven compiler plugin and what it does. I have a project that has several modules. In my top pom.xml I have a section
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
My understanding is this specifies the JDK compiler version used to compile the code, and this section get inherited by all the modules. What I don't get is in my IntelliJ IDEA I can still specify project JDK in the settings and it seems like that setting overrides this. When I run maven install in the IDE I can confirm that it is using javac from JDK 8 to compile. So what does this section do exactly?
You are correct; maven-compiler settings should be inherited by child modules.
I don't know about IntelliJ, but I can tell you that Eclipse picks&chooses whatever it wants from maven config, and for all the rest it uses its own settings.
Therefore, I'd expect IntelliJ may do something similar?
The simplest way to test this is to run a mvn clean install via command line, and see which "wins". If you get artifact compiled with 1.8 then it means you're missing something in Maven config which causes those settings not to propagate to children. If you get artifact compiled with 1.7 then it is IntelliJ who does it and not maven-compiler-plugin.

Does maven-release-plugin also do what maven-compiler-plugin does?

In project pom.xml is it required to include both maven-release-plugin and maven-compiler-plugin? The reason I want the release plugin is because I want to release the project at the end of a sprint/release. I understand that the compiler plugin will compile the source code, however I am not sure if the release plugin will also compile the source code, apart from updating the pom and checking out/in to scm.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
Any help with this is much appreciated.
If unspecified, Maven will know what to do when it needs to compile the code. However, in order to be able to control things like source and target versions, you will need to have a declaration of the maven-compiler-plugin.
It is generally good practice, to explicitly define the plugins you are using, along with their configurations and above all their versions, so that you have a guarantee of what was working.
What the Maven Release Plugin does is described here.
Your question is about the bullet "Run the project tests against the modified POMs to confirm everything is in working order". There is not that much magic here. What happens is that the maven-release-plugin will start a new Maven session. It will execute "mvn verify" in the same folder where you ran "mvn release:prepare".
All the steps done by the maven-release-plugin could also be done by hand, but that's asking for mistakes.

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 Compiler Plugin Fork Option Not Working

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.

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