Maven study concluded - built with Maven multi-module project

In the usual Javaweb project development in order to facilitate the maintenance of late, we usually stratified development, the most common of which is divided into the domain (domain model layer), dao (database access layer), service (business logic layer), web ( presentation layer), then this hierarchical responsibilities between the various layers would be more clear, post-maintenance it is also relatively easy, today we are using Maven to build more of the various layers.

Project is structured as follows:

system-parent

| ---- pom.xml

|----system-domain

| ---- pom.xml

| ---- system-dao

| ---- pom.xml

|----system-service

| ---- pom.xml

|----system-web

| ---- pom.xml

First, create a system-parent project

Create a system-parent, to give each sub-module inheritance.

Enter the command line, enter the following command:

mvn archetype:create 
-DgroupId=me.gacl 
-DartifactId=system-parent 
-DarchetypeArtifactId=maven-archetype-quickstart 
-DinteractiveMode=false
复制代码

As shown below:

Maven study concluded - built with Maven multi-module project

After executing the command completion can be seen in the current directory: generating a system-parent directory (C Documents and SettingsAdministrator), which has a pom.xml src directory and a file, as shown below:

Maven study concluded - built with Maven multi-module project

The src folder is deleted , and then modify pom.xml file, the <packaging> jar </ packaging> modify <Packaging> POM </ Packaging> , indicating that it is a POM inherited module, the modified content as follows:

<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>me.gacl</groupId>
 <artifactId>system-parent</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>pom</packaging>
 <name>system-parent</name>
 <url>http://maven.apache.org</url>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>
</project>
复制代码

Second, create a sytem-domain module

Enter on the command line to create a good system-parent directory, and then execute the following command:

mvn archetype:create 
-DgroupId=me.gacl 
-DartifactId=system-domain 
-DarchetypeArtifactId=maven-archetype-quickstart 
-DinteractiveMode=false
复制代码

As shown below:

Maven study concluded - built with Maven multi-module project

After completion of the command can be seen in the generated system-domain system-parent directory, and a directory which contains src pom.xml file. As shown below:

Maven study concluded - built with Maven multi-module project

Maven study concluded - built with Maven multi-module project

Meanwhile, pom.xml file system-parent directory automatically added the following content:

<modules>
 <module>system-domain</module>
</modules>
复制代码

At this time, system-parent pom.xml file is as follows:

<?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>me.gacl</groupId>
 <artifactId>system-parent</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>pom</packaging>
 <name>system-parent</name>
 <url>http://maven.apache.org</url>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>
 <modules>
 <module>system-domain</module>
 </modules>
</project>
复制代码

Modify pom.xml file system-domain directory, the <groupId> me.gacl </ groupId> and <version> 1.0-SNAPSHOT </ version> removed, together with <Packaging> JAR </ Packaging> , and because of groupId version will inherit the system-parent and groupId version, packaging is provided a packaging jar, pom.xml file after the amended as follows:

<?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>me.gacl</groupId>
 <artifactId>system-parent</artifactId>
 <version>1.0-SNAPSHOT</version>
 </parent>
 
 <artifactId>system-domain</artifactId>
 <packaging>jar</packaging>
 
 <name>system-domain</name>
 <url>http://maven.apache.org</url>
</project>
复制代码

Third, create a sytem-dao module

Enter on the command line to create a good system-parent directory, and then execute the following command:

mvn archetype:create 
-DgroupId=me.gacl 
-DartifactId=system-dao 
-DarchetypeArtifactId=maven-archetype-quickstart 
-DinteractiveMode=false
复制代码

As shown below: 

Maven study concluded - built with Maven multi-module project

After completion of the command can be seen in the generated system-dao system-parent directory, and a directory which contains src pom.xml file. As shown below:

Maven study concluded - built with Maven multi-module project

Maven study concluded - built with Maven multi-module project

Meanwhile, pom.xml file system-parent directory automatically become the following:

<?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>me.gacl</groupId>
 <artifactId>system-parent</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>pom</packaging>
 <name>system-parent</name>
 <url>http://maven.apache.org</url>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>
 <modules>
 <module>system-domain</module>
 <module>system-dao</module>
 </modules>
</project>
复制代码

Modify pom.xml file system-dao directory ,, the <groupId> me.gacl </ groupId> and <version> 1.0-SNAPSHOT </ version> removed, together with <Packaging> JAR </ Packaging> , because groupId and inherit system-parent version of groupId and version, packaging JAR set to a packaging, while adding dependency on system-domain module , the modified content as follows:

<?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>me.gacl</groupId>
 <artifactId>system-parent</artifactId>
 <version>1.0-SNAPSHOT</version>
 </parent>
 <artifactId>system-dao</artifactId>
 <packaging>jar</packaging>
 <name>system-dao</name>
 <url>http://maven.apache.org</url>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
 <!--system-dao需要使用到system-domain中的类,所以需要添加对system-domain模块的依赖-->
 <dependency>
 <groupId>me.gacl</groupId>
 <artifactId>system-domain</artifactId>
 <version>${project.version}</version>
 </dependency>
 </dependencies>
</project>
复制代码

Fourth, create a system-service module

Enter on the command line to create a good system-parent directory, and then execute the following command:

mvn archetype:create 
-DgroupId=me.gacl 
-DartifactId=system-service 
-DarchetypeArtifactId=maven-archetype-quickstart 
-DinteractiveMode=false
复制代码

As shown below:

Maven study concluded - built with Maven multi-module project

After completion of the command can be seen in the generated system-service system-parent directory, and a directory which contains src pom.xml file. As shown below:

Maven study concluded - built with Maven multi-module project

Maven study concluded - built with Maven multi-module project

Meanwhile, pom.xml file system-parent directory automatically become the following:

<?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>me.gacl</groupId>
 <artifactId>system-parent</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>pom</packaging>
 <name>system-parent</name>
 <url>http://maven.apache.org</url>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>
 <modules>
 <module>system-domain</module>
 <module>system-dao</module>
 <module>system-service</module>
 </modules>
</project>
复制代码

Modify pom.xml file system-service directory ,, the <groupId> me.gacl </ groupId> and <version> 1.0-SNAPSHOT </ version> removed, together with <Packaging> JAR </ Packaging> , because groupId and version will inherit the system-parent groupId and version, packaging set are packaged as jar, while adding dependency on system-dao modules , system-service-dependent system-dao and system-domain, but we just need to add a system-dao dependency can, as has been dependent on the system-dao system-domain. The modification is as follows:

<?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>me.gacl</groupId>
 <artifactId>system-parent</artifactId>
 <version>1.0-SNAPSHOT</version>
 </parent>
 <artifactId>system-service</artifactId>
 <packaging>jar</packaging>
 
 <name>system-service</name>
 <url>http://maven.apache.org</url>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
 <!--
 system-service依赖system-dao和system-domain,
 但是我们只需添加system-dao的依赖即可,因为system-dao已经依赖了system-domain
 -->
 <dependency>
 <groupId>me.gacl</groupId>
 <artifactId>system-dao</artifactId>
 <version>${project.version}</version>
 </dependency>
 </dependencies>
</project>
复制代码

Fifth, create system-web module

Enter on the command line to create a good system-parent directory, and then execute the following command:

mvn archetype:create 
-DgroupId=me.gacl 
-DartifactId=system-web 
-DarchetypeArtifactId=maven-archetype-webapp 
-DinteractiveMode=false
复制代码

As shown below: 

Maven study concluded - built with Maven multi-module project

After completion of the command can be seen in the generated system-web system-parent directory, and a directory which contains src pom.xml file. As shown below:

Maven study concluded - built with Maven multi-module project

Maven study concluded - built with Maven multi-module project

In the system-websrcmainwebapp directory also generates a simple index.jsp, as shown below:

Maven study concluded - built with Maven multi-module project

Content which is

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
复制代码

system-websrcmainwebappWEB-INF directory generated web.xml

Maven study concluded - built with Maven multi-module project

Meanwhile, pom.xml file system-parent directory automatically become the following:

<?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>me.gacl</groupId>
 <artifactId>system-parent</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>pom</packaging>
 <name>system-parent</name>
 <url>http://maven.apache.org</url>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>
 <modules>
 <module>system-domain</module>
 <module>system-dao</module>
 <module>system-service</module>
 <module>system-web</module>
 </modules>
</project>
复制代码

Modify pom.xml file system-web directory ,, the <groupId> me.gacl </ groupId> and <version> 1.0-SNAPSHOT </ version> removed, as inherit groupId and version of system-parent and groupId Version, while adding dependency on system-service module , the modified content as follows:

<?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>me.gacl</groupId>
 <artifactId>system-parent</artifactId>
 <version>1.0-SNAPSHOT</version>
 </parent>
 <artifactId>system-web</artifactId>
 <packaging>war</packaging>
 
 <name>system-web Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <dependencies>
 <!--
 system-web依赖system-service
 -->
 <dependency>
 <groupId>me.gacl</groupId>
 <artifactId>system-service</artifactId>
 <version>${project.version}</version>
 </dependency>
 </dependencies>
 <build>
 <finalName>system-web</finalName>
 </build>
</project>
复制代码

Note that a packaging web projects is the WAR .

Sixth, compile and run the project

After five steps above, all of the relevant module is created, how it up and running. As the final run is system-web module, so we added support for the jetty module, convenient test run. Modify system-web pom.xml following items:

<?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>me.gacl</groupId>
 <artifactId>system-parent</artifactId>
 <version>1.0-SNAPSHOT</version>
 </parent>
 <artifactId>system-web</artifactId>
 <packaging>war</packaging>
 
 <name>system-web Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <dependencies>
 <!--
 system-web依赖system-service
 -->
 <dependency>
 <groupId>me.gacl</groupId>
 <artifactId>system-service</artifactId>
 <version>${project.version}</version>
 </dependency>
 </dependencies>
 <build>
 <finalName>system-web</finalName>
 <plugins>
 <!--配置Jetty插件-->
 <plugin>
 <groupId>org.mortbay.jetty</groupId>
 <artifactId>maven-jetty-plugin</artifactId>
 </plugin>
 </plugins>
 </build>
</project>
复制代码

At the command line to enter the system-parent directory, and then execute the following command:

mvn clean install
复制代码

As shown below:

Maven study concluded - built with Maven multi-module project

Maven study concluded - built with Maven multi-module project

After the command is executed, the system-web in a multi-target directory directory, which have system-web.war, as shown below:

Maven study concluded - built with Maven multi-module project

Command line into the sytem-web directory, execute the following command to start the jetty

mvn jetty:run
复制代码

As shown below:

Maven study concluded - built with Maven multi-module project

Maven study concluded - built with Maven multi-module project

After starting jetty server, access http: // localhost: 8080 / system-web / operation results as shown below:

Maven study concluded - built with Maven multi-module project

Seven introduced into the Eclipse development

Procedure as follows:

Maven study concluded - built with Maven multi-module project

Maven study concluded - built with Maven multi-module project

Maven study concluded - built with Maven multi-module project

Maven study concluded - built with Maven multi-module project

Maven study concluded - built with Maven multi-module project


Guess you like

Origin juejin.im/post/5d8f4c8451882509453c2116
Recommended