Maven Learning Summary (eight)-building multi-module projects with Maven

Source: Internet
Author: User

Maven Learning Summary (eight)-building multi-module projects with Maven

In the usual Javaweb project development in order to facilitate the maintenance of later, we generally will be layered development, the most common is divided into domains (domain model layer), DAO (Database access Layer), service (business logic layer), the Web (presentation layer), so that after layering, The responsibilities between the layers are clear, and later maintenance is relatively easy, and today we are using MAVEN to build each layer above.

The project structure is as follows:

System-parent
|----Pom.xml
|----System-domain
|----Pom.xml
|----System-dao
|----Pom.xml
|----System-service
|----Pom.xml
|----System-web
|----Pom.xml

First, create the System-parent project

Create a system-parent that is used to inherit from each sub-module.

Go to the command line and enter the following command:

MVN Archetype:create-dgroupid=me.gacl-dartifactid=system-parent-darchetypeartifactid=maven-archetype-quickstart -dinteractivemode=false

As shown in the following:

  

After the command execution, you can see that the System-parent directory is generated in the current directory (C:\Documents and settings\administrator) with a src directory and a pom.xml file, as shown in:

  

  Delete the src folder and modify the Pom.xml file to change <packaging>jar</packaging> to <packaging>pom </packaging>, Pom indicates that it is an inherited module, with the following modified content:

1 <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 2 xsi : schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > 3 < Modelversion>4.0.0</modelversion> 4 5 <groupId>me.gacl</groupId> 6 <artifactId> System-parent</artifactid> 7 <version>1.0-SNAPSHOT</version> 8 <packaging>Pom</packaging> 9 <name>system-parent</name>11 <url>http://maven.apache.org</url>12 <properties>14 <project.build.sourceencoding>utf-8</project.build.sourceencoding>15 </pro perties>16 <dependencies>18 <dependency>19 <groupid>junit</groupid>20 < Artifactid>junit</artifactid>21 <version>3.8.1</version>22 <scope>test</scope&gt ; </dependency>24 </dependencies>25 </project>
Two Creating a Sytem-domain module

  Enter the created system-parent directory at the command line, and then execute the following command:

MVN Archetype:create-dgroupid=me.gacl-dartifactid=system-domain-darchetypeartifactid=maven-archetype-quickstart -dinteractivemode=false

As shown in the following:

  

After the command execution, you can see that the System-domain is generated in the System-parent directory, which contains the SRC directory and the Pom.xml file. As shown in the following:

  

  

At the same time, the Pom.xml file in the System-parent directory automatically adds the following:

<modules>    <module>system-domain</module></modules>

At this point, System-parent's Pom.xml file is as follows:

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <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 "> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId> Me.gacl</groupid> 6 <artifactId>system-parent</artifactId> 7 <version>1.0-snapshot</ Version> 8 <packaging>pom</packaging> 9 <name>system-parent</name>11 <url>http:/ /maven.apache.org</url>12 <properties>14 <project.build.sourceencoding>utf-8</ project.build.sourceencoding>15 </properties>16 <dependencies>18 <dependency>19 < Groupid>junit</groupid>20 <artifactid>junit</artifactid>21 <version>3.8.1</versi   On>22 <scope>test</scope>23 </dependency>24</dependencies>25 <modules>26 <module>system-domain</module>27 </modules>28 </pro Ject>

Modify the Pom.xml file in the System-domain directory to <groupId>me.gacl</groupId> and <version>1.0-snapshot </version> Remove, plus <packaging>jar</packaging>, Because GroupID and version inherit the GroupID and version,packaging settings in System-parent, the package is a jar

The modified Pom.xml file is as follows:

1 <?xml version= "1.0"?> 2 <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 "3     xmlns:xsi="/http/ Www.w3.org/2001/XMLSchema-instance "> 4   <modelVersion>4.0.0</modelVersion> 5   <parent > 6     <groupId>me.gacl</groupId> 7     <artifactId>system-parent</artifactId> 8     <version>1.0-SNAPSHOT</version> 9   </parent>10   <artifactid>system-domain </artifactid>12   <packaging>jar</packaging>13   <name> system-domain</name>15   <url>http://maven.apache.org</url>16 </project>
Third, create Sytem-dao module

  Enter the created system-parent directory at the command line, and then execute the following command:

MVN Archetype:create-dgroupid=me.gacl-dartifactid=system-dao-darchetypeartifactid=maven-archetype-quickstart- Dinteractivemode=false

As shown in the following:

  

After the command execution, you can see that the System-dao is generated in the System-parent directory, which contains the SRC directory and the Pom.xml file. As shown in the following:

  

At the same time, the Pom.xml file in the System-parent directory automatically becomes the following:

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <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 "> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId> Me.gacl</groupid> 6 <artifactId>system-parent</artifactId> 7 <version>1.0-snapshot</ Version> 8 <packaging>pom</packaging> 9 <name>system-parent</name>11 <url>http:/ /maven.apache.org</url>12 <properties>14 <project.build.sourceencoding>utf-8</ project.build.sourceencoding>15 </properties>16 <dependencies>18 <dependency>19 < Groupid>junit</groupid>20 <artifactid>junit</artifactid>21 <version>3.8.1</versi   On>22 <scope>test</scope>23 </dependency>24</dependencies>25 <modules>26 <module>system-domain</module>27 <module>system-dao& lt;/module>28 </modules>29 </project>

Modify the Pom.xml file in the System-dao directory, and put <groupId>me.gacl</groupId> and <version>1.0-snapshot </version> Remove, plus <packaging>jar</packaging>, Because GroupID and version inherit the GroupID and version in System-parent,packaging set the package as a jar and add a dependency on the System-domain module , the revised content is as follows:

1 <?xml version= "1.0"?> 2 <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 "3 xmlns:xsi=" http://www.w3.org/ 2001/xmlschema-instance "> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupid>me .gacl</groupid> 7 <artifactId>system-parent</artifactId> 8 <version>1.0-snapshot</versi On> 9 </parent>10 <artifactid>system-dao</artifactid>12 <packaging>Jar</packaging>13 <name>system-dao</name>15 <url>http://maven.apache.org</url>16 <properties>17 <project.build.sourceencoding>utf-8</project.build.sourceencoding>18 </      Properties>19 <dependencies>20 <!--System-dao need to use classes in System-domain, so you need to add a dependency on the System-domain module-->21 <dependency>22 <groupid>me.gacl</groupid>23 <artifactid>system-domain</artifacti d>24 <version>${project.version}</version>25 </dependency>26 </dependencies>27 < /project>
Iv. Creating the System-service module

Enter the created system-parent directory at the command line, and then execute the following command:

MVN archetype:create-dgroupid=me.gacl-dartifactid=system-service-darchetypeartifactid= Maven-archetype-quickstart-dinteractivemode=false

As shown in the following:

  

After the command execution, you can see that the system-service is generated in the System-parent directory, which contains the SRC directory and the Pom.xml file. As shown in the following:

  

At the same time, the Pom.xml file in the System-parent directory automatically becomes the following:

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <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 "> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId> Me.gacl</groupid> 6 <artifactId>system-parent</artifactId> 7 <version>1.0-snapshot</ Version> 8 <packaging>pom</packaging> 9 <name>system-parent</name>11 <url>http:/ /maven.apache.org</url>12 <properties>14 <project.build.sourceencoding>utf-8</ project.build.sourceencoding>15 </properties>16 <dependencies>18 <dependency>19 < Groupid>junit</groupid>20 <artifactid>junit</artifactid>21 <version>3.8.1</versi   On>22 <scope>test</scope>23 </dependency>24</dependencies>25 <modules>26 <module>system-domain</module>27 <module>system-dao& lt;/module>28 <module>system-service</module>29 </modules>30 </project>

Modify the Pom.xml file in the System-service directory, and put <groupId>me.gacl</groupId> and <version>1.0- Snapshot</version> removed, plus <packaging>jar</packaging>, Because GroupID and version inherit the GroupID and version in System-parent,packaging set the package as a jar and add a dependency on the System-dao module , System-service relies on System-dao and system-domain, but we only need to add System-dao dependencies, because System-dao already relies on system-domain. The revised content is as follows:

1 <?xml version= "1.0"?> 2 <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 "3 xmlns:xsi=" http://www.w3.org/ 2001/xmlschema-instance "> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupid>me .gacl</groupid> 7 <artifactId>system-parent</artifactId> 8 <version>1.0-snapshot</versi On> 9 </parent>10 <artifactid>system-service</artifactid>12 <packaging>Jar</packaging>13 <name>system-service</name>15 <url>http://maven.apache.org</url> <properties>17 <project.build.sourceencoding>utf-8</project.build.sourceencoding>18 </pro Perties>19 <dependencies>20 <!--system-service dependent on System-dao and system-domain,22 but we just need to add system-da o depends, because System-dao has relied on system-domain23-->24 <dependency>25 <groupid>me.gacl</groupid&gt     ; <artifactid>system-dao</artifactid>27 <version>${project.version}</version>28 </dependency>29 </dependencies>30 </project>
V. Create a System-web module

Enter the created system-parent directory at the command line, and then execute the following command:

MVN archetype:create-dgroupid=me.gacl-dartifactid=system-web-darchetypeartifactid=Maven-archetype-webapp -dinteractivemode=false

As shown in the following:

  

After the command execution, you can see that the system-web is generated in the System-parent directory, which contains the SRC directory and the Pom.xml file. As shown in the following:

  

A simple index.jsp is also generated in the \system-web\src\main\webapp directory, as shown in:

  

Inside the content is

Web. XML generated in the System-web\src\main\webapp\web-inf directory

  

At the same time, the Pom.xml file in the System-parent directory automatically becomes the following:

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <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 "> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId> Me.gacl</groupid> 6 <artifactId>system-parent</artifactId> 7 <version>1.0-snapshot</ Version> 8 <packaging>pom</packaging> 9 <name>system-parent</name>11 <url>http:/ /maven.apache.org</url>12 <properties>14 <project.build.sourceencoding>utf-8</ project.build.sourceencoding>15 </properties>16 <dependencies>18 <dependency>19 < Groupid>junit</groupid>20 <artifactid>junit</artifactid>21 <version>3.8.1</versi   On>22 <scope>test</scope>23 </dependency>24</dependencies>25 <modules>26 <module>system-domain</module>27 <module>system-dao& lt;/module>28 <module>system-service</module>29 <module>system-web</module>30 </mo Dules>31 </project>

Modify the Pom.xml file in the System-web directory, and put <groupId>me.gacl</groupId> and <version>1.0-snapshot </version> is removed because GroupID and version inherit GroupID and version in System-parent, while Add a dependency on the System-service module with the following modified content:

1 <?xml version= "1.0"?> 2 <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 "3 xmlns:xsi=" http://www.w3.org/ 2001/xmlschema-instance "> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupid>me .gacl</groupid> 7 <artifactId>system-parent</artifactId> 8 <version>1.0-snapshot</versi On> 9 </parent>10 <artifactid>system-web</artifactid>12 <packaging>War</packaging>13 <name>system-web Maven webapp</name>15 <url>http://maven.apache.org</u       Rl>16 <dependencies>17 <!--system-web dependent system-service19-->20 <dependency>21 <groupid>me.gacl</groupid>22 <artifactid>system-service</artifactid>23 <version >${project.version}</version>24 </dependency>25 </dependencies>26 <build>27 <fin alname>system-web</finalname>28 </build>29 </project>

Note thatthe Web project is packaged in a war.

Vi. compiling and running the project

  After the five steps above, the relevant modules are all created and how to run them. Since the System-web module is finally running, we add jetty support to the module to facilitate the test run. The pom.xml for modifying the System-web project are as follows:

 1 <?xml version= "1.0"?> 2 <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 "3 xmlns:xsi=" http://www.w3.org/ 2001/xmlschema-instance "> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupid>me .gacl</groupid> 7 <artifactId>system-parent</artifactId> 8 <version>1.0-snapshot</versi On> 9 </parent>10 <artifactid>system-web</artifactid>12 <packaging>war</packaging& gt;13 <name>system-web Maven webapp</name>15 <url>http://maven.apache.org</url>16 <d Ependencies>17 <!--system-web dependent system-service19-->20 <dependency>21 <groupid& Gt;me.gacl</groupid>22 <artifactid>system-service</artifactid>23 <version>${project.ve Rsion}</version>24 </dependency></dependencies>26 <build>27 <finalname>system-web</finalname>28 <plugins>29             <!--configuration Jetty plug-in-->30 <plugin>31 <groupid>org.mortbay.jetty</groupid>32 <artifactid>maven-jetty-plugin</artifactid>33 </plugin>34 </plugins>35 &lt ;/build>36 </project>

Enter the System-parent directory at the command line, and then execute the following command:

MVN clean Install

As shown in the following:

  

  

After the command is executed, the target directory is added to the System-web directory with System-web.war as shown:

  

command line into the Sytem-web directory, execute the following command, start jetty

MVN Jetty:run

As shown in the following:

  

  

After starting the jetty server, access the http://localhost:8080/system-web/running as shown in the results:

  

Vii. Import into Eclipse for development

The procedure is as follows:

  

  

  

  

  

Maven Learning Summary (eight)-building multi-module projects with Maven

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.