MAVEN Multi-module Project Management summary

Source: Internet
Author: User

Preface

I recently completed a Web project built with Maven and looked at some maven books, such as "maven combat," but still did not understand the MAVEN multi-module project clearly, so took a little time to study the next, now share the following.

Problem

The following is a brief project structure diagram

Parent
'------Childa (Businesslayer)
'---pom.xml

'------childb (Weblayer)
'---pom.xml

'------pom.xml

1. How can the parent find Childa and childb?

In Maven, the parent module organizes Childa and childb, called "Aggregations," and multiple modules are compiled together. Simple to implement, simply add the following to the parent's pom file.

<modules>
<module>childA</module>
<module>childB</module>
</modules>

2. Is this a complete OK?

This only tells the Maven compiler to find Childa and childb when reading the parent's pom file, but it will compile the dependencies they have introduced separately. This will cause the Pom file to introduce the package duplication!! So we introduce the concept of "inheritance", that is, to form a "parent-child" relationship, the child Pom can refer to the dependency introduced in the parent POM. The following are the specific practices:

In the parent, write the following, where the line identified by "*" can form a path through which the Pom file can be found in the MAVEN repository! In this example, Path is M2_path/com/sang/main/parent-moduel/1.0.2/xxxx-1.0.2.pom. So these three tags are required!!!

<modelVersion>4.0.0</modelVersion>
<groupId>com.sang.main</groupId> *
<artifactId>Parent-Moduel</artifactId>*
<version>1.0.2</version> *
<packaging>pom</packaging>
<name>Simple-main</name>

The parent Pom is written, and the child Pom inherits the dependency of the parent pom through the <parent> tag, as follows:

<parent>
<groupid>com.sang.main</groupid>
<artifactId>Parent-Moduel</artifactId>
<version>1.0.2</version>
<relativepath>, .... /pom.xml</relativepath> <!--In this example here is optional--
</parent>

It is important to note that the <relativePath> tag can be omitted if the hierarchical relationship of the POM is only one layer apart, as in this example. Maven can also find the child Pom.

After the introduction of the <parent> tag in the child Pom, the properties such as <version> are inherited from the parent Pom, for example Childa only need to add the following content!

<modelVersion>4.0.0</modelVersion>
<groupId>com.sang.business</groupId> <!--and Artifactid uniquely identify this jar file--
<artifactId>ChildA-module</artifactId>
<packaging>jar</packaging> <!--indicate package type--
<name>childA</name>

3, how to add dependencies?

MAVEN allows us to easily manage jar dependencies, as follows:

<dependencies>
<dependency> <!--Add a jar package dependent--
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>

If you do not pass inheritance, you need to include such a dependency in each pom so that the module corresponding to the child Pom can refer to the jar package. The above mentioned duplicate reference jar package can be resolved in the following way:

The dependency of the main pom on the <dependecyManagement>, indicating that the child pom may be used by the jar package dependency

<Dependencymanagement>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</dependencymanagement>

Child Pom If you need to reference the jar package, you can directly reference it! No need to join <VERSION>, easy to manage in a unified. You can also include jar packages that are only used in the child Pom, such as:

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId> <!--There 's no need for verison here! -
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.9.4</version> <!--Of course you can also add a jar that is only used in this submodule--
</dependency>
</dependencies>

4, in addition to the jar dependencies, plug-ins can also be managed in this way

Mainmodule-
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<!-- Childa--
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
</plugins>
</build>

5, if there is a reference relationship between the child Pom, such as Childa reference to the Childb jar package, how to do?

<dependency>
<groupId>com.module</groupId>
<artifactId>childA</artifactId> <!--plus Childa dependency--
<version>1.0.0</version>
</dependency>

Summary

This article only explains the basic usage of MAVEN from the perspective of multi-module organization, and of course, the other complex usage is to refer to the MAVEN handbook.

(End of full text)

Reprinted blog, Convenient for later use, reprint address: http://blog.csdn.net/whuslei/article/details/7989102

MAVEN Multi-module Project Management summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.