Setup Menus in Admin Panel

How to Create a Maven Project

4. How to Create a Maven Project

Two ways to create maven project

  • Command prompt
  • GUI

4.1 Create a Maven Java Project using Command Prompt

Creating a maven project is extremely easy using the maven archetype plugin. Information about archetypes can be found at http://maven.apache.org/guides/introduction/introduction-to-archetypes.html. We can create a maven project using the “archetype:create” goal in the following manner.

mvn archetype:generate -DgroupId=com.test-DartifactId=mytest

This command states to create a project called “mytest” specified by the artifactId. The project belongs to the “com.test” group, specified by the groupId. This group specifies essentially where the project is located hierarchically in a maven repository, and it allows for the grouping of similar projects into similar groups.

Now, I’ll run the “archetype:create” goal at the command prompt.

apachemaven14

This is the first time I’ve run the command, so maven will download the resources that it needs from the central repository to my local maven repository. When the goal finishes, we can see that the “mytest” project was successfully created.

apachemaven16

If we go to Windows Explorer, we can examine the project that we just created. I ran the goal in C:\mavenwrkspace\test, so the “mytest” project was created at C:\mavenwrkspace\test\mytest.

apachemaven17

Notice that mytest contains a set of directories. The src/main/java directory is the default Java source directory for maven projects. This is where you typically place your source code. This directory location is configurable, but in general it’s best to stay with the maven defaults unless you have a very compelling reason to change them. Notice that the com.test package structure was created within src/main/java, and that an App.java file was created in src/main/java/com/test. This is a simple HelloWorld-style class.

apachemaven18

The src/test/java directory is the default location for JUnit tests, which are integrated into the maven lifecycle. The com.test package structure was created within src/test/java, and a simple JUnit test class called AppTest.java was created in src/test/java/com/.

4.2 Create a web application project using maven

In a previous tutorial, we saw how we could use the maven archetype:create goal to create a basic maven project. We can also use the archetype:create goal to create a basic web application project by using with a “maven-archetype-webapp” archetype.

 mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.0 -DgroupId=com.webmaven -DartifactId=mywebmaven -Dversion=1.0-SNAPSHOT

The above maven command states to use the “archetype:create” goal to create a basic web application (war) project using the “maven-archetype-webapp” archetype. The project is called “mywebmaven”, it belongs to the group “com.webmaven”, and its version number is “1.0-SNAPSHOT”.

I opened a command prompt window and typed the above maven command.

apacheweb2

The execution of the maven command is shown here.

apacheweb1

After executing the command, we can see that the “mywebtest” project has been created by the webapp archetype. If we examine the project, we can see that a webapp directory has been created in src/main. The is the web context directory for the project, and we can see that it contains a WEB-INF directory, which holds a default web.xml file.

apacheweb3

The project’s pom.xml file is shown below. Notice that the packaging is set to “war”. This means that if a “mvn package” is performed, a war file will be built for the project.

“mywebmaven/pom.xml”

<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.webmaven</groupId>
<artifactId>mywebmaven</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>mywebmaven Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mywebmaven</finalName>
</build>
</project>

 

Import the project into eclipse and run the index.jsp file on server(url: http://localhost:8080/mywebmaven/index.jsp).View the Output.

apacheweb4

4.3 Create an Enterprise Project using Maven

Creating the directory structure of a J2EE project can be tedious. However, if you would use maven-archetype-j2ee-simple, the usual directories for a j2ee project would be provided for you.

 mvn archetype:generate -DgroupId=com.jeemaven -DartifactId=myjeemaven -DarchetypeArtifactId=maven-archetype-j2ee-simple

The execution of the maven command is shown here.

apacheweb5

This would then create a maven project.

apacheweb6

4.4 Create a Maven Java Project in Eclipse

When creating a project in Eclipse, one may use Maven to manage dependencies more easily and to resolve transitive dependencies automatically. Maven projects have a consistent structure for each project created, and it is possible to create this structure automatically within Eclipse.

To create Maven Project in Eclipse go to File->New ->Other

apacheweb7

Scroll to the Maven folder, open it, and choose Maven Project. Then choose Next.

apacheweb8

You may choose to Create a simple project or forgo this option. For the purposes of this tutorial, we will choose the simple project. This will create a basic, Maven-enabled Java project. Leave other options as is, and click Next.

apacheweb9

Now, you will need to enter information regarding the Maven Project you are creating.. In general, the Group Id should correspond to your organization name, and the Artifact Id should correspond to the project’s name. The version is up to your discretion as is the packing and other fields. If this is a stand-alone project that does not have parent dependencies, you may leave the Parent Project section as is. Fill out the appropriate information, and click Finish.

apacheweb10

You will now notice that your project has been created. You will place your Java code in /src/main/java, resources in /src/main/resources, and your testing code and resources in /src/test/java and /src/test/resources respectively.

apacheweb11

Open the pom.xml file to view the structure Maven has set up. You may also use the tabs at the bottom of the window to change to view Dependencies, the Dependency Hierarchy, the Effective POM, and the raw xml code for the pom file in the pom.xml tab.

apacheweb12

You now have a new Java project with Maven enabled.

4.5 Create Maven Web Project in Eclipse

This section explains how to create Dynamic Web Project having Maven enabled in Eclipse. This project can be used as base project and can be easily converted to most kind of advanced Java project like Spring MVC based etc.

Assumption:

You have setup Maven and Apache Tomcat Server successfully in your Eclipse Environment.

Create a simple maven web Project in Eclipse as explained in section 4.4.

apacheweb8

Now, you will need to enter information regarding the Maven Project you are creating.. , the Group Id Artifact Id.Select War from the packaging .Fill out the appropriate information, and click Finish.

apacheweb13

Right Click on Project name (DemoWebProject) select properties->select Project facets->check the Dynamic web Module. Click on furthur configuration available link.

apacheweb14

Check generate web.xml deployment descriptor.Click OK.

apacheweb15

In eclipse Maven web project structure appears as below.

apacheweb155

December 31, 2015

2 responses on "How to Create a Maven Project"

  1. Hi! I am trying to chgnae my local repository of maven, but I am using ant too, so I can’t find where to do it: in the fetch.xml (from ant) file, or in the settings.xml file. Of course i tried it changing the settings.xml, but it didn’t work, so now i am trying it in the fetch.xml and get-m2.xml of ant, but i haven’t got nothing yet any idea?????? thanx!!!!!

  2. Profile photo of admin

    Hey AVOL,

    Please check the Environment Variable of Java & Maven is correctly set or not,

    Set the JAVA_HOME environment variable to point to the base directory location where Java is installed on your machine. For example

    OS Output
    Windows Set the environment variable JAVA_HOME to C:\Program Files\Java\jdk1.7.0_75
    Linux export JAVA_HOME=/usr/local/java-current
    Mac export JAVA_HOME=/Library/Java/Home

    Extract the archive, to the directory you wish to install Maven 3.3.3. The subdirectory apache-maven-3.3.3 will be created from the archive.

    OS Location (can be different based on your installation)
    Windows C:\Program Files\Apache Software Foundation\apache-maven-3.3.3
    Linux /usr/local/apache-maven
    Mac /usr/local/apache-maven

    In case of wrong or not yet, you may face this kind of issue, please set it correctly and run the ant all again, after that you should get an console log downloading jar and dumping into new location of .m2 repo.

Leave a Reply to admin Cancel reply

Your email address will not be published. Required fields are marked *

About Us

Pradeep Academy is an online & Training institute To make learning easy, interesting, affordable and accessible to millions of learners across the Globe. To create an alternative learning platform, using a unique learning methodology of live online interactive courses along with 24x7 support. We aim to empower our customers with skills which will help them get an edge in their careers and improve their lives.

Contact Us

Address :

PradeepIT Consulting Services Pvt Ltd

205,Brigade gardens, No.19 Church Street , Bangalore – 560001, Karnataka, India

Email : [email protected]
Ph No: 08047363377
Web: https://academy.pradeepit.com
top
© Pradeep Academy.  Design & Developed by