10 - Maven IDE integration

10. IDE integration

          10.1 Eclipse integration

                       10.1.1 Create a Maven project

                       10.1.2 Import a Maven project

                       10.1.3 Update a Maven project

                       10.1.4 Link project to settings.xml

           10.2 Netbeans integration

           10..3 IntellijIdea integration

An integrated development environment (IDE) is a programming environment packaged as an application and consists of a code editor, a compiler, a debugger and a graphical user interface (GUI) builder.

IDEs are widely used by developers. Maven is a command-line tool but many of its commands can be executed by the IDEs.

The most popular IDEs for developers are Eclipse, Netbeans and JetBrains Intellij IDEA. They integrate successfully with Maven, Maven projects and extend Maven.

From an IDE Maven project the developer can switch easily to command line because this type of project is independent of the IDE.

 

10.1 Eclipse integration

Eclipse is designed for enterprise and open source development. It contains M2Eclipse plugin developed by Sonatype (if Eclipse does not contain the M2Eclipse plugin it has to be downloaded or added from the menu Help > Eclipse Marketplace).

One of the most important features of Maven is the sound integration with Eclipse. Maven IDE is a Maven focused distribution of Eclipse composed of Eclipse distribution, M2Eclipse and some Maven support integrations.

Some m2ecplise features:

  • run Maven goals form Eclipse
  • output of Maven commands can be viewed in the Eclipse console
  • launch fast of the builds from Eclipse
  • dependency management build path
  • solving of the dependencies
  • automatic downloads of the dependencies
  • easy search of the dependencies in remote Maven repositories.

 

10.1.1 Create a Maven project

In order to create a new Maven project with Eclipse some steps must be followed:

1. In Eclipse select File > New > Other to arrive to the creation wizard window.

           

2. Select Maven > Maven Project and click on the Next button.

 

           

3. In the next window there is an option Create a simple project check this option if the project is Maven – enabled Java project. For an advanced setup this option can remain unchecked. Click Next.

           

 

4. The next window will contain information about the project. The Maven Coordinates (groupId, artifactId, version, packaging) should be filled up.

           

A new project is has been created with /src/main/java and /src/main/resources where to place the code and /src/test/java and /src/test/resources for placing the tests.

            

The pom.xml file contains the Maven structure. In the view Mode there are some tabs to change: Dependencies, Dependency Hierarchy, Effective POM and pom.xml with the code for this file.

           

If we want to create a multi module project then when creating the parent project the packaging type must be set to “pom” for the parent module.

           

Then we follow the same steps as before for creating a new module:

           

 

Choose the name for the new module:

           

 

We choose the packaging type for the new module:

           

The new structure has now a new structure:

           

10.1.2 Import a Maven project

The projects created with Maven (from command line or also from Eclipse – for example downloaded projects from CVS) can be imported in Eclipse by following the next steps:

1. Select File > Import > Option:

We choose a project created before form command line:

After the importing is ready Eclipse will create classpath files:

 

In Build Path we can see that the Maven dependencies have been added:

From the window Run Configuration we can start the build with clean install command:

The result in the Console is:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building somecompany-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ somecompany-app ---
[INFO] Deleting C:\my maven project\somecompany-app\target
[INFO] 
....................
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ somecompany-app ---
[INFO] Building jar: C:\my maven project\somecompany-app\target\somecompany-app-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ somecompany-app ---
[INFO] Installing C:\my maven project\somecompany-app\target\somecompany-app-1.0-SNAPSHOT.jar to C:\temp\com\somecompany\app\somecompany-app\1.0-SNAPSHOT\somecompany-app-1.0-SNAPSHOT.jar
[INFO] Installing C:\my maven project\somecompany-app\pom.xml to C:\temp\com\somecompany\app\somecompany-app\1.0-SNAPSHOT\somecompany-app-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 51.404 s
[INFO] Finished at: 2014-10-21T06:28:00+03:00
[INFO] Final Memory: 10M/24M
[INFO] ------------------------------------------------------------------------

 

The new project can be run as Java Application :

The final result:

 

10.1.3 Update a Maven project

A Maven project can be updated by running the goal eclipse:eclipse or with the option from Eclipse Maven > Update Project.

            

This command synchronizes Eclipse project settings with pom.xml settings and generates Maven Dependencies class path container. All the changes that are necessary in a project must be done through pom.xml not through Eclipse project settings.

A Maven project can be also updated with the command

mvn eclipse:clean eclipse:eclipse

This command can be executed also from Eclipse from the window Run Configuration:

           

[INFO] ------------------------------------------------------------------------
[INFO] Building somecompany-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-eclipse-plugin:2.9:clean (default-cli) @ somecompany-app ---
……..
[INFO] Deleting file: .project
[INFO] Deleting file: .classpath
[INFO] Deleting file: .wtpmodules
[INFO] 
[INFO] >>>maven-eclipse-plugin:2.9:eclipse (default-cli) @ somecompany-app >>>
[INFO] 
[INFO] <<<maven-eclipse-plugin:2.9:eclipse (default-cli) @ somecompany-app <<<
[INFO] 
[INFO] --- maven-eclipse-plugin:2.9:eclipse (default-cli) @ somecompany-app ---
[INFO] Using Eclipse Workspace: null
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "somecompany-app" to C:\my maven project\somecompany-app.
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.554 s
[INFO] Finished at: 2014-10-21T22:49:08+02:00
[INFO] Final Memory: 8M/19M
[INFO] ------------------------------------------------------------------------

 

The command generates .project and .classpath files and setting/org.eclipse.jdt.core.prefs with project specific compiler settings.

 

10.1.4 Link project to settings.xml

If the file settings.xml is modified often during the development Eclipse provides a link to this file.

We can made a link from Eclipse to some external files and then control them from Navigator menu.

For example:

 

In Navigator there can be seen a new file added and an arrow drawn on it suggesting that is a link to an external file.

10.2 Netbeans integration

Netbeans is an open source platform-framework and IDE for Java development or other technologies. It includes Maven support since the version 6.7.

The features of NetBeans are similar to features of Eclipse.

  • Maven goals
  • Output console
  • Update Maven dependencies
  • Launch Maven Builds
  • Handles Dependency management automatically
  • Maven dependencies and sources solved and downloaded from remote Maven repositories.

 

If we want to create Maven – based NetBeans module the first steps are the same like in Eclipse, we open the Maven category and choose Maven Project.

 

In case that we want to use a project already created from command line we can choose Open Project Menu.

We can open a maven project from File Menu > Open Project:

 

We can see the project Libraries and Test Libraries. Netbeans has added the dependencies to the classpath:

The project can be built by right-clicking on the project and then select Clean and Build as option.

The output of the build will be displayed in the Netbeans console:

cd C:\netbeans-maven\somecompany-app; "JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_25" cmd /c "\"\"C:\\Program Files\\NetBeans 8.0.1\\java\\maven\\bin\\mvn.bat\" -Dmaven.ext.class.path=\"C:\\Program Files\\NetBeans 8.0.1\\java\\maven-nblib\\netbeans-eventspy.jar\" clean install\""
Scanning for projects...
------------------------------------------------------------------------
Building somecompany-app 1.0-SNAPSHOT
------------------------------------------------------------------------
...............
File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
Compiling 1 source file to C:\netbeans-maven\somecompany-app\target\classes
--- maven-resources-plugin:2.5:testResources (default-testResources) @ somecompany-app ---
[debug] execute contextualize
Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
skip non existing resourceDirectory C:\netbeans-maven\somecompany-app\src\test\resources
--- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ somecompany-app ---
File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
Compiling 1 source file to C:\netbeans-maven\somecompany-app\target\test-classes
……………………..
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.somecompany.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
………………………………………………….
Installing C:\netbeans-maven\somecompany-app\target\somecompany-app-1.0-SNAPSHOT.jar to C:\Users\vtg9234\.m2\repository\com\somecompany\app\somecompany-app\1.0-SNAPSHOT\somecompany-app-1.0-SNAPSHOT.jar
Installing C:\netbeans-maven\somecompany-app\pom.xml to C:\Users\vtg9234\.m2\repository\com\somecompany\app\somecompany-app\1.0-SNAPSHOT\somecompany-app-1.0-SNAPSHOT.pom
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 10.742s
Finished at: Tue Oct 21 23:43:59 EEST 2014
Final Memory: 15M/37M
------------------------------------------------------------------------

To run the project, right-click on the class App.java and then select Run File option.

The result will be displayed in Netbeans Console.

Scanning for projects...
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/1.2.1/exec-maven-plugin-1.2.1.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/1.2.1/exec-maven-plugin-1.2.1.pom (8 KB at 12.3 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/1.2.1/exec-maven-plugin-1.2.1.jar
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/1.2.1/exec-maven-plugin-1.2.1.jar (37 KB at 148.3 KB/sec)
------------------------------------------------------------------------
Building somecompany-app 1.0-SNAPSHOT
------------------------------------------------------------------------
--- exec-maven-plugin:1.2.1:exec (default-cli) @ somecompany-app ---
Downloading: http://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.1/commons-exec-1.1.pom

Downloaded: http://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.1/commons-exec-1.1.pom (11 KB at 70.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.1/commons-exec-1.1.jar

Downloaded: http://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.1/commons-exec-1.1.jar (52 KB at 235.4 KB/sec)
Hello World!
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 2.246s
Finished at: Tue Oct 21 23:51:24 EEST 2014
Final Memory: 5M/15M
------------------------------------------------------------------------

10.3 IntellijIdea integration

We can create a new project in IntellijIdea importing the previous Maven project created with command line.

The features of Intellij IDEA are similar to features of Netbeans ( see 9.2 )

From Intellij IDEA Import Project >Select File or Directory to Import

Select Import project from external Model > Maven.

Select project location:

Select Maven project to import:

Configure IntellijIdea java jdk and sdk.

The project structure can be seen in IntellijIDEA:

 

The project has to be built. Then right-click on App.java and select Run App.main()

 

The result in the console of Intellij IDEA is :

Like us on Facebook