Maven update resets Java version - java

Everytime I modify the dependencies inside the pom.xml file in my IntelliJ Maven project, the Java version is set to 1.5 and I have to receonfigure my project.
The following settings are modified by Maven:
Settings | Compiler | Java Compiler -> Target bytecode version
And Project Settings | Modules -> Language Level
Why is this happening and what do I have to do, so that maven doesn't vandalise my settings?

You have to explicitely set the java version in your pom file so that version 1.5 doesn't get set by default.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>

Specify the java version under properties.
<properties>
<java-version>1.8</java-version>
</properties>
and to use the same version with maven compile or package then include the same version in compile plug-in
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

Related

after upgrading to java 12, in which syntax and for what to use for surefire plugin and maven

i upgraded my java version to 12.0.2 in my system, and i have few projects where i have used maven 3.6.2, and some i used maven + surefire plugin 3.0.0-M8.
since i know that maven is working with java 1.6 and i need to update the compiler version to work with these, also i need to update the surefire plugin to work with the java 12 version,
so i want to understand in what to use, and on what (Maven or surefire or both) and also if the syntax is the right one?
also if these is not the right way, so what is the right one?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable> ${JAVA_HOME}/bin/javac </executable>
</configuration>
</plugin>
also
<properties>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
and also
<properties>
<maven.compiler.release>12</maven.compiler.release>
</properties>
and also
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>12</release>
</configuration>
</plugin>
</plugins>
</build>

old jdk profile in maven build

I have one maven project which is have maven compiler plugin to specify the target java version as 1.8. I want it to be able to built on jdk 1.6 as well as it was a dependency for other java 1.6 based project. How to configure maven pom file for this requirement?
Referring to the maven documentation, include this into your POM:
<project>
[...]
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
[...]
</project>
or configure the plugin directly (also inside your POM):
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
And do not forget to install JDK 1.6!

Eclipse - Maven project changing source compliance

I have a mid-size project in java eclipse, in which all dependencies were being manually imported into the build path. For the sake of sanity, I wanted to convert it to a maven project.
The problem is: I had the source level/jre defined for 1.8 and when i converted to a maven project, It switched to the default of 1.5.
I tried some answers around SO for forcing the source compliance in maven to 1.8 but it didn't work.
I added to the
<build>
<plugins>
<plugin>
<groupId>[project group id]</groupId>
<artifactId>[project artifact id]</artifactId>
<version>0.0.1-SNAPSHOT</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Is anything missing here?
EDIT1: Forget me, I'm bad. The correct version is:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
after this you need these two
preference - > java - > compiler - > 1.8 and language level to 8
preference - > java -> installed JRE -> JDK 1.8

Set default java compliance level for Maven projects in Eclipse

The default compliance level for Maven is 1.5 and every time that I update a maven project in Eclipse it sets back the compliance level from 1.6 to 1.5 which is really annoying for me.
I know that I can set the target to 1.6 in the POM file but the problem is that I cannot set this in the parent POM and expect the children to inherit it. So I have to do it for every single Maven module. How can I set it in my Maven project or in the whole eclipse once for a "lifetime" without modifying every single Maven module!?
I know that I can set the target to 1.6 in pom file but the problem is that I cannot set this in the parent pom and expect the children to inherit it.
Setting the <source> and <target> version in the parent pom does work.
For example, in my parent pom, I have:
<pluginManagement>
<plugins>
    <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
If you are having problems, you might want to check that:
the child specifies the correct version of the parent;
the parent pom specifies both source and target values;
you have run mvn install on the parent; and
mvn help:effective-pom on the child project shows the expected source/target values.
After modifying the poms, you may need to select both projects and use Maven->Update Project.
Another way (if you do not have already compiler plugin defined) is to set just these properties in your pom file:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Details: https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
This worked for me..
<build>
<finalName>ProjectName</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
Go to the project's pom.xml and insert below in between tags. Then the eclipse plugin (like m2eclipse) should rebuild the workspace.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
Note: this is project-level as only the project's pom.xml is affected.

Maven gives a java version error (ask for java 1.5) even when I have java 1.7 installed

I'm getting an error saying
(use -source 5 or higher to enable annotations) {class path} error:
for-each loops are not supported in -source 1.3
when I try to compile a module using maven.
The thing is that the java version in my machine is 1.7.0_02
Can anyone suggest a solution?
You may want to include this in your pom.xml file as I had the same problem:
<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>
Please, check the value of the JAVA_HOME environment variable.
For the user acc which is used by maven
The shortest version is to set maven.compiler.source and maven.compiler.target properties in your pom.xml:
<project>
....
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<compilerArgument></compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
by blackpanther
this other too
<project>
....
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
</properties>
by rzymek
all together
and clean and build the proyect and that's work fine vatos!!!
<build>
<finalName>your project name</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>

Categories