Eclipse - Maven project changing source compliance - java

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

Related

Maven project java unable to import dependencies

I am unable to import the dependencies as shown in the screenshots. What am i doing wrong here. Please help thank you.
Above your <dependencies> section, add the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
And then right click on your project -> Maven -> Reimport. This will reconfigure the compiler for language level (instead of default 1.5 which is much too low), and more importantly import the libraries in your dependencies.
try
right click the project
1.configure-->convert to maven project
2.Maven--->update maven project-->force update of snapshots and releases

Maven update resets Java version

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>

project compile error release 8 requires target 1.8 release

I have an error Error:java: javacTask: source release 8 requires target release 1.8. I use 1.8 java, language level set to 8. The only thing I guess could be the reason is the line in the pom.xml file
<configuration>
<executable>${java.home}/bin/java</executable>
<commandlineArgs>${runfx.args}</commandlineArgs>
</configuration>
here the line
<commandlineArgs>${runfx.args}</commandlineArgs>
is red and what to do with this I can't manage to get.
To set the compiler version in Maven, you would have something that looks like this in your pom.xml file:
<build>
<pluginManagement>
<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>
</pluginManagement>
</build>
3.3 is the latest Maven Compiler Plugin version. I assumed it was needed for Java 8 support.
This is the same as setting the -source and -target compiler options.

Maven javacTask: source release 1.7 requires target release 1.7

When I try building my Java project using Maven, I'm getting the following error:
javacTask: source release 1.7 requires target release 1.7
In my pom file, I have defined source and target for 1.7.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
However, when using the maven plugin in Eclipse, I do not run into this error and my code builds without any errors. I'm thinking this is due to something particular to Maven itself on my local machine. Any ideas what could be the root cause of this?

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.

Categories