Using MAVEN to build multiple module projects __maven

Source: Internet
Author: User

Code Https://github.com/beiyoufx/maven-multi-module
The code does not contain engineering files, it supports idea import and Eclipse import . Why do we have to do modular development?

When multiple people are using MAVEN to collaborate on a project, especially on a slightly larger scale, each rd's work is subdivided into specific features and modules, and some modules are even deployed separately.

We assume that there are such a mall project, including the following modules: Mall front desk (shop) management background (Admin) Database interaction Module (DAO) Common Business Module (Service) interface module (API) general tools (UTIL)

where shop and admin need to be deployed separately, DAO, service, util you may want some experienced people to maintain, if you use an application to manage, all the functions and modules will be coupled together, everyone can modify the code, this is obviously not what we expect.

And with an application to manage, any one point of code changes, the entire project needs to be build, another advantage of using modular development is that if the DAO's code is modified, it is only necessary to rebuild the DAO module. Web modules can be build into War,dao, service, util, etc. can build into a jar, only need to configure a good dependency, you can achieve the solution coupling between modules. This design is to follow the "high cohesion, low coupling" design principles. How do we make modular development?

We use the example above to demonstrate that we first make reasonable optimizations, and we want the DAO and service to be used as a common underlying tool, merging them into one core module (CORE), build into Core.jar, and the simple MAVEN modular project structure as follows:

----------Mall         //top-level items
   |------pom.xml      //packaging = Pom
   |------mall-util//    Universal Tools
   |  | ---pom.xml      //packaging = jar
   |------mall-core    //core Module
   |  | ---pom.xml      //packaging = jar
   |------MALL-WEB-API//Interface Module |  | ---pom.xml      //packaging = War
   |------mall-web-admin//Management Background
   |  | ---pom.xml      //packaging = War
   |------mall-web-shop//Mall front desk
   |  | ---pom.xml      //packaging = War

The APIs, admin, and shop in these modules are Web applications that can be deployed individually, without dependencies, but all rely on the core module, while the core module relies on the Util module. Next we build the project structure according to the structure we identified above. use idea to create a MAVEN multi-module Project First, create a common maven project New Project

Fill in the basic information, use IPR as the project description file

Generic MAVEN projects do not need to be built using MAVEN templates

add modules to the MAVEN project New Module

Filling in basic information, the JAR project also does not need to use the Maven template to build

At this point, we can see that the module we added has been introduced into parent's pom file.

<groupId>com.mall</groupId>
<artifactId>mall</artifactId>
<packaging>pom </packaging>//Packing mode for pom
<version>1.0-SNAPSHOT</version>

<modules>
    < Module>mall-util</module>
</modules>
Change the Util module's build method to jar
<parent>
    <artifactId>mall</artifactId>
    <groupId>com.mall</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

<packaging>jar</packaging>// Packaged as Jar
<artifactId>mall-util</artifactId>
iii. Adding a Web module to a MAVEN projectCreate a module and select the Create from Archetype option, while the MAVEN template selects WebApp

Then patiently wait for Maven to help you create module, which has been added

<modules>
    <module>mall-util</module>
    <module>mall-web-admin</module>
</modules>

The directory structure is as follows:

Pom

<parent>
    <artifactId>mall</artifactId>
    <groupId>com.mall</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelversion>4.0.0</modelversion >
<artifactId>mall-web-admin</artifactId>
<packaging>war</packaging>
<name>mall-web-admin</name>
<url>https://github.com/beiyoufx</url>
< dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId> junit</artifactid>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <finalname>mall-web-admin</ Finalname>
</build>
iv. adding dependencies between modulesIncrease the reliance of core and Util

Increase the dependencies between admin and Core

Admin and Core, util chain of dependence

Construction and release of multi-module project packaged

All builds in the root project are passed into the module, for example, package in Root will package the entire project, and when the file is changed, it will be aggregated, and the other commands are the same. The package in the module will only package the current module.

Use the Source:jar command to package the source code. Publish

Web modules can be deployed separately or aggregated.

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.