Quantcast
Viewing all articles
Browse latest Browse all 10

Basics of Maven Build

Maven is a very powerful build tool. there are many benefits of maven in terms of development and maintenance, Before maven Ant was the popular build tool but time to time programmers found some difficulties in Ant tool. Maven and Ant both are open source and developed by apache foundation.
Before installing and creating sample project in maven let us start with some basic concepts in maven.

  • Maven uses the repository for download the dependencies jars or pom. There are three kind of repository.
    • Local Repository: This repository will be created under the user folder with .m2 name if you are using windows you can find out this folder in your user folder.  Eg: C:Userschandan.m2. When you are packaging/compiling your project then all the dependency jar files will be downloaded from central or intrnal repository and kept in this local repository. So if you do packaging/compile again the dependency will be taken from local repository.
    • Internal Repository: this is the repository for your internal organization use. If your organization has some internal private repository then you can use this repository also after adding the repository in your project pom or maven settings like below
      
      <project>
      ...
        <repositories>
          <repository>
            <id>my-internal-site</id>
            <url>http://myserver/repo</url>
          </repository>
        </repositories>
      ...
      </project>
      
    • Central Repository: This repository is the main repository for all the dependency where you can find the all the maven dependency. You can find out the dependency information from central repository from the given url: http://mvnrepository.com
  • Another important file in maven is settings.xml, if you want to change the repository location or want to use internal repository then you need to create settings.xml file and provide these information. This settings.xml file can be created in two places.
    • If you want to change the settings for particular user then put this file to your local repository folder eg: ${user.home}/.m2/settings.xml.
    • if you want to change the complete maven settings for all the users in the system then  put this file in this location : $M2_HOME/conf/settings.xml
      Overview of top elements of settings.xml is
      
      <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
      http://maven.apache.org/xsd/settings-1.0.0.xsd">
        <localRepository/>
        <interactiveMode/>
        <usePluginRegistry/>
        <offline/>
        <pluginGroups/>
        <servers/>
        <mirrors/>
        <proxies/>
        <profiles/>
        <activeProfiles/>
      </settings>
      

      You can find out more details at http://maven.apache.org/settings.html or http://maven.apache.org/ref/3.2.3/maven-settings/settings.html

  • One of the important file in maven project is pom.xml where we are defining the project information and dependencies.
  • Some of the basic term which is used in maven pom.xml is:
    
    <project>
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.test</groupId>
       <artifactId>test-project</artifactId>
       <version>1.0-SNAPSHOT</version>
    </project>
    
    • modelVersion:  The maven pom version number. Indicates which version of pom specification we are using.
    • groupId: your project’s organizational group Id.
    • artifactId: the name of the artifact of the project. Simply you can say this is the project name. And after packaging your package name will be the artifact name with version number. If you are not using any additional maven plugin.
    • Version: the version number of your project.
  • Basically maven manages all above things in a repository if you want to add other dependency to your project then you need the below information
    
    <dependencies>
      <dependency>
         <groupId>org.apache.logging.log4j</groupId>
         <artifactId>log4j-core</artifactId>
         <version>2.0.2</version>
         <type>jar</type>
         <scope>runtime</scope>
      </dependency>
    </dependencies>
    

    To know more about dependency management browser this url : http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

  • Maven build phases:
    • validate – validate the project is correct and all necessary information is available
    • compile – compile the source code of the project
    • test – test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
    • package – take the compiled code and package it in its distributable format, such as a JAR.
    • integration-test – process and deploy the package if necessary into an environment where integration tests can be run
    • verify – run any checks to verify the package is valid and meets quality criteria
    • install – install the package into the local repository, for use as a dependency in other projects locally.
    • deploy – done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

We will install maven and create a sample project  in next article.

The post Basics of Maven Build appeared first on CodingLoading.


Viewing all articles
Browse latest Browse all 10

Trending Articles