maven multi-module and inheritance

Keywords: Maven Spring nexus encoding

maven has the function of multi-module aggregation, that is, it can reflect the relationship between modules by parent-child relationship, and associate each module.

 

Firstly, we introduce the module with inheritance dependency (multiple modules are not only associated with public modules, but also interdependent with each other).

Definition of parent module:

1.packaging must be a pom type

2.modules Partial Declarations Submodule

3.properties declares public variables that can be inherited directly
4.dependencies are public dependencies that can be inherited directly.

5. Dependency Management is used to manage dependencies. Elements configured by Dependency Management neither introduce dependencies from parent projects nor from child projects. It simply means that this part is inheritable, but not directly referenced. If you need to refer to the dependency management section, just declare groupId and artifactId, and no version number is required. maven or automatically inherits dependencies of the same name in dependency management.

6. Repository toties are used to define private service warehouses.

7. Distribution Management is used to publish packaged projects to private servers.

The following example defines an iscp_esb_components parent project, which includes two modules: iscp_esp_components_web and iscp_esb_components_dqms. iscp_esp_components_web depends on iscp_esb_components_dqms module.

<modelVersion>4.0.0</modelVersion>  
<groupId>com.dhcc.iscp.components</groupId>  
<packaging>pom</packaging>  
<artifactId>iscp_esb_components</artifactId>  
<version>2.1.0</version>  
<name>iscp_esb_components</name>  
<modules>  
    <module>iscp_esb_components_web</module>  
    <module>iscp_esb_components_dqms</module>  
</modules>  
<properties>  
    <package.environment>src/main/resources</package.environment>  
    <struts2.version>2.3.16.3</struts2.version>  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
</properties>  
<dependencies>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-orm</artifactId>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-aop</artifactId>  
    </dependency>  
</dependencies>  
<dependencyManagement>  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-beans</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-core</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
    </dependencies>  
</dependencyManagement>  
  
<repositories>  
    <repository>  
        <id>mvn-repo</id>  
        <url>http://maven.ansj.org/</url>  
    </repository>  
</repositories>  
<!-- deploy To private clothes -->  
<distributionManagement>  
    <repository>  
        <id>releases</id>  
        <name>dhcc_releases</name>  
        <url>http://113.140.81.70:8081/nexus/content/repositories/dhcc_releases/</url>  
    </repository>  
    <snapshotRepository>  
        <id>Snapshots</id>  
        <name>dhcc_snapshots</name>  
        <url>http://113.140.81.70:8081/nexus/content/repositories/dhcc_snapshots/</url>  
    </snapshotRepository>  
</distributionManagement>  

Submodule definition

Parent part declares parent module

Dependencies section adds iscp_esb_components_dqms module dependencies

iscp_esb_components_web module definition

<parent>  
    <groupId>com.dhcc.iscp.components</groupId>  
    <artifactId>iscp_esb_components</artifactId>  
    <version>2.1.0</version>  
</parent>  
<groupId>com.dhcc.iscp.components.web</groupId>  
<artifactId>iscp_esb_components_web</artifactId>  
<version>2.1.0</version>  
<packaging>war</packaging>  
<name>iscp_components_web</name>  
<dependencies>  
    <dependency>  
        <groupId>com.dhcc.iscp.components</groupId>  
        <artifactId>iscp_esb_components_dqms</artifactId>  
        <version>2.1.0</version>  
    </dependency>  
</dependencies>  
      
<build>  
    <finalName>iscp_components_web</finalName>  
    <sourceDirectory>src/main/java</sourceDirectory>  
    <resources>  
        <resource>  
            <directory>src/main/resources</directory>  
        </resource>  
        <resource>  
            <directory>src/main/baseResources</directory>  
        </resource>  
    </resources>  
    <testResources>  
        <testResource>  
            <directory>src/test/resources</directory>  
        </testResource>  
    </testResources>  
          
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-compiler-plugin</artifactId>  
            <version>2.0.2</version>  
            <configuration>  
                <source>1.7</source>  
                <target>1.7</target>  
                <encoding>UTF-8</encoding>  
            </configuration>  
        </plugin>  
        <plugin>  
            <artifactId>maven-war-plugin</artifactId>  
            <version>2.1</version>  
            <configuration>  
                <webappDirectory>webContent</webappDirectory>  
                <failOnMissingWebXml>false</failOnMissingWebXml>  
                <packagingExcludes>  
                    WEB-INF/lib/spring-2.5.**.jar,  
                    WEB-INF/lib/javassist-3.11.0.GA.jar,  
                    WEB-INF/lib/xml-apis-1.0.b2.jar,  
                    WEB-INF/lib/jackson-core-asl-1.9.11.jar,  
                    WEB-INF/lib/jackson-mapper-asl-1.9.11.jar,  
                    WEB-INF/lib/c3p0-0.9.1.1.jar,  
                    WEB-INF/lib/commons-fileupload-1.3.jar,  
                </packagingExcludes>  
            </configuration>  
        </plugin>  
    </plugins>  
</build>  

iscp_esb_components_dqms module definition

1. packaging is not declared, but is packaged as jar by default.

2. No version is declared. GroupId inherits the version of the parent module by default, groupId.

<groupId>com.dhcc.iscp.components</groupId>  
    <artifactId>iscp_esb_components</artifactId>  
    <version>${iscp-components}</version>  
</parent>  
<artifactId>iscp_esb_components_dqms</artifactId>  
<name>iscp_esb_components_dqms</name>

maven compilation process

For this project, we have two ways of compiling.
The first is the direct maven clean install parent project (iscp_esb_components). maven compiles all projects in turn through the Reactor mechanism. In this case, maven compiles the iscp_esb_components project first, and then finds that iscp_esb_components_web depends on iscp_esb_components_dqms, so it compiles iscp_esb_components_dqms first, and iscp_esb_components_web finally.

Compiled under the parent module, maven knows all the sub-modules under the parent module, and processes the execution according to the dependencies.

The second is compiling and packaging separately. That is to say, each project is compiled separately, but there is a problem in doing so. When we first package iscp_esb_components_web, maven will find that iscp_esb_components_web depends on iscp_esb_components_dqms. Then we will go to the local warehouse to find the jar of iscp_esb_components_dqms. But now there is no jar compiled by iscp_esb_components_dqms, and the local warehouse does not have the jar yet. maven goes to the remote warehouse to find it. You also publish the jar to the private service. If you still can't find it, you will report an error. You need to manually recompile iscp_esb_components_dqms.

When compiling maven sub-projects, Maven compiles and executes not only the sub-projects, but also the parent projects, but also the sub-projects of sibling projects and their siblings.

Non-dependency multi-module projects

That is to say, sibling projects are not related to each other, but only dependent on parent projects.

For this type of project, we can delete the parent module's <modules>... </modules> section, while the child module retains the parent section. At this time, the project is no longer a multi-module project, but a collection of multi-project projects.

The above example shows that when iscp_esb_components are compiled without the <modules> part of iscp_esb_components, only iscp_esb_components themselves will be compiled, and web and dqms modules will not be compiled. If iscp_esb_components_web is compiled, maven will first go to the warehouse to find iscp_esb_components and iscp_esb_components_dqms dependencies, and then execute them in turn, because the parent-child relationship still exists.

At this time, when publishing web modules, iscp_esb_components-> iscp_esb_components_dqms-> iscp_esb_components_web should be published in turn.

If the parent part of the sub-module is deleted while the parent part of the parent module is retained, then the project is no longer a parent-child project, but a multi-module project, each sub-project has its own resources and cannot be shared.

Posted by IwnfuM on Sat, 20 Apr 2019 23:48:35 -0700