Home » Maven » How To Build Project With Maven

How To Build Project With Maven

A maven project can be build using the "mvn package" command.Open your console, change to your project directory where pom.xml file is placed, and issue following command :

  1. If “packaging” = jar, it will package your project into a “jar” file and put it into your target folder.Sample pom.xml is given bellow.
  2. <modelVersion>4.0.0</modelVersion>
      <groupId>com.javawebtutor</groupId>
      <artifactId>myexample</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>
      <name>myexample</name>
    

  3. If “packaging” = war, it will package your project into a “war” file and put it into your target folder.Sample pom.xml is given bellow.
  4. <modelVersion>4.0.0</modelVersion>
      <groupId>com.javawebtutor</groupId>
      <artifactId>myexample</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
      <name>myexample</name>
    


Note:-When no packaging is declared, Maven assumes the artifact is the default: jar.


“mvn package” example


Step 1 : Create Java Project

Create java project in your favourite directory

First of all, using the terminal (Linux or Mac) or the command prompt (Windows), navigate to the folder where the new project shall be created and issue the following command

mvn archetype:generate -DgroupId=com.javawebtutor -DartifactId=JavaApplication -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command will create java project in your directory


Step 2 : 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

After executing this command Maven will start building the project as shown bellow.

D:\MavenProject\JavaApplication>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building JavaApplication 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ JavaApplic
ation ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\MavenProject\JavaApplication\src\m
ain\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ JavaApplicati
on ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 1 source file to D:\MavenProject\JavaApplication\target\classes

[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ Ja
vaApplication ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\MavenProject\JavaApplication\src\t
est\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ JavaA
pplication ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 1 source file to D:\MavenProject\JavaApplication\target\test-cl
asses
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ JavaApplication ---
[INFO] Surefire report directory: D:\MavenProject\JavaApplication\target\surefir
e-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.javawebtutor.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ JavaApplication ---
[INFO] Building jar: D:\MavenProject\JavaApplication\target\JavaApplication-1.0-
SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.767s
[INFO] Finished at: Fri Jun 06 12:17:19 IST 2014
[INFO] Final Memory: 6M/15M
[INFO] ------------------------------------------------------------------------
D:\MavenProject\JavaApplication>

Now check project target folder you can find generated jar file there.


Previous Next Article

comments powered by Disqus