Unit 5
Maven :
What is Maven
Maven Tutorial
Maven tutorial provides basic and advanced concepts of apache maven technology. Our
maven tutorial is developed for beginners and professionals.
Maven is a powerful project management tool that is based on POM (project object
model). It is used for projects build, dependency and documentation.
It simplifies the build process like ANT. But it is too much advanced than ANT.
Current version of Maven is 3.
Understanding the problem without Maven
There are many problems that we face during the project development. They are
discussed below:
1) Adding set of Jars in each project: In case of struts, spring, hibernate
frameworks, we need to add set of jar files in each project. It must include all the
dependencies of jars also.
2) Creating the right project structure: We must create the right project structure in
servlet, struts etc, otherwise it will not be executed.
3) Building and Deploying the project: We must have to build and deploy the project
so that it may work.
What it does?
Maven simplifies the above mentioned problems. It does mainly following tasks.
1. It makes a project easy to build
2. It provides uniform build process (maven project can be shared by all the maven
projects)
3. It provides project information (log document, cross referenced sources, mailing
list, dependency list, unit test reports etc.)
4. It is easy to migrate for new features of Maven
Apache Maven helps to manage
o Builds
o Documentation
o Reporing
o SCMs
o Releases
o Distribution
What is Build Tool
A build tool takes care of everything for building a process. It does following:
o Generates source code (if auto-generated code is used)
o Generates documentation from source code
o Compiles source code
o Packages compiled code into JAR of ZIP file
o Installs the packaged code in local repository, server repository, or central
repository
o Difference between Ant and
Maven
o Ant and Maven both are build tools provided by Apache. The main purpose of
these technologies is to ease the build process of a project.
o There are many differences between ant and maven that are given below:
Ant Maven
Ant doesn't has formal Maven has a convention to place source
conventions, so we need to provide code, compiled code etc. So we don't
information of the project structure in need to provide information about the
build.xml file. project structure in pom.xml file.
Ant is procedural, you need to Maven is declarative, everything you
provide information about what to do define in the pom.xml file.
and when to do through code. You
need to provide order.
There is no life cycle in Ant. There is life cycle in Maven.
It is a tool box. It is a framework.
It is mainly a build tool. It is mainly a project management
tool.
The ant scripts are not reusable. The maven plugins are reusable.
It is less preferred than Maven. It is more preferred than Ant.
How to install Maven on windows
You can download and install maven on windows, linux and MAC OS platforms. Here, we
are going to learn how to install maven on windows OS.
To install maven on windows, you need to perform following steps:
1. Download maven and extract it
2. Add JAVA_HOME and MAVEN_HOME in environment variable
3. Add maven path in environment variable
4. Verify Maven
1) Download Maven
To install maven on windows, you need to download apache maven first.
Download Maven latest Maven software from Download latest version of Maven
For example: apache-maven-3.1.1-bin.zip
Extract it. Now it will look like this:
2) Add MAVEN_HOME in environment
variable
Right click on MyComputer -> properties -> Advanced System Settings -
> Environment variables -> click new button
Now add MAVEN_HOME in variable name and path of maven in variable value. It must
be the home directory of maven i.e. outer directory of bin. For example: E:\apache-
maven-3.1.1 .It is displayed below:
Now click on OK button.
3) Add Maven Path in environment variable
Click on new tab if path is not set, then set the path of maven. If it is set, edit the path
and append the path of maven.
Here, we have installed JDK and its path is set by default, so we are going to append the
path of maven.
The path of maven should be %maven home%/bin. For example, E:\apache-
maven-3.1.1\bin .
4)Verify maven
To verify whether maven is installed or not, open the command prompt and write:
1. mvn −version
Now it will display the version of maven and jdk including the maven home and java
home.
Let's see the output:
Let's see the output:
Maven Repository
A maven repository is a directory of packaged JAR file with pom.xml file. Maven
searches for dependencies in the repositories. There are 3 types of maven repository:
1. Local Repository
2. Central Repository
3. Remote Repository
Maven searches for the dependencies in the following order:
Local repository then Central repository then Remote repository.
If dependency is not found in these repositories, maven stops processing and throws an
error.
1) Maven Local Repository
Maven local repository is located in your local system. It is created by the maven when
you run any maven command.
By default, maven local repository is %USER_HOME%/.m2 directory. For
example: C:\Users\SSS IT\.m2.
Update location of Local Repository
We can change the location of maven local repository by changing the settings.xml file.
It is located in MAVEN_HOME/conf/settings.xml, for example: E:\apache-maven-
3.1.1\conf\settings.xml.
Let's see the default code of settings.xml file.
settings.xml
1. ...
2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.
apache.org/xsd/settings-1.0.0.xsd">
5. <!-- localRepository
6. | The path to the local repository maven will use to store artifacts.
7. |
8. | Default: ${user.home}/.m2/repository
9. <localRepository>/path/to/local/repo</localRepository>
10. -->
11.
12. ...
13. </settings>
Now change the path to local repository. After changing the path of local repository, it
will look like this:
settings.xml
1. ...
2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.
apache.org/xsd/settings-1.0.0.xsd">
5. <localRepository>e:/mavenlocalrepository</localRepository>
6.
7. ...
8. </settings>
As you can see, now the path of local repository is e:/mavenlocalrepository.
2) Maven Central Repository
Maven central repository is located on the web. It has been created by the apache
maven community itself.
The path of central repository is: http://repo1.maven.org/maven2/.
The central repository contains a lot of common libraries that can be viewed by this
url http://search.maven.org/#browse.
3) Maven Remote Repository
Maven remote repository is located on the web. Most of libraries can be missing from
the central repository such as JBoss library etc, so we need to define remote repository
in pom.xml file.
Let's see the code to add the jUnit library in pom.xml file.
pom.xml
1. <project xmlns="http://maven.apache.org/POM/4.0.0"
2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
5.
6. <modelVersion>4.0.0</modelVersion>
7.
8. <groupId>com.javatpoint.application1</groupId>
9. <artifactId>my-application1</artifactId>
10. <version>1.0</version>
11. <packaging>jar</packaging>
12.
13. <name>Maven Quick Start Archetype</name>
14. <url>http://maven.apache.org</url>
15.
16. <dependencies>
17. <dependency>
18. <groupId>junit</groupId>
19. <artifactId>junit</artifactId>
20. <version>4.8.2</version>
21. <scope>test</scope>
22. </dependency>
23. </dependencies>
24.
25. </project>
You can search any repository from Maven official website mvnrepository.com.
Maven pom.xml file
POM is an acronym for Project Object Model. The pom.xml file contains information of
project and configuration information for the maven to build the project such as
dependencies, build directory, source directory, test source directory, plugin, goals etc.
Maven reads the pom.xml file, then executes the goal.
Before maven 2, it was named as project.xml file. But, since maven 2 (also in maven 3),
it is renamed as pom.xml.
Elements of maven pom.xml file
For creating the simple pom.xml file, you need to have following elements:
Element Description
project It is the root element of pom.xml file.
modelVersio It is the sub element of project. It specifies the modelVersion. It should be set
n
groupId It is the sub element of project. It specifies the id for the project group.
artifactId It is the sub element of project. It specifies the id for the artifact (project). An a
produced or used by a project. Examples of artifacts produced by Maven for a p
binary distributions, and WARs.
version It is the sub element of project. It specifies the version of the artifact under giv
File: 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javatpoint.application1</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
Maven pom.xml file with additional
elements
Here, we are going to add other elements in pom.xml file such as:
Element Description
packaging defines packaging type such as jar, war etc.
name defines name of the maven project.
url defines url of the project.
dependencies defines dependencies for this project.
dependency defines a dependency. It is used inside dependencies.
scope defines scope for this maven project. It can be compile, provided, runtime,
File: 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javatpoint.application1</groupId>
<artifactId>my-application1</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Maven Example
We can create a simple maven example by executing
the archetype:generate command of mvn tool.
To create a simple java project using maven, you need to open command prompt and
run the archetype:generate command of mvn tool.
Syntax
The syntax to generate the project architecture is given below:
1. mvn archetype:generate -DgroupId=groupid -DartifactId=artifactid
2. -DarchetypeArtifactId=maven-archetype-quickstart -
DinteractiveMode=booleanValue
Example
The example to generate the project architecture is given below:
1. mvn archetype:generate -DgroupId=com.javatpoint -DartifactId=CubeGenerator
2. -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Note: Here, we are using maven-archetype-quickstart to create simple maven core
project. if you use maven-archetype-webapp, it will generate a simple maven web
application.
Output
Now it will generate following code in the command prompt:
1. mvn archetype:generate -DgroupId=com.javatpoint -DartifactId=Cub
2. eGenerator -DarchetypeArtifactId=maven-archetype-quickstart -
DinteractiveMode=fa
3. lse
4. [INFO] Scanning for projects...
5. [INFO]
6. [INFO] ------------------------------------------------------------------------
7. [INFO] Building Maven Stub Project (No POM) 1
8. [INFO] ------------------------------------------------------------------------
9. [INFO]
10. [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-
pom >>
11. >
12. [INFO]
13. [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalon
e-pom <<
14. <
15. [INFO]
16. [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom
--
17. -
18. [INFO] Generating project in Batch mode
19. Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mav
20. en-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
21. Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mave
22. n-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (5 KB at 3.5 KB/s
e
23. c)
24. Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mav
25. en-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom
26. Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mave
27. n-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 0.9 K
B/s
28. ec)
29. [INFO] -------------------------------------------------------------------------
30. ---
31. [INFO] Using following parameters for creating project from Old (1.x) Archetype:
32. maven-archetype-quickstart:1.0
33. [INFO] -------------------------------------------------------------------------
34. ---
35. [INFO] Parameter: groupId, Value: com.javatpoint
36. [INFO] Parameter: packageName, Value: com.javatpoint
37. [INFO] Parameter: package, Value: com.javatpoint
38. [INFO] Parameter: artifactId, Value: CubeGenerator
39. [INFO] Parameter: basedir, Value: C:\Users\SSS IT
40. [INFO] Parameter: version, Value: 1.0-SNAPSHOT
41. [INFO] project created from Old (1.x) Archetype in dir: C:\Users\SSS IT\CubeGen
e
42. rator
43. [INFO] ------------------------------------------------------------------------
44. [INFO] BUILD SUCCESS
45. [INFO] ------------------------------------------------------------------------
46. [INFO] Total time: 10.913s
47. [INFO] Finished at: Thu Dec 26 16:45:18 IST 2013
48. [INFO] Final Memory: 9M/25M
49. [INFO] ------------------------------------------------------------------------
50. 'cmd' is not recognized as an internal or external command,
51. operable program or batch file.
Generated Directory Structure
Now go to the current directory from where you have executed the mvn command. For
example: C:\Users\SSS IT\CubeGenerator. You will see that a simple java project is
created that has the following directory:
1. CubeGenerator
2. -src
3. --main
4. ---java
5. ----com
6. -----javatpoint
7. ------App.java
8. --test
9. ---java
10. ----com
11. -----javatpoint
12. ------AppTest.java
13. -pom.xml
As you can see, there are created 3 files pom.xml, App.java and AppTest.java. Let's
have a quick look at these files:
1) Automatically Generated pom.xml file
1. <project xmlns="http://maven.apache.org/POM/4.0.0"
2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4. http://maven.apache.org/maven-v4_0_0.xsd">
5.
6. <modelVersion>4.0.0</modelVersion>
7. <groupId>com.javatpoint</groupId>
8. <artifactId>CubeGenerator</artifactId>
9. <packaging>jar</packaging>
10. <version>1.0-SNAPSHOT</version>
11. <name>CubeGenerator</name>
12. <url>http://maven.apache.org</url>
13. <dependencies>
14. <dependency>
15. <groupId>junit</groupId>
16. <artifactId>junit</artifactId>
17. <version>3.8.1</version>
18. <scope>test</scope>
19. </dependency>
20. </dependencies>
21. </project>
2) Automatically Generated App.java file
1. package com.javatpoint;
2. /**
3. * Hello world!
4. *
5. */
6. public class App
7. {
8. public static void main( String[] args )
9. {
10. System.out.println( "Hello World!" );
11. }
12. }
3) Automatically Generated AppTest.java file
1. package com.javatpoint;
2.
3. import junit.framework.Test;
4. import junit.framework.TestCase;
5. import junit.framework.TestSuite;
6. /**
7. * Unit test for simple App.
8. */
9. public class AppTest
10. extends TestCase
11. {
12. /**
13. * Create the test case
14. *
15. * @param testName name of the test case
16. */
17. public AppTest( String testName )
18. {
19. super( testName );
20. }
21. /**
22. * @return the suite of tests being tested
23. */
24. public static Test suite()
25. {
26. return new TestSuite( AppTest.class );
27. }
28. /**
29. * Rigourous Test :-)
30. */
31. public void testApp()
32. {
33. assertTrue( true );
34. }
35. }
Compile the Maven Java Project
To compile the project, go to the project directory,
for example: C:\Users\SSS IT\CubeGenerator and write the following command on
the command prompt:
1. mvn clean compile
Now, you will see a lot of execution on the command prompt. If you check your project
directory, target directory is created that contains the class files.
Run the Maven Java Project
To run the project, go to the project directory\target\classes,
for example: C:\Users\SSS IT\CubeGenerator\target\classes and write the
following command on the command prompt:
1. java com.javatpoint.App
Now, you will see the output on the command prompt:
Output of the maven example
1. Hello World!
How to build the maven project or how to
package maven project?
The mvn package command completes the build life cycle of the maven project such
as:
1. validate
2. compile
3. test
4. package
5. integration-test
6. verify
7. install
8. deploy
Visit this link to know more about build life
cycle http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
You need to execute the following command on the command prompt to package the
maven project:
1. mvn package
Now you will see that a jar file is created inside the project/target directory.
You can also run the maven project by the jar file. To do so, go to the maven project
directory, for example: C:\Users\SSS IT\CubeGenerator and execute the following
command on the cmd:
1. java -classpath target\CubeGenerator-1.0-SNAPSHOT.jar;.; com.javatpoint.App
Now you will see the following output:
1. Hello World!
Maven Web Application
We can create a simple maven web application example by executing
the archetype:generate command of mvn tool.
To create a simple java project using maven, you need to open command prompt and
run the archetype:generate command of mvn tool.
Syntax
The syntax to generate the project architecture is given below:
1. mvn archetype:generate -DgroupId=groupid -DartifactId=artifactid
2. -DarchetypeArtifactId=maven-archetype-webapp -
DinteractiveMode=booleanValue
Example
The example to generate the project architecture is given below:
1. mvn archetype:generate -DgroupId=com.javatpoint -
DartifactId=CubeGeneratorWeb
2. -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Note: Here, we are using maven-archetype-webapp to create simple maven web
application. if you use maven-archetype-quickstart, it will generate a simple maven core
project.
Output
Now it will generate following code in the command prompt:
1. mvn archetype:generate -DgroupId=com.javatpoint -
DartifactId=CubeGeneratorWe
2. b -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
3. [INFO] Scanning for projects...
4. [INFO]
5. [INFO] ------------------------------------------------------------------------
6. [INFO] Building Maven Stub Project (No POM) 1
7. [INFO] ------------------------------------------------------------------------
8. [INFO]
9. [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-
pom >>
10. >
11. [INFO]
12. [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalon
e-pom <<
13. <
14. [INFO]
15. [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom
--
16. -
17. [INFO] Generating project in Batch mode
18. Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mav
19. en-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar
20. Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mave
21. n-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (4 KB at 3.8 KB/sec)
22. Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mav
23. en-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom
24. Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mave
25. n-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom (533 B at 0.8 KB/se
c)
26. [INFO] -------------------------------------------------------------------------
27. ---
28. [INFO] Using following parameters for creating project from Old (1.x) Archetype:
29. maven-archetype-webapp:1.0
30. [INFO] -------------------------------------------------------------------------
31. ---
32. [INFO] Parameter: groupId, Value: com.javatpoint
33. [INFO] Parameter: packageName, Value: com.javatpoint
34. [INFO] Parameter: package, Value: com.javatpoint
35. [INFO] Parameter: artifactId, Value: CubeGeneratorWeb
36. [INFO] Parameter: basedir, Value: D:\
37. [INFO] Parameter: version, Value: 1.0-SNAPSHOT
38. [INFO] project created from Old (1.x) Archetype in dir: D:\CubeGeneratorWeb
39. [INFO] ------------------------------------------------------------------------
40. [INFO] BUILD SUCCESS
41. [INFO] ------------------------------------------------------------------------
42. [INFO] Total time: 10.273s
43. [INFO] Finished at: Thu Dec 26 19:25:04 IST 2013
44. [INFO] Final Memory: 10M/24M
45. [INFO] ------------------------------------------------------------------------
46. 'cmd' is not recognized as an internal or external command,
47. operable program or batch file.
Generated Directory Structure
Now go to the current directory from where you have executed the mvn command. For
example: d:\CubeGeneratorWeb. You will see that a simple java project is created
that has the following directory:
1. CubeGenerator
2. -src
3. --main
4. ---resources
5. ---webapp
6. ----WEB-INF
7. -----web.xml
8. ----index.jsp
9. -pom.xml
As you can see, there are created 3 files pom.xml, index.jsp and web.xml. Let's have a
quick look at these files:
1) Automatically Generated pom.xml file
1. <project xmlns="http://maven.apache.org/POM/4.0.0"
2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4. http://maven.apache.org/maven-v4_0_0.xsd">
5. <modelVersion>4.0.0</modelVersion>
6. <groupId>com.javatpoint</groupId>
7. <artifactId>CubeGeneratorWeb</artifactId>
8. <packaging>war</packaging>
9. <version>1.0-SNAPSHOT</version>
10. <name>CubeGeneratorWeb Maven Webapp</name>
11. <url>http://maven.apache.org</url>
12. <dependencies>
13. <dependency>
14. <groupId>junit</groupId>
15. <artifactId>junit</artifactId>
16. <version>3.8.1</version>
17. <scope>test</scope>
18. </dependency>
19. </dependencies>
20. <build>
21. <finalName>CubeGeneratorWeb</finalName>
22. </build>
23. </project>
2) Automatically Generated index.jsp file
1. <html>
2. <body>
3. <h2>Hello World!</h2>
4. </body>
5. </html>
3) Automatically Generated web.xml file
1. <!DOCTYPE web-app PUBLIC
2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
4.
5. <web-app>
6. <display-name>Archetype Created Web Application</display-name>
7. </web-app>
Deploy and Run the Maven Web Project
Now you need to deploy the project on the server and access it by the following url:
http://<host-name>:<portnumber>/projectname, for example:
http://localhost:8888/CubeGeneratorWeb
next →←prev
Maven Web Application
We can create a simple maven web application example by executing
the archetype:generate command of mvn tool.
To create a simple java project using maven, you need to open command prompt and
run the archetype:generate command of mvn tool.
Syntax
The syntax to generate the project architecture is given below:
1. mvn archetype:generate -DgroupId=groupid -DartifactId=artifactid
2. -DarchetypeArtifactId=maven-archetype-webapp -
DinteractiveMode=booleanValue
Example
The example to generate the project architecture is given below:
1. mvn archetype:generate -DgroupId=com.javatpoint -
DartifactId=CubeGeneratorWeb
2. -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Note: Here, we are using maven-archetype-webapp to create simple maven web
application. if you use maven-archetype-quickstart, it will generate a simple maven
core project.
Output
Now it will generate following code in the command prompt:
1. mvn archetype:generate -DgroupId=com.javatpoint -
DartifactId=CubeGeneratorWe
2. b -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
3. [INFO] Scanning for projects...
4. [INFO]
5. [INFO] ------------------------------------------------------------------------
6. [INFO] Building Maven Stub Project (No POM) 1
7. [INFO] ------------------------------------------------------------------------
8. [INFO]
9. [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-
pom >>
10. >
11. [INFO]
12. [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalon
e-pom <<
13. <
14. [INFO]
15. [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom
--
16. -
17. [INFO] Generating project in Batch mode
18. Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mav
19. en-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar
20. Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mave
21. n-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (4 KB at 3.8 KB/sec)
22. Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mav
23. en-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom
24. Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archety
pes/mave
25. n-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom (533 B at 0.8 KB/se
c)
26. [INFO] -------------------------------------------------------------------------
27. ---
28. [INFO] Using following parameters for creating project from Old (1.x) Archetype:
29. maven-archetype-webapp:1.0
30. [INFO] -------------------------------------------------------------------------
31. ---
32. [INFO] Parameter: groupId, Value: com.javatpoint
33. [INFO] Parameter: packageName, Value: com.javatpoint
34. [INFO] Parameter: package, Value: com.javatpoint
35. [INFO] Parameter: artifactId, Value: CubeGeneratorWeb
36. [INFO] Parameter: basedir, Value: D:\
37. [INFO] Parameter: version, Value: 1.0-SNAPSHOT
38. [INFO] project created from Old (1.x) Archetype in dir: D:\CubeGeneratorWeb
39. [INFO] ------------------------------------------------------------------------
40. [INFO] BUILD SUCCESS
41. [INFO] ------------------------------------------------------------------------
42. [INFO] Total time: 10.273s
43. [INFO] Finished at: Thu Dec 26 19:25:04 IST 2013
44. [INFO] Final Memory: 10M/24M
45. [INFO] ------------------------------------------------------------------------
46. 'cmd' is not recognized as an internal or external command,
47. operable program or batch file.
Generated Directory Structure
Now go to the current directory from where you have executed the mvn command.
For example: d:\CubeGeneratorWeb. You will see that a simple java project is
created that has the following directory:
1. CubeGenerator
2. -src
3. --main
4. ---resources
5. ---webapp
6. ----WEB-INF
7. -----web.xml
8. ----index.jsp
9. -pom.xml
As you can see, there are created 3 files pom.xml, index.jsp and web.xml. Let's have
a quick look at these files:
1) Automatically Generated pom.xml file
1. <project xmlns="http://maven.apache.org/POM/4.0.0"
2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4. http://maven.apache.org/maven-v4_0_0.xsd">
5. <modelVersion>4.0.0</modelVersion>
6. <groupId>com.javatpoint</groupId>
7. <artifactId>CubeGeneratorWeb</artifactId>
8. <packaging>war</packaging>
9. <version>1.0-SNAPSHOT</version>
10. <name>CubeGeneratorWeb Maven Webapp</name>
11. <url>http://maven.apache.org</url>
12. <dependencies>
13. <dependency>
14. <groupId>junit</groupId>
15. <artifactId>junit</artifactId>
16. <version>3.8.1</version>
17. <scope>test</scope>
18. </dependency>
19. </dependencies>
20. <build>
21. <finalName>CubeGeneratorWeb</finalName>
22. </build>
23. </project>
2) Automatically Generated index.jsp file
1. <html>
2. <body>
3. <h2>Hello World!</h2>
4. </body>
5. </html>
3) Automatically Generated web.xml file
1. <!DOCTYPE web-app PUBLIC
2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
4.
5. <web-app>
6. <display-name>Archetype Created Web Application</display-name>
7. </web-app>
Deploy and Run the Maven Web Project
Now you need to deploy the project on the server and access it by the following url:
http://<host-name>:<portnumber>/projectname, for example:
http://localhost:8888/CubeGeneratorWeb
Maven Webapp in Eclipse
You can import the maven web project in eclipse. To do so, perform following steps:
1) Open eclipse IDE
2) Import the maven project
File Menu -> Import -> Maven -> Existing Maven Projects
-> Next -> Browse Project
-> Next -> Browse Project
-> Finish.
3) Run the maven web project
Right click on project -> Run As -> Run on Server
Maven Plugins
The maven plugins are central part of maven framework, it is used to perform specific
goal.
According to Apache Maven, there are 2 types of maven plugins.
1. Build Plugins
2. Reporting Plugins
Build Plugins
These plugins are executed at the time of build. These plugins should be declared inside
the <build> element.
Reporting Plugins
These plugins are executed at the time of site generation. These plugins should be
declared inside the <reporting> element.
Maven Core Plugins
A list of maven core plugins are given below:
Plugin Description
clean clean up after build.
compiler compiles java source code.
deploy deploys the artifact to the remote repository.
failsafe runs the JUnit integration tests in an isolated classloader.
install installs the built artifact into the local repository.
resources copies the resources to the output directory for including in the JAR.
site generates a site for the current project.
surefire runs the JUnit unit tests in an isolated classloader.
verifier verifies the existence of certain conditions. It is useful for integration tests.
List of Maven Plugins
To see the list of maven plugins, you may visit apache maven official
website http://repo.maven.apache.org/maven2/org/apache/maven/plugins/. Maven
plugins are also available outside the maven at codehaus.org and code.google.com.