Cannot change Maven version in Eclipse IDE - java

After failing to mvn package inside of Eclipse but succeeding in the terminal, I realized that Eclipse was running Maven 3.3.3 while terminal was running 3.3.9. I attempted, and by all apparent accounts succeeded, to reconfigure eclipse by going to Eclipse -> Preferences -> Maven -> Installations and pointing to my 3.3.9 folder. I restarted Eclipse and tried again but to no available. Also, changes to the pom.xml file are not acknowledged when attempting to build. I've inserted inappropriate slashes and other characters and am only able to generate this error:
Error assembling WAR: web.xml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
I modified my pom file as per this recommendation but as I said, maven is not recognizing changes to the pom and I cannot get running mvn --version to produce 3.3.9 as it should. I've attached my pom.xml below
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javawebtutor</groupId>
<artifactId>LoginWebApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>LoginWebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
<build>
<finalName>LoginWebApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>src\main\webapp\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>

Try setting up a runtime configuration, or see if you're using one already.
In eclipse, right click on the project->run as->run configurations. Look for the m2 Maven Build section on the left. If there is one under your project's name, then edit it, otherwise, create one. On the bottom of the dialog there is a drop down that allows you to select which maven configuration you want to run:
This gives me the following output:
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11 10T11:41:47-05:00)
Maven home: C:\Program Files\Java\apache-maven-3.3.9
Java version: 1.7.0_80, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_80\jre
Default locale: en_US, platform encoding: UTF-8
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "windows"

Call cmd, type systempropertiesadvanced Check environment variables:
%MAVEN_HOME%, %PATH%.
Check these steps:
Look at the below screenshot (this is relate to your problem):
EMBEDDED: 3.3.3
outsite Eclipse: 3.3.9
(source: http://codingvn.com/2016/02/08/cau-hinh-de-eclipse-su-dung-ban-cai-dat-maven-ben-ngoai-ide/ this is my website)

Normally you have to reinstall the m2eclipse plugin to run with the correct version. If it was updated, it won't work with the previous one, as it will take the "old" maven version.

Related

Build failing due to version only via terminal

I have a simple maven project which builds fine and runs in Intellij. The Java version is set to version 8 under relevant settings in Intellij.
Under the maven POM file, I have set the following to point to the java version.
<modelVersion>4.0.0</modelVersion>
<properties>
<java.version>1.8</java.version>
<javac.src.version>1.8</javac.src.version>
<javac.target.version>1.8</javac.target.version>
</properties>
<groupId>org.example</groupId>
<artifactId>exa</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
.....
My maven version and info as follows:
Maven home: C:\Users\name\apache-maven-3.6.3\bin\..
Java version: 1.8.0_231, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_231\jre
As mentioned, no issues when run via Intellij. But when I run maven commands via the intellij terminal or normal terminal, I get following error.
Example Maven commands.
mvn clean package
or
mvn verify
Error:
COMPILATION ERROR
/C:/Users/name/projectname/src/main/java/pack/Main.java:[17,51] method references are not supported in -source 1.5
I don't even have java sdk 1.5 on my machine. Can I get some help on where I should change this version to 8 so I can make a maven build please? Thanks.
In your project's pom.xml, it seems the source is configured to java 1.5. Make sure the correct source version is configured in pom.xml.
If you are using maven-compiler-plugin, change the version as below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
or
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

Configure Maven to allow a range of JREs

I am teaching a 2nd semester Java course, and I am trying to set up a simple Maven project that will will run in any student's environment (Windows/Mac/Linux, IntelliJ/VS Code/Eclipse, any version of Java 8 or above) without complaining. Because this is only a second semester programming course, I want pom.xml to be as simple as possible. The project need only be a couple of Java source files and a couple of test files.
The current pom.xml is below. It works, but Visual Studio Code on MacOS with Java 8 generates the following warnings:
Build path specifies execution environment J2SE-1.5. There are no
JREs installed in the workspace that are strictly compatible with
this environment.
The compiler compliance specified is 1.5 but a JRE
1.8 is used
I can fix the problem by adding these lines:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
But, won't that cause warnings or errors for students with different Java versions? Since we aren't doing anything fancy, I don't want to require that they all use Java 12 or 13. Any version 8 or above is fine.
I also hesitate to tell the students to edit the properties above to match their machines because I assume that will cause problems if I pull their code and try to run it on my machine.
Is there a way to configure Maven and the IDE to simply be happy with any JDK version 8 and above?
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>gvsucis</groupId>
<artifactId>ArrayLab</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>ArrayLab</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
You need to distinguish between the JDK and the Java version you build for.
Setting maven.compiler.source and maven.compiler.target only sets the target Java version. If you set it to 1.8, then you can build with any JDK that supports this, which are JDK 8 and above.
Please change your java home to use jdk not jre.
if you are using a jdk8+, then your pom configuration should work.
Typically all IDE refer the JAVA_HOME environment variable to find the java binary. So, Set your JAVA_HOME to jdk8+.
if it is widows machine restart is required otherwise just run source .bashrc or source .bach_profile

Change JDK in Maven Projects (Eclipse)

New Maven projects created in Eclipse (on Windows) use the default JRE System Library "J2SE-1.5".
Default System Library in Maven Project
The project was created the following way:
New Project
Maven -> Maven Project
Archtype: maven-archetype-quickstart
I want to use the JDK 1.8.0, which is my default JDK for non Maven projects.
My JAVA_HOME variable is set to
C:\Program Files\Java\jdk1.8.0_152
I tried switching the system library via Right Click on the System Library and set the path to my JDK 1.8.0. But after a Maven update it resets back to the 1.5.
Other Stackoverflow questions just suggest to set the JAVA_HOME, but that did not work for me.
Specify JDK for Maven to use
I looked for some POM.xml commands to set the library, but couldn't find anything. My POM looks pretty standard right now:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Test123</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test123</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
What do I need to change to get the JDK 1.8 as my default Library?
Set the JDK in the pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
in eclipse do a 'Maven->Update Project'
Open your Eclipse, click on
Windows -> Preferences -> Java -> Installed JREs
Verify that the checked JRE refers to the JDK you want. Otherwise, select the checked JRE and click Edit, and change the path to the JDK home.
The following steps did the job for me on Windows:
Set JAVA_HOME to the new JDK (necessary for Maven)
Add the new JDK as runtime environment in Eclipse (Window/Preferences/Installed JRE’s) and set the new JDK as default
Go to Window/Preferences/Installed JRE’s/Execution Environments and select the JavaSE version of the new JDK by clicking it. Select the new JDK as the default for that JavaSE version by filling the checkbox.
Run Maven/Update Project for your project

Java/Maven confusion 'Unsupported major.minor version 51.0' [duplicate]

This question already has answers here:
Why Maven uses JDK 1.6 but my java -version is 1.7
(9 answers)
Closed 8 years ago.
I am trying to download and install this Maven plugin:
https://github.com/mirkonasato/graphipedia
I cd in the directory and run
mvn clean install
I installed mvn today with homebrew on a Mac so it should be up to date. The error I'm getting is:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project graphipedia-dataimport: Compilation failure
[ERROR] error: Exception thrown while constructing Processor object: org/neo4j/kernel/impl/annotations/ServiceProcessor : Unsupported major.minor version 51.0
I googled this error and found a couple of people saying, the reason might be an old Java Version. So I checked in my System Preferences and it says Java 7.
But I didn't really trust that and ran
mvn --version
Which gave me this output
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T22:58:10+02:00)
Maven home: /usr/local/Cellar/maven/3.2.3/libexec
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: de_DE, platform encoding: MacRoman
OS name: "mac os x", version: "10.9.5", arch: "x86_64", family: "mac"
I am confused. Why is Java v. 1.6 here? And how do I fix this? I just want to use this nice java app.
Oh, and the pom.xml from the Graphipedia App looks like this. Thought it might be helpful as well
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.graphipedia</groupId>
<artifactId>graphipedia-parent</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Graphipedia Parent</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<neo4j.version>2.0.0</neo4j.version>
</properties>
<modules>
<module>graphipedia-dataimport</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>${neo4j.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-lucene-index</artifactId>
<version>${neo4j.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-graph-algo</artifactId>
<version>${neo4j.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<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>
</configuration>
</plugin>
</plugins>
</build>
</project>
This error means you are trying to execute java bytecode with a java runtime that is older than the java installation the code was compiled with. If you're using eclipse, check window | preferences | java | compiler and make sure the version isn't newer than 1.6.
Or you can upgrade the installation of java that maven is trying to use to run it.

Maven: Attaching multiple artifacts

I have a maven project that uses some custom jars [not found in any repository]. To add them along with the Maven build I am using the attach-artifact goal in maven. Following is my pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maximus</groupId>
<artifactId>adminbuild</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>adminbuild</name>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<outputDirectory>target</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${basedir}/../../resources/dependencies/java/customjar1.jar</file>
<type>jar</type>
</artifact>
<artifact>
<file>${basedir}/../../resources/dependencies/java/customjar2.jar</file>
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
My projects that use these jars [customjar1.jar, customjar2.jar] have their dependency on the above pom file [adminbuild].
When I execute the mvn clean install command, I get the following error:
[ERROR] Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:1.7:attach-artifact (attach-artifacts) on project adminbuild: The artifact with same type and classifier: jar:null is used more than once. -> [Help 1]
Following is the output of mvn -version command:
Apache Maven 3.0.3 (r1075438; 2011-02-28 23:01:09+0530)
Maven home: C:\maven
Java version: 1.6.0_26, vendor: Sun Microsystems Inc.
Java home: C:\Java\jdk1.6.0_26\jre
Default locale: en_IN, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
It seems that the way I am attaching the artifacts is incorrect. Should I not attach multiple artifacts in the same pom file? If yes then how. Please help.
Attached artifacts are normally used to install additional files created by your build, like the classes jar of a webapp, documentation or sources.
To add files to your maven repository so they are available as dependencies you should use the install-file goal.
Edit: Attached artifacts are identified by the same groupId and artifactId as your main project but with a different classifier. In your configuration, you did not specify the classifier, hence the error message.
You would want to have these custom jars in a repository manager like nexus so that they can be downloaded/used like a normal dependency jar.
Assuming that is not possible and seeing that these jars are in well-known location, perhaps you could specify these custom jars with system scope in the projects that need them?
<project>
...
<dependencies>
<dependency>
<groupId>my-group</groupId>
<artifactId>customjar1</artifactId>
<version>a.b.c</version>
<scope>system</scope>
<systemPath>${basedir}/../../resources/dependencies/java/customjar1.jar</systemPath>
</dependency>
</dependencies>
...
</project>
I used a secondary project to do this along with a multi-module.
https://stackoverflow.com/q/7908872/242042

Categories

Resources