Main Tutorials

How to build project with Maven

To build a Maven based project, open your console, change to your project folder where pom.xml file is placed, and issue this command :


mvn package

This will execute the Maven “package” phase.

Maven build lifecycle
Maven is run by phases, read this default Maven build lifecycle article for more detail. So, when the “package” phase is executed, all its above phases – “validate“, “compile” and “test“, including the current phase “package” will be executed orderly.

“mvn package” example

When you run “mvn package” command, it will compile source code, run unit test and pack it depends on your “packaging” tag in pom.xml file. For example,

1. If “packaging” = jar, it will package your project into a “jar” file and put it into your target folder

File : pom.xml


<project ...>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong</groupId>
	<artifactId>Maven Example</artifactId>
	<packaging>jar</packaging>
	...

2. If “packaging” = war, it will package your project into a “war” file and put it into your target folder.

File : pom.xml


<project ...>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong</groupId>
	<artifactId>Maven Example</artifactId>
	<packaging>war</packaging>
	...

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Trupti
6 years ago

I am getting this error-
Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:1.1 from any of the configured repositories

Please help me resolve this issue.

Joseph
8 years ago

Hi. My project team is using maven build. We have a package name as **/**/**/RCS under which all the classes are getting generated. But the jar does not contain these classes for some reason. Is there a way to include these classes into the RCS.jar?

There is no problem with other packages having a similar format.

Please suggest where the issue can be.

Thanks in advance!!!

http://wiredtreecouponcode2013.drupalgardens.com
10 years ago

Normally I do not learn article on blogs, however I wish to say that
this write-up very forced me to check out and do it! Your
writing taste has been amazed me. Thank you, quite nice post.

Akram
10 years ago

Hi mkyong, I have a question please, we have an ant build.xml, we want to migrate to maven, how to convert targets from build.xml to maven.
thanks, your help is appreciated.

rohit
11 years ago

how to genrate ear file from Maven?

Siva
11 years ago

when i run mvn package. i am getting the following error..

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\namasivayams&gt;cd C:\test\deleteLater\testMVNProject\mkyongweb-core

C:\test\deleteLater\testMVNProject\mkyongweb-core&gt;mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building mkyongweb-core Maven Webapp
[INFO]    task-segment: [package]
[INFO] ------------------------------------------------------------------------
[WARNING] POM for 'org.apache.maven.wagon:wagon-file:pom:1.0-beta-2:runtime' is
invalid.

Its dependencies (if any) will NOT be available to the current build.
[WARNING] POM for 'org.apache.maven.wagon:wagon-ssh-external:pom:1.0-beta-2:runt
ime' is invalid.

Its dependencies (if any) will NOT be available to the current build.
[WARNING] POM for 'org.apache.maven.wagon:wagon-ssh:pom:1.0-beta-2:runtime' is i
nvalid.

Its dependencies (if any) will NOT be available to the current build.
[WARNING] POM for 'org.codehaus.plexus:plexus-interpolation:pom:1.6:runtime' is
invalid.

Its dependencies (if any) will NOT be available to the current build.
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 0 resource
[WARNING] POM for 'org.codehaus.plexus:plexus-container-default:pom:1.0-alpha-8:
runtime' is invalid.

Its dependencies (if any) will NOT be available to the current build.
[WARNING] POM for 'org.codehaus.plexus:plexus-utils:pom:1.0.5:runtime' is invali
d.

Its dependencies (if any) will NOT be available to the current build.
[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\test\deleteLater\testMVNProject\mk
yongweb-core\src\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[WARNING] POM for 'org.apache.maven.surefire:surefire-api:pom:2.4.3:runtime' is
invalid.

Its dependencies (if any) will NOT be available to the current build.
-----------------------------------------------------
this realm = app0.child-container[org.apache.maven.plugins:maven-surefire-plugin
:2.4.3]
urls[0] = file:/C:/Users/namasivayams/.m2/repository/org/apache/maven/plugins/ma
ven-surefire-plugin/2.4.3/maven-surefire-plugin-2.4.3.jar
urls[1] = file:/C:/Users/namasivayams/.m2/repository/org/apache/maven/surefire/s
urefire-booter/2.4.3/surefire-booter-2.4.3.jar
urls[2] = file:/C:/Users/namasivayams/.m2/repository/org/apache/maven/surefire/s
urefire-api/2.4.3/surefire-api-2.4.3.jar
urls[3] = file:/C:/Users/namasivayams/.m2/repository/org/codehaus/plexus/plexus-
utils/1.5.1/plexus-utils-1.5.1.jar
Number of imports: 10
import: org.codehaus.classworlds.Entry@a6c57a42
import: org.codehaus.classworlds.Entry@12f43f3b
import: org.codehaus.classworlds.Entry@20025374
import: org.codehaus.classworlds.Entry@f8e44ca4
import: org.codehaus.classworlds.Entry@92758522
import: org.codehaus.classworlds.Entry@ebf2705b
import: org.codehaus.classworlds.Entry@bb25e54
import: org.codehaus.classworlds.Entry@bece5185
import: org.codehaus.classworlds.Entry@3fee8e37
import: org.codehaus.classworlds.Entry@3fee19d8


this realm = plexus.core
urls[0] = file:/C:/Downloads/apache-maven-2.2.1/lib/maven-2.2.1-uber.jar
Number of imports: 10
import: org.codehaus.classworlds.Entry@a6c57a42
import: org.codehaus.classworlds.Entry@12f43f3b
import: org.codehaus.classworlds.Entry@20025374
import: org.codehaus.classworlds.Entry@f8e44ca4
import: org.codehaus.classworlds.Entry@92758522
import: org.codehaus.classworlds.Entry@ebf2705b
import: org.codehaus.classworlds.Entry@bb25e54
import: org.codehaus.classworlds.Entry@bece5185
import: org.codehaus.classworlds.Entry@3fee8e37
import: org.codehaus.classworlds.Entry@3fee19d8
-----------------------------------------------------
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Internal error in the plugin manager executing goal 'org.apache.maven.plu
gins:maven-surefire-plugin:2.4.3:test': Unable to load the mojo 'org.apache.mave
n.plugins:maven-surefire-plugin:2.4.3:test' in the plugin 'org.apache.maven.plug
ins:maven-surefire-plugin'. A required class is missing: org/apache/maven/surefi
re/util/NestedCheckedException
org.apache.maven.surefire.util.NestedCheckedException
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Fri Aug 03 08:42:38 EDT 2012
[INFO] Final Memory: 9M/109M
[INFO] ------------------------------------------------------------------------
C:\test\deleteLater\testMVNProject\mkyongweb-core&gt;
venkatesh
12 years ago

i am runing the server in debuging mode i am getting this error:Could not initialize the ServletContextListener