error in compiling project having annotations in maven2 - java

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>

Related

Can Scala 2.11.12 compiler on JDK11 produce bytecode for target jvm11?

Have a project that uses java 8 and scala 2.11.12 and depends on spark 2.4.2.
I need to compile and run this project using jdk11 (Java version: 11.0.12, vendor: Eclipse Foundation)
My scala-maven-plugin config:
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.0.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
<goal>add-source</goal>
</goals>
</execution>
</executions>
<configuration>
<args>
<arg>-target:jvm-1.8</arg>
</args>
</configuration>
</plugin>
If I compile and run it without any change just using jdk11 I get bytecode version 52. Almost everything is fine and works. Just need to override xbean-asm6-shaded library with new version - 4.10 because old version (bundled with a spark) somehow find bytecode 55 and throw an exception.
When I change target to -target:jvm-1.11 or -target:jvm-11 I get the error:
[ERROR] 'jvm-1.11' is not a valid choice for '-target'
[ERROR] bad option: '-target:jvm-1.11'
So there is the question:
Can scala compiler 2.11.12 or 2.12.14 produce jdk11 target?

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

maven ignores sourcelevel in pom.xml

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>

Intellij Java version

Can somebody tell me what are all the places where I have to change the java version, because I have changed it in more than 5 places so far (I don't even remember all of them) and it is still saying "diamond operator is not supported in -source 1.5". The most stupid ide ever..
Project -> Project SDK
Project -> Project language level
Modules -> Language level
Project bytecode version
Target bytecode version
If you use Maven in IntelliJ IDEA to build your project you have to set Java version as parameters source and target in configuration for maven-compiler-plugin:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Default version for this is 1.5 and IDE use it to check source code compatibility.

Maven builds project with Java 1.6 even if 1.5 is specified [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java version mismatch in Maven
I have a project which is configured to use Java 1.5 with maven:
mvn help:effective-pom
...
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
...
My machine has Java 1.6 installed, and Maven is using it:
mvn -v
...
Apache Maven 2.2.1 (r801777; 2009-08-06 20:16:01+0100)
Java version: 1.6.0_21
I have imported the project into Eclipse (using mvn eclipse:eclipse), and it is building it with Java 1.5, as specified in the POM.
Now if I mark an interface implementation method with #Override, which is disallowed in Java 1.5 but supported in Java 1.6, then Maven will build the project without errors, but Eclipse will mark the annotation as a compile-time error and refuse to build the project.
How can I either get Maven to really compile the project with Java 1.5, or get Eclipse to compile the project using the same settings as Maven?
It seems that newer versions of the JDK 1.5 ignore the #Override if it is in an interface implementation method: https://stackoverflow.com/a/2336850/256245 I guess that JDK 1.6 does the same if it is compiling using target 1.5. That´s the reason Maven does not complain about it.
To check if Maven is really compiling to Java 1.5, you should use Animal Sniffer Plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>check-java15-sun</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java15-sun</artifactId>
<version>1.0</version>
</signature>
</configuration>
</execution>
</executions>
</plugin>
Since Eclipse have its own compiler implementation, I guess you would have to remove the #Override annotation from interface implementation methods to make him happy. The removal will not do any harm since the annotation was being ignored by the JDK.

Categories