maven ignores sourcelevel in pom.xml - java

Okay, I'm pretty sure I'm missing a fundamental point here, but I'm stumped. I'm playing around with maven to get main and test sources to compile at different levels (1.7 & 1.8) - found a lot of posts but haven't gotten it working yet, so back to basics with an empty project.
So first, I want to try off by having maven not compile my code by setting the maven-compiler-plugin to source level 1.7 and using 1.8 code (stream).
This is my test code:
public static void main(String[] args) {
new ArrayList<>().stream();
}
And this is my pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
IntellIJ is not having any of it and complains about the source level as expected until I change it. Now, why is it that when running maven from the command line...
$ mvn compile
or
$ mvn install
Maven won't complain?
maven version 3.5.0
java version 1.8.0_131

There's nothing in the syntax that won't work in Java 7. However, the stream() method was only introduced in JDK 8. This code compiles because you happen to be using JDK 8. If you switch to an older JDK, your code will break.
One way to prevent such potential bugs is to use the Animal Sniffer Maven Plugin which allows you to check that you aren't using parts of the JDK that are too new, even when compiling against the aforementioned new JDK:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.15</version>
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java17</artifactId>
<version>1.0</version>
</signature>
</configuration>
<executions>
<execution>
<id>animal-sniffer</id>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>

Related

Cannot build any maven project with same error

It has been few weeks since I build anything in maven, and I just tried to build one of the projects and it gives me this error. I try with multiple project and they are throwing same error:
Execution default of goal org.codehaus.mojo:aspectj-maven-plugin:1.8:compile failed: Plugin org.codehaus.mojo:aspectj-maven-plugin:1.8 or one of its dependencies could not be resolved: Could not find artifact com.sun:tools:jar:15.0.2 at specified path /usr/local/Cellar/openjdk/15.0.2/libexec/openjdk.jdk/Contents/Home/../lib/tools.jar -> [Help 1]
How can I resolve this error? I can't even run mvn install -DskipTests or mvn package.
I am on JDK version 8.
Edit: here is my plugin from pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectj-maven-plugin.version}</version>
<configuration>
<complianceLevel>${java.version}</complianceLevel>
<source>${java.version}</source>
<target>${java.version}</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<aspectLibraries>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-aspects</artifactId>
</dependency>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
java version = 1.8
aspectj-maven-plugin.version = 1.8
EDIT: I pull this project from the repo, which means it works on other people's computer/on jenkins. I believe this is something to do with my local computer. It also gives me same error if I use terminal.
I found the answer after more research. As #Michael Katt mentioned in the comment, my JAVA_HOME wasn't properly set up. When I did which java, it gave me /usr/bin/java, but when I did echo $JAVA_HOME, it gave me nothing.
So with help of other colleague, I did vi ~/.bash_profile and added export JAVA_HOME=$(/usr/libexec/java_home). Not exactly sure why, but it is something to do with matching it with my jdk in my project structure which has /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk...
I opened new shell and typed echo $JAVA_HOME and it gives me /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home. My project build/compiles now.
I am still not sure how $(/usr/libexec/java_home) translated to /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk...

Unrecognized target bytecode. Compiling Java with Groovy

I saw couple of problems like this but none of the given advice helped me, so I add this.
I have Java app and I migrate it to Java 11. I can run it but when i try to mvn install it i get tge error:
Failed to execute goal
org.codehaus.gmavenplus:gmavenplus-plugin:1.6:compileTests (default)
on project osa-backend: Execution default of goal
org.codehaus.gmavenplus:gmavenplus-plugin:1.6:compileTests failed:
Unrecognized target bytecode.
This is my plugin:
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.16</version>
</dependency>
</dependencies>
</plugin>
Thanks for any suggestions cause I have no idea what can I change now.
This message is to warn you that your desired bytecode is not supported by the versions you currently have.
Targeting Java 11 bytecode requires Groovy 2.5.3/3.0.0-alpha-4 or newer and GMavenPlus 1.6.2 or newer.
In my case bumping the version of the maven-compiler-plugin from 3.8.1 to 3.10.1 solved the issue, every other approach suggested in similar threads failed :
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>${maven.compiler.release}</release>
</configuration>
</plugin>
where ${maven.compiler.release} = your java version

Compile errors with Maven but not in Eclipse after upgrade from java 1.7 to 1.8

I am aware that similar questions has been asked in the past, but I couldn't found my answer in it.
I am using Eclipse Luna and upgraded to java 8 (build 0.25).
The JAVA_HOME points to java 8 and also it's part of the Windows Classpath.
After the upgrade, maven compile shows some Ambiguous method calls that didn't show up in java 7.
The problem is that these errors aren't shown in Eclipse and I can't find why.
I removed all jre's in eclipse and removed/added the java 8 jdk, but still no luck.
Also Eclipse is using the java 8 environment for compiler errors/warnings.
With maven I use the "plexus-compiler-javac" compiler, snapshot 2-4. I also tried without the plexus compiler and with version 2.0, but no luck.
Any idea what this could be?
My Pom snippet:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-javac</artifactId>
<!--version>2.0</version-->
<version>2.4-SNAPSHOT</version>
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose>
<source>1.8</source>
<target>1.8</target>
<fork>false</fork>
<encoding>utf-8</encoding>
<meminitial>256m</meminitial>
<maxmem>1024m</maxmem>
</configuration>
</plugin>
Try telling maven to use java 8 source level by adding/changing configuration of your compile plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
i face this issue with tools.jar not found, so download the latest tools.jar & copy to D:\Java\jdk1.8.0_25..\lib\tools.jar, thanks.

set the default jdk for maven-compiler-plugin

I have just installed maven on an new ubuntu system, which includes the maven-compiler-plugin. I have a java project that was previously building fine, defaulting to a javac source and target of 5 (jdk 1.5). However, the project is now trying to compile using jdk1.3 on the new system. Is there an easy way to configure the system to use >=jdk5 ?
Here's some of the configuration details of the system:
$ java -version
java version "1.6.0_45"
$ dpkg -s maven
Package: maven
Status: install ok installed
Priority: optional
Section: java
Installed-Size: 1489
Maintainer: Ubuntu Developers <ubuntu-devel-discuss#lists.ubuntu.com>
Architecture: all
Version: 3.0.4-2
$ dpkg -s libmaven-compiler-plugin-java
Package: libmaven-compiler-plugin-java
Status: install ok installed
Priority: optional
Section: java
Installed-Size: 75
Maintainer: Ubuntu Developers <ubuntu-devel-discuss#lists.ubuntu.com>
Architecture: all
Source: maven-compiler-plugin
Version: 2.0.2-6
I've checked the maven-compiler-plugin-2.0.2.pom file, and plexus-compiler-javac.originalVersion and others are set to 1.5.3.
I know I can set this on a per-project basis by including a source/target tag in a plugin context, but I'd like to configure maven-compiler to default to jdk5 or higher without having to do this across a large number of projects.
how can i do this?
In your pom specify the following to set the compiler to JDK5:
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
</properties>
i.e.
<project>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
...
</project>
I specify mine prior to the dependencies, although so long as its part of the project element you should be able to place it anywhere inside the pom.
I ran into a similar issue with Maven previously, this fixed it for me. Essentially what this does is set the -source and -target flags to the value specified and passes it to the compiler. Newer plugins default to 1.5.
In order to use the default approach without specifying the properties, you will need to be running a later version of Maven.
I suppose you could also set up a template via your IDE to include this in all new pom files. Of course the actual implementation would depend upon your IDE...
See The apache maven compiler plugin documentation as well as Setting the source and compiler examples for more details.
I tried the maven-compiler-plugin approach and it proved cumbersome as there are plugins like maven-surefire-plugin and maven-cobertura-plugin which still fail due to incompatibility issues.
The better approach was to use maven-toolchain-plugin.
Step 1
Create /.m2/toolchains.xml
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>1.7</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>1.6</version>
<vendor>apple</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/1.6.0_65-b14-462.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
<!-- other toolchains -->
<!--
<toolchain>
<type>netbeans</type>
<provides>
<version>5.5</version>
</provides>
<configuration>
<installDir>/path/to/netbeans/5.5</installDir>
</configuration>
</toolchain>
-->
Step 2
Add maven-toolchain-plugin to plugins section in your project pom.xml.
*If using maven 3, ensure this goes into pluginManagement as well *
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>1.7</version>
<vendor>sun</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
Voila all your other plugins pick up the right JDK. Hope it helps. I spent almost half day on this exact issue today.
Default value for source and target was 1.3 in older versions of maven-compiler plugin (like 2.0.2-6).
Use at least a 3.0 version of the Maven compiler plugin to get this back to the original behaviour, or just configure that plugin to get source and target to appropriate values.
I used following settings to set maven default java compiler version.
first, modify maven settings.xml:
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
second, in eclipse preferences, make the java home point to jdk home
Suggestion: Use the latest maven compiler plugin.
In order to change the defaults, you should set source and target.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
More Info: maven-compiler-plugin
The simplest solution to such things is using an up-to-date version of Maven (3.1.1) and in particular create a parent pom.xml file for all your projects where you define the configuration and version of your maven-compiler-plugin via pluginManagement or better all of your plugins.
Based on Chinto's answer I got it running on my windows machine with this:
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>openjdk</vendor>
</provides>
<configuration>
<jdkHome>C:\path\to\openJDK1.8.0</jdkHome>
</configuration>
</toolchain>
</toolchains>

error in compiling project having annotations in maven2

I am using maven to compile some java classes. One of the classes has an annotation(#Override) so when i run mvn compile then I get error:
annotations are not supported in -source 1.3
(use -source 5 or higher to enable annotations)
#Override
annotations are not supported in -source 1.3
(use -source 5 or higher to enable annotations)
#Override
Although the java version in my system is jdk 1.6.29 still i can't understand this error.
So i there a way by which i can check the jdk version maven is using and perhaps change it.
Or, is there any other solution?
Thanks.
You need to instruct the maven build to set the compiler flags for compilation of java source level 1.5. This is done inside your pom.xml file, adding configuration for the maven-compiler-plugin plugin.
For instance:
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</execution>
</executions>
</plugin>
[...]
</plugins>

Categories