Create new maven project with eclipse


To create new maven project on eclipse:

  1.  Click File –> New–>Other and choose Maven project
  2. Check the checkbox beside Create simple project like this, then click next

  3. In the group id field enter your company group id, for example: “com.smartapi”
  4. in the artifact id field, enter your project name , for example “first-demo”
  5. In packaging type, if you are building desktop or api application, keep it “jar”, if you are building web application, change it to “war”, then click finish

Then new project will be shown like this:

By default, ,maven uses JDK 5 as the standard JDK version for your project, to force it to use specific version, for example 1.7, open pom.xml file by double clicking on it, then click on the pom.xml tab like this:

, and the add the build section as follows:

 <build>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>3.5.1</version>
 <configuration>
 <!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
 <source>1.8</source>
 <target>1.8</target>
 </configuration>
 </plugin>
 </plugins>
 </build>


Now, you have to inform the eclipse IDE to change the JDK configuration by clicking right click  on the project–> Maven–> Update Project then click OK

Web Applications:

Note: In case of web application, you have to set the packaging type to war, and add the following section to pom.xml to inform maven that the web.xml configuration file is optional and avoid errors:

 <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.jk.examples.web</groupId>
 <artifactId>web-test</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>

 <build>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>3.5.1</version>
 <configuration>
 <!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
 <source>1.8</source>
 <target>1.8</target>
 </configuration>
 </plugin>

 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-war-plugin</artifactId>
 <version>2.6</version>
 <configuration>
 <!-- web.xml is not mandatory since JavaEE 5 -->
 <failOnMissingWebXml>false</failOnMissingWebXml>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>