Maven Combat (vi)--aggregation and inheritance of sub-modules

Source: Internet
Author: User

1. Concepts of aggregation and inheritance

Aggregation: Put multiple projects together to run

New parent project, adding in Pom.xml

<modules> < Module>SIP-utils</module>< Module>SIP-web</module>< Module>SIP-SQL</module>< Module>SIP-core</module></modules>

These are the projects that are aggregated so that mvn clean install this pro will also package the above items

Inheritance: For example, if you use JUnit for a project, you can create a Jutint parent class like Java

Then we can inherit a parent project in the project and configure it as follows in Pom.xml

    <parent>        <groupId>com.learnPro</groupId>        <artifactId>SIP-parent</artifactId>        <version>0.0.1-SNAPSHOT</version>    </parent>
2. Building sub-modules from the command line

For example, the module structure we need to build is as follows:

2.1 Building the parent module pro-parent
mvn org.apache.maven.plugins:maven-archetype-plugin:2.2-DgroupId=com.-DartifactId=pro-parent-DarchetypeArtifactId=maven-archetype-quickstart

There was a problem here, before the following command, but build does not pass

-DgroupId=com.-DartifactId=pro-parent-DarchetypeArtifactId=maven-archetype-quickstart

Workaround, please refer to: http://josh-persistence.iteye.com/blog/2258213

With the above command you can see that our project has been created successfully
At this point, we can delete the Src folder (because the parent item is just a container, no code required)
Modify the Pom.xml file, modify the jar to Pom,pom to indicate that it is an inherited module, and modify the contents as follows

<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/xsd/maven-4.0.0.xsd ">    <modelversion>4.0.0</modelversion>  <groupId>Com.learnpro</groupId>  <artifactid>Pro-parent</artifactid>  <version>1.0-snapshot</version>  <packaging>Pom</Packaging><!--change from jar to pom-->  <name>Pro-parent</name>  <URL>http://maven.apache.org</URL>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </Properties>  <dependencies>    <dependency>      <groupId>Junit</groupId>      <artifactid>Junit</artifactid>      <version>3.8.1</version>      <scope>Test</Scope>    </Dependency>  </dependencies></Project>
2.2 Building sub-modules DAO

Run the following command in the previous folder

cd pro-parentmvn org.apache.maven.plugins:maven-archetype-plugin:2.2-DartifactId=pro-dao-DarchetypeArtifactId=maven-archetype-quickstart

After the compilation is successful, the following is automatically added in the Pro-parent pom.xml

  <modules>    <module>pro-dao</module>  </modules>

Let's take a look at Pro-dao's pom file.

<?xml version= "1.0"?><project xsi:schemalocation="http://maven.apache.org/POM/4.0.0/HTTP// Maven.apache.org/xsd/maven-4.0.0.xsd " xmlns=" http://maven.apache.org/POM/4.0.0 "xmlns : xsi="Http://www.w3.org/2001/XMLSchema-instance">      <modelversion>4.0.0</modelversion>  <parent>    <groupId>Com.learnpro</groupId>    <artifactid>Pro-parent</artifactid>    <version>1.0-snapshot</version>  </Parent>  <groupId>Com.learnpro</groupId>  <artifactid>Pro-dao</artifactid>  <version>1.0-snapshot</version>  <name>Pro-dao</name>  <URL>http://maven.apache.org</URL>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </Properties>  <dependencies>    <dependency>      <groupId>Junit</groupId>      <artifactid>Junit</artifactid>      <version>3.8.1</version>      <scope>Test</Scope>    </Dependency>  </dependencies></Project>

Here, it automatically adds <parent> , we need to put in <groupId>com.learnpro</groupId>和<version>1.0-SNAPSHOT</version>去掉,并加上<packaging>jar</packaging> , because we already have a parent module, GroupID and version inherit the GroupID and version from the parent module, and finally set the packaging packaging method to Jar

3.eclipse plug-in Build project 3.1 build parent Project
右键->new->other->搜索maven,选择Maven Project


3.2 Building sub-modules
右键->new->other->搜索maven,选择Maven Module

Then next, and then select the MAVEN project model, you can create the corresponding sub-modules
At the same time, we can find that the pro-parent Pom file also adds the following content

  <modules>    <module>pro-children</module>  </modules>

At this time, we also need to<groupId>com.learnpro</groupId>和<version>1.0-SNAPSHOT</version>去掉,并加上<packaging>jar</packaging>

3.3 Importing MAVEN Projects
右键->Import->搜索maven,选择Existing Maven Project

Choose the pro-parent we built earlier.

Maven Combat (vi)--aggregation and inheritance of sub-modules

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.