Explain elements of pom.xml in Maven project


In each Maven project there can be one or more pom.xml file. Because Maven project can have multiple modules and each module has its own pom.xml file. The outermost pom.xml present at project level outside all the modules is called as parent pom.xml.

<project> : The outermost element is <project> tag : Every pom.xml file starts with <project> tag, it consists of namespaces attribute required by maven project.
<modelVersion> : It is used to specify the model version used by your locally installed maven version.
<groupId> : <groupId> tag is present inside the <project> tag, it used to identify the group of project/ module, same like package name in java class.
<artifactId> : <artifactId> is used to identify the unique project/ module name.
<version>: It is the version of the project which we are creating
<name> : We can provide complete name of the project in this tag.
<packaging>: <packaging> tag is used describe the output format of build. e.g. ear, war, jar, pom, etc.
<properties> : It is used to create variable values to be used as substitute at various places in pom.xml file, instead of writing same values at multiple places we can create properties. e.g. version number of various spring framework modules used in pom.xml
<dependencies> : This tag is used to include all  <dependency> tags, it is present inside <project> tag.
<dependency>: This tag consist of name of library / jar file required in your project to be downloaded from Maven repository, the library name is identified by artifactId. Once you include that artifactId, all the related or dependent libraries / jar file for this artifactId will be automatically downloaded.
<dependency> tag has its own <groupId>, <artifactId> and <version>.
<scope>: <scope> tag is present inside <dependency> tag, if the scope is declared as “provided” then Maven uses this library/ jar from Web server instead of downloading from maven repository.
<build>: <build> tag is present inside <project> tag, it consists of configuration details required to build the projects.
<finalName> : It is present inside the <build> tag, where we can provide what should be the name of ear/war etc. after the build is complete.
<plugins>: <plugins> tag is present inside <build> tag and it used to include all <plugin> tag.
<plugin>: It consist of <groupId>, <artifactId>, <version> and <configuration>. It used to specify the plugin to be used while building the Maven project. e.g. Compiler plugin which states that which version of JDK should be used to compile and build project. Similarly there are lots of plugins available in Maven
<modules> : It is used to include all <module> tags, this tag is present inside <project> tag and mostly present in parent pom.xml file only. It is used to include all other modules’ pom.xml file as a child in outermost pom.xml which acts as parent pom.xml.
<module> : This tag consist of name of child pom.xml’s artifactid.


Sample 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>epub.spring.war</groupId>
    <artifactId>evaluatethecode</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>epubmgmt Maven Webapp</name>

    <properties>
        <mysql.connector.version>5.1.9</mysql.connector.version>
        <spring.version>4.3.0.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>
    </dependencies>
    
    <build>
        <finalName>evaluatethecode</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
</project>

No comments:

Post a Comment