Maven Learning Record (iv)-Multi-module development configuration

Source: Internet
Author: User

The benefits of multiple modules are collaborative development, for large projects such division of labor can improve efficiency, while reducing code coupling, it seems clear, MAVEN multi-module also solves the different people development use of the rack package version conflicts and other issues

I. Creating a project 1. Create a parent container

Do not use the template, directly created, after the creation of the SRC directory can be directly deleted, this container is used to host other sub-projects

2. Creating sub-projects

Right-click on the parent project and create module to select the template you want to use

3. Create a configuration after idea generation

The first is the directory structure,

corresponding MAVEN structure, root on behalf of parent project

The Pom.xml,idea of the parent project is very smart to configure the configuration is complete

<?xml version= "1.0" encoding= "UTF-8"?><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.mrdear</groupId>    <artifactid>Module_main</artifactid>    <!--Pom Type indicates that the current project is a parent item --    <packaging>Pom</Packaging>    <version>1.0-snapshot</version>    <!--modules The list of items used to define the current module--    <modules>        <module>Maven-parent</module>        <module>Maven-entity</module>        <module>Maven-core</module>        <module>Maven-web</module>    </Modules></Project>

Pom.xml of sub-projects

<?xml version= "1.0" encoding= "UTF-8"?><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 ">                      <!--represents the item currently inheriting with this maven coordinate--    <parent>        <artifactid>Module_main</artifactid>        <groupId>Com.mrdear</groupId>        <version>1.0-snapshot</version>    </Parent>    <modelversion>4.0.0</modelversion><!--package type is not Configured, default is Jar-->    <artifactid>Maven-entity</artifactid></Project>

To this whole multi-module even if the creation is complete, have to say with idea is very convenient, summed up
1. Create a parent project
2. Creating sub-projects
3. Parent Project Settings Package Type packaging is POM, represented as parent project, and Child project is managed with modules label
4. Child projects inherit parent items by using parent tag

Two. Using Dependencymanagement to manage dependencies

The way in which the dependencymanagement elements are indicated in the POM maven combines project inheritance to manage dependencies. In multi-module applications, multiple sub-projects may have a common dependency. In order to run correctly, you must have all the child projects use the same version of the dependency. You must ensure that the dependencies and versions of each project are applied consistently to ensure that the same results are tested and published. Therefore, a common dependency should be defined in the top-level pom.

Unlike Dependencys, although the subproject inherits the Pom file, only the displayed declarations are introduced and are not actually introduced, and not all of the inherited packages are introduced like Dependencys.
In other words , dependencymanagement only affects existing dependent configurations, but does not introduce dependencies

For example, configure in the main project:

    <dependencymanagement>        <dependencies>            <dependency>                <groupId>Junit</groupId>                <artifactid>Junit</artifactid>                <version>4.10</version>                <scope>Test</Scope>            </Dependency>        </dependencies>    </dependencymanagement>

Sub-projects are introduced:

    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>        </dependency>    </dependencies>

This ensures that all the sub-projects are using the same version of the package

Three. Using the Pluginmanagement Management plugin

Pluginmanagement and Dependencymanagement are different, after the configuration is complete, the subproject is directly inherited and can be viewed through effectivepom, so general configuration of common plug-ins, such as compiled version, encoding, etc.

<build>        <pluginmanagement>            <plugins>                <plugin>                    <groupId>Org.apache.maven.plugins</groupId>                    <artifactid>Maven-compiler-plugin</artifactid>                    <version>2.3.2</version>                    <configuration>                        <source>1.8</Source>                        <target>1.8</target>                        <encoding>UTF-8</encoding>                    </configuration>                </plugin>            </plugins>        </pluginmanagement>    </Build>

As configured directly in the parent project, the child project can use the plugin directly, without configuration.

Four. Interdependence between projects

Directly using the dependency tag configuration, the project as a jar, as follows is to increase the dependency of entity entities, the benefit is to co-development, different people write different modules

  <!--配置子项目依赖-->        <dependency>            <groupId>com.mrdear</groupId>            <artifactId>maven-entity</artifactId>            <version>1.0-SNAPSHOT</version>        </dependency>

Maven Learning Record (iv)-Multi-module development configuration

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.