Maven multi-module aggregation

The blog has been transferred to: http://www.muxuanli.com/lmx/

[Aggregation] Similar projects with multiple modules, when we build, we will hope that the build will be completed at one time, instead of executing the mvn command for each module. Maven's aggregation feature can achieve this requirement.
[Inheritance] Defining some dependencies in the parent project can avoid repeated definitions in other sub-projects. The value of <packaging> is also pom.

Take the admin project as an example, including version configuration and other information

admin-common is mainly POJO entity vo and other
admin- biz mainly performs some business operations (depending on biz)
admin-web web project depends on common/biz
admin-parent mainly defines the version of the jar package that the project depends on, etc., used for inheritance

On the above four directories, a new The project admin, the project has only one pom file, its special features are:
1. The value of <packaging> is pom, not the default jar
2. The modules element is used to achieve the aggregation of multiple modules


admin
        --- admin-common
        --- admin-biz
        --- admin-web
        --- admin-parent
        --- pom.xml 

step1 Create a general aggregation folder admin, which has only one pom.xml file

<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.lmx.test</groupId>
  <artifactId>admin</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Admin Project</name>
</project>


step2 Create a parent project in the admin directory, and there is only one pom.xml file in it

<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.lmx.test</groupId>
  <artifactId>admin-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Admin Parent</name>
</project>



Step3 Create common, biz, and web projects respectively.

Take the common project as an example: Execute the mvn command in the admin directory:
mvn archetype:generate -DgroupId=com.lmx.test -DartifactId=admin-common -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


Create the admin-web project:
mvn archetype:generate -DgroupId=com.lmx.test -DartifactId=admin-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractivMode=false



View the pom file of admin-common, which has inherited parent:

<?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.lmx.test</groupId>
    <artifactId>admin</artifactId>
    <version>0.0.1-SNAPSHOT</version>

  </parent>
  <groupId>com.lmx.test</groupId>
  <artifactId>admin-common</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>admin-common</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>


Among them, groupId version, etc. can be inherited, so it can be deleted!


View the pom file under admin: 3 submodules have been automatically added under the modules element
<?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.lmx.test</groupId>
  <artifactId>admin</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Admin Project</name>
  <modules>
    <module>admin-common</module>
    <module>admin-biz</module>
    <module>admin-web</module>
  </modules>
</project>



step4 eclipse introduction
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326570836&siteId=291194637