Build Project Maven

Build Project Maven

Maven is a build tool which used primarily in Java project. Because it’s java based tool so before using it you will have to installed JDK on your computer then download and install maven using this tutorial

  • Once installation is complete open command prompt type below command:

Build Project Maven

  • Create sample maven web project and below is sample project structure of maven web project:

Build Project Maven

  • As you see default directory structure of the project. Its main file that is called pom.xml is a core configuration of project where all information about project build is stored and it has details about project the way you want. If you are working on big project you will see pom.xml file huge can be difficult to understand but its not mandatory is to understand all of its component to use it effectively. Basic structure of 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>MavenTestProject</groupId>
  <artifactId>MavenTestProject</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>MavenTestProject 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>MavenTestProject</finalName>
  </build>
</project>

Run maven build: There are various phases are involved to build project in maven:

  • validate: It validates project is correct and check if all necessary information are available]
  • compile: Compile source code
  • test: Test compiled source code using unit testing framework. These tests should not require the code be packaged or deployed
  • package: Package the project in its distributed format
  • integration-test: Process and deploy package into an environment where we can run integration tests
  • verify: Verify packages if they are valid and meets criteria
  • install: Install project into local repository
  • deploy:Release on the environment and copies final package to remote repository

And apart form this there are two other maven life-cycle steps you can use:

  • clean: This cleans up all the build artifacts created before
  • site: This generates site documentation of the project

Please note: Executing any build phases maven executes its previous phases. For example if you execute install command it will all its previous command in order: validate –> compile — > test –> package –> integration-test –> verify –> install

  • Before running build it is good practice to execute clean command to clean out any previous build artifacts as below:

Build Project Maven

  • Now as you saw above for demo we have created web project it means packaging type is WAR. To package this project to WAR archive and ready to make it deployable on the server execute package command as below this will create WAR file inside target directory:

2015-01-18_1720

  • To verify if WAR file is created or not refresh target directory you should see new WAR file:

2015-01-18_1722

  • For more details visit maven official site here

download  Download Project:  MavenTestProject

Leave a Reply

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