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>
	...
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website mkyong.com is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts