In the tutorial “Setting up Eclipse IDE step by step tutorial” we looked at installing Eclipse IDE, importing an existing Java project folder into eclipse, and creating a Java project within Eclipse. Industrial strength Java applications will be using Eclipse with Maven.
Configure Eclipse to work with Maven
We have already have a modified “settings.xml” file in “c:\tools\apache-maven-3.5.0-bin\apache-maven-3.5.0\conf” as per “Setting up Maven step by step tutorial” – “Configure Maven” section.
Step 1: Let’s configure Eclipse to point to this file via “Window –> Preferences“, and in the pop up console select “Maven –> User Settings“.
Import an existing Maven project into Eclipse
Let’s import the Maven project that we created in “Create a Maven project” at “Setting up Maven step by step tutorial“.
Step 1: “File –> Import“, and in the pop up console select “Maven –> Existing Maven Projects“.
Select the previously create Maven project, which has a “pom.xml” file in its base folder.
Check the Maven folder structure & the file contents.
Note: Java projects have a little “J” symbol on top of it whereas a Maven based Java projects have both “M” & “J”.
Step 2: You can run the “HelloWorld.java” by right mouse clicking, and then “Run As” –> “Java Application.”
Step 3: You can build the “jar” file within Eclipse by selecting the project “simple-maven-project“, and right mouse click to select “Run As” –> “Maven Install“.
Create a new Maven Project In Eclipse
Step 1: File –> New Project…“, and then in the pop up console select “Maven –> Maven Project“.
Step 2: Select “C\projects” as the location.
Step 3: Select “Quickstart” as the Maven archetype.
Step 4: Assign a Maven “group id” & “artifact id”.
You have a basic Maven project structure to which you can add packages, classes, interfaces, etc. Also, can modify the “pom.xml” file to add more library dependencies & plugins as you see fit. You are now ready to “Create a new Maven Project outside Eclipse & then import into Eclipse
Step 1: From a DOS prompt run:
1 |
c:\projects>mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=myproject2 -DinteractiveMode=false |
Step 2: Import “C:\projects\myproject2” as an existing Maven project into Eclipse as shown above.
You are now ready to “Work on the project” within Eclipse.
Upgrade to Java 8 or Later
As you could notice both the Maven projects were Using J2SE 1.5.
Step 1: Select the project “myproject2” within Eclipse and the right mouse click, select “Properties“, which brings up the project properties. Select the “Libraries” tab, and click on “Add Library“.
Step 2: Remove the “J2SE-1.5” library.
Step 3: You must also add the following two lines to the pom.xml file to use Java 8.
1 2 3 4 5 |
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> |
The complete “pom.xml” will look like with 4.12 JUnit library:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<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.mytutorial</groupId> <artifactId>myproject2</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>myproject2</name> <url>http://maven.apache.org</url> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project> |
You can check for the library versions at “http://repo1.maven.apache.org/maven2/” by clicking on the group id “junit”, and then the artifact id “junit”, and finally the version “4.12”. You can see the jar file “junit-4.12.jar“.
Next Java Beginner Tutorial: Setting up GitHub with Eclipse for Maven based Java projects.