SSM001/构建maven多模块项目

一。Idea构建maven多模块项目

1。创建maven项目--创建父模块

【1】。File->New->Module...

【2】。点击next,填写:GroupId,ArtifactId和Version

注:ArtifactId即为项目名字。

【3】。点击next,添加参数archetypeCatalog=internal(不加该参数,在maven生成骨架时会非常慢)

注:

archetypeCatalog表示插件使用的archetype元数据,不加这个参数时默认为remote,local,即中央仓库archetype元数据,
由于中央仓库的archetype太多了所以导致很慢,指定internal来表示仅使用内部元数据。

【4】点击next,填写module name(模块名称)->finish

【5】。项目结构如下所示:

2。创建module子模块

【1】右击项目-》new Module..

【2】新建Module同1创建父模块。

修改点:

(1)。ArtifactId:ssm-web

(2)。Modual name:ssm-web

最终项目架构如下所示:

3。每个模块中依赖pom.xml配置

maven模块结构图:

---web-ssm

    |--pom.xml(pom)

    |--ssm-web

      |--pom.xml(war)

    |--ssm-service

      |--pom.xml(jar)

    |--ssm-dao

      |--pom.xml(jar)

    |--ssm-model

      |--pom.xml(jar)

注意:括号中标识的是模块的打包(packaging类型)。父项目只能为pom,子项目根据包含内容具体考虑打jar/war包。

maven依赖传递性:

|-ssm-dao模块:负责与数据库交互,持久层。依赖于model层(ssm-model)

         ssm-dao->依赖->ssm-model

|-ssm-service模块:负责业务逻辑处理,事务控制层。调用dao传递处理结果。依赖于dao层(ssm-dao)。

                             而dao依赖于model层。所以service依赖于model层(ssm-model)

        ssm-service->依赖->ssm-dao->依赖->ssm-model

|-ssm-web模块:负责与客户端交互,控制层。主要为springmvc的controller类/struts的action类。

                         依赖于service层。依赖于dao层,依赖于model层。(依赖传递性)

        ssm-web->依赖->ssm-service->依赖->ssm-dao->依赖->ssm-model

|-ssm-model模块:独立模块,不依赖于任何其他模块

上述:除非配置了特殊的依赖scope。否则依赖均具有传递性。

【1】。ssm-web子模块

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>web-ssm</artifactId>
 7         <groupId>com.yufeng.web</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <groupId>com.yufeng.web</groupId>
13     <artifactId>ssm-web</artifactId>
14     <packaging>war</packaging>
15 
16     <name>ssm-web Maven Webapp</name>
17     <!-- FIXME change it to the project's website -->
18     <url>http://www.example.com</url>
19 
20     <properties>
21         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22         <maven.compiler.source>1.7</maven.compiler.source>
23         <maven.compiler.target>1.7</maven.compiler.target>
24     </properties>
25 
26     <dependencies>
27         <dependency>
28             <groupId>junit</groupId>
29             <artifactId>junit</artifactId>
30             <version>4.11</version>
31             <scope>test</scope>
32         </dependency>
33         <!-- web层添加对model的依赖-->
34         <dependency>
35             <groupId>com.yufeng.web</groupId>
36             <artifactId>ssm-model</artifactId>
37             <version>${project.version}</version>
38         </dependency>
39         <!-- web层添加对service的依赖-->
40         <dependency>
41             <groupId>com.yufeng.web</groupId>
42             <artifactId>ssm-service</artifactId>
43             <version>${project.version}</version>
44         </dependency>
45         <!-- web层添加对dao的依赖-->
46         <dependency>
47             <groupId>com.yufeng.web</groupId>
48             <artifactId>ssm-dao</artifactId>
49             <version>${project.version}</version>
50         </dependency>
51     </dependencies>
52 
53     <build>
54         <finalName>ssm-web</finalName>
55         <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
56             <plugins>
57                 <plugin>
58                     <artifactId>maven-clean-plugin</artifactId>
59                     <version>3.0.0</version>
60                 </plugin>
61                 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
62                 <plugin>
63                     <artifactId>maven-resources-plugin</artifactId>
64                     <version>3.0.2</version>
65                 </plugin>
66                 <plugin>
67                     <artifactId>maven-compiler-plugin</artifactId>
68                     <version>3.7.0</version>
69                 </plugin>
70                 <plugin>
71                     <artifactId>maven-surefire-plugin</artifactId>
72                     <version>2.20.1</version>
73                 </plugin>
74                 <plugin>
75                     <artifactId>maven-war-plugin</artifactId>
76                     <version>3.2.0</version>
77                 </plugin>
78                 <plugin>
79                     <artifactId>maven-install-plugin</artifactId>
80                     <version>2.5.2</version>
81                 </plugin>
82                 <plugin>
83                     <artifactId>maven-deploy-plugin</artifactId>
84                     <version>2.8.2</version>
85                 </plugin>
86             </plugins>
87         </pluginManagement>
88     </build>
89 </project>
View Code

【2】。ssm-service子模块

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>web-ssm</artifactId>
 7         <groupId>com.yufeng.web</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <groupId>com.yufeng.web</groupId>
13     <artifactId>ssm-service</artifactId>
14     <packaging>jar</packaging>
15 
16     <name>ssm-service Maven Webapp</name>
17     <!-- FIXME change it to the project's website -->
18     <url>http://www.example.com</url>
19 
20     <properties>
21         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22         <maven.compiler.source>1.7</maven.compiler.source>
23         <maven.compiler.target>1.7</maven.compiler.target>
24     </properties>
25 
26     <dependencies>
27         <dependency>
28             <groupId>junit</groupId>
29             <artifactId>junit</artifactId>
30             <version>4.11</version>
31             <scope>test</scope>
32         </dependency>
33         <!-- service层添加对model的依赖-->
34         <dependency>
35             <groupId>com.yufeng.web</groupId>
36             <artifactId>ssm-model</artifactId>
37             <version>${project.version}</version>
38         </dependency>
39         <!-- service层添加对dao的依赖-->
40         <dependency>
41             <groupId>com.yufeng.web</groupId>
42             <artifactId>ssm-dao</artifactId>
43             <version>${project.version}</version>
44         </dependency>
45     </dependencies>
46 
47     <build>
48         <finalName>ssm-service</finalName>
49         <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
50             <plugins>
51                 <plugin>
52                     <artifactId>maven-clean-plugin</artifactId>
53                     <version>3.0.0</version>
54                 </plugin>
55                 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
56                 <plugin>
57                     <artifactId>maven-resources-plugin</artifactId>
58                     <version>3.0.2</version>
59                 </plugin>
60                 <plugin>
61                     <artifactId>maven-compiler-plugin</artifactId>
62                     <version>3.7.0</version>
63                 </plugin>
64                 <plugin>
65                     <artifactId>maven-surefire-plugin</artifactId>
66                     <version>2.20.1</version>
67                 </plugin>
68                 <plugin>
69                     <artifactId>maven-war-plugin</artifactId>
70                     <version>3.2.0</version>
71                 </plugin>
72                 <plugin>
73                     <artifactId>maven-install-plugin</artifactId>
74                     <version>2.5.2</version>
75                 </plugin>
76                 <plugin>
77                     <artifactId>maven-deploy-plugin</artifactId>
78                     <version>2.8.2</version>
79                 </plugin>
80             </plugins>
81         </pluginManagement>
82     </build>
83 </project>
View Code

【3】。ssm-dao子模块

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>web-ssm</artifactId>
 7         <groupId>com.yufeng.web</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <groupId>com.yufeng.web</groupId>
13     <artifactId>ssm-dao</artifactId>
14     <packaging>jar</packaging>
15 
16     <name>ssm-dao Maven Webapp</name>
17     <!-- FIXME change it to the project's website -->
18     <url>http://www.example.com</url>
19 
20     <properties>
21         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22         <maven.compiler.source>1.7</maven.compiler.source>
23         <maven.compiler.target>1.7</maven.compiler.target>
24     </properties>
25 
26     <dependencies>
27         <dependency>
28             <groupId>junit</groupId>
29             <artifactId>junit</artifactId>
30             <version>4.11</version>
31             <scope>test</scope>
32         </dependency>
33         <!-- dao层添加对model的依赖-->
34         <dependency>
35             <groupId>com.yufeng.web</groupId>
36             <artifactId>ssm-model</artifactId>
37             <version>${project.version}</version>
38         </dependency>
39     </dependencies>
40 
41     <build>
42         <finalName>ssm-dao</finalName>
43         <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
44             <plugins>
45                 <plugin>
46                     <artifactId>maven-clean-plugin</artifactId>
47                     <version>3.0.0</version>
48                 </plugin>
49                 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
50                 <plugin>
51                     <artifactId>maven-resources-plugin</artifactId>
52                     <version>3.0.2</version>
53                 </plugin>
54                 <plugin>
55                     <artifactId>maven-compiler-plugin</artifactId>
56                     <version>3.7.0</version>
57                 </plugin>
58                 <plugin>
59                     <artifactId>maven-surefire-plugin</artifactId>
60                     <version>2.20.1</version>
61                 </plugin>
62                 <plugin>
63                     <artifactId>maven-war-plugin</artifactId>
64                     <version>3.2.0</version>
65                 </plugin>
66                 <plugin>
67                     <artifactId>maven-install-plugin</artifactId>
68                     <version>2.5.2</version>
69                 </plugin>
70                 <plugin>
71                     <artifactId>maven-deploy-plugin</artifactId>
72                     <version>2.8.2</version>
73                 </plugin>
74             </plugins>
75         </pluginManagement>
76     </build>
77 </project>
View Code

【4】。ssm-model子模块

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>web-ssm</artifactId>
 7         <groupId>com.yufeng.web</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <groupId>com.yufeng.web</groupId>
13     <artifactId>ssm-model</artifactId>
14     <packaging>jar</packaging>
15 
16     <name>ssm-model Maven Webapp</name>
17     <!-- FIXME change it to the project's website -->
18     <url>http://www.example.com</url>
19 
20     <properties>
21         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22         <maven.compiler.source>1.7</maven.compiler.source>
23         <maven.compiler.target>1.7</maven.compiler.target>
24     </properties>
25 
26     <dependencies>
27         <dependency>
28             <groupId>junit</groupId>
29             <artifactId>junit</artifactId>
30             <version>4.11</version>
31             <scope>test</scope>
32         </dependency>
33     </dependencies>
34 
35     <build>
36         <finalName>ssm-model</finalName>
37         <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
38             <plugins>
39                 <plugin>
40                     <artifactId>maven-clean-plugin</artifactId>
41                     <version>3.0.0</version>
42                 </plugin>
43                 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
44                 <plugin>
45                     <artifactId>maven-resources-plugin</artifactId>
46                     <version>3.0.2</version>
47                 </plugin>
48                 <plugin>
49                     <artifactId>maven-compiler-plugin</artifactId>
50                     <version>3.7.0</version>
51                 </plugin>
52                 <plugin>
53                     <artifactId>maven-surefire-plugin</artifactId>
54                     <version>2.20.1</version>
55                 </plugin>
56                 <plugin>
57                     <artifactId>maven-war-plugin</artifactId>
58                     <version>3.2.0</version>
59                 </plugin>
60                 <plugin>
61                     <artifactId>maven-install-plugin</artifactId>
62                     <version>2.5.2</version>
63                 </plugin>
64                 <plugin>
65                     <artifactId>maven-deploy-plugin</artifactId>
66                     <version>2.8.2</version>
67                 </plugin>
68             </plugins>
69         </pluginManagement>
70     </build>
71 </project>
View Code

【5】。web-ssm父模块(聚合子模块modules)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5   <modelVersion>4.0.0</modelVersion>
 6 
 7   <groupId>com.yufeng.web</groupId>
 8   <artifactId>web-ssm</artifactId>
 9   <version>1.0-SNAPSHOT</version>
10     <!--注意:父模块的packaging是pom ,表示该工程为pom类型。子模块的packaging可以为jar/war-->
11   <packaging>pom</packaging>
12 
13     <!-- maven聚合,聚合子模块。<module>配置的是子模块的artifactId唯一.-->
14   <modules>
15     <module>ssm-web</module>
16     <module>ssm-service</module>
17     <module>ssm-dao</module>
18     <module>ssm-model</module>
19   </modules>
20 
21   <name>web-ssm Maven Webapp</name>
22   <!-- FIXME change it to the project's website -->
23   <url>http://www.example.com</url>
24 
25   <properties>
26       <!-- 项目统一编码格式-->
27     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28     <maven.compiler.source>1.7</maven.compiler.source>
29     <maven.compiler.target>1.7</maven.compiler.target>
30   </properties>
31 
32   <dependencies>
33     <dependency>
34       <groupId>junit</groupId>
35       <artifactId>junit</artifactId>
36       <version>4.11</version>
37       <scope>test</scope>
38     </dependency>
39   </dependencies>
40 
41   <build>
42     <finalName>web-ssm</finalName>
43     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
44       <plugins>
45         <plugin>
46           <artifactId>maven-clean-plugin</artifactId>
47           <version>3.0.0</version>
48         </plugin>
49         <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
50         <plugin>
51           <artifactId>maven-resources-plugin</artifactId>
52           <version>3.0.2</version>
53         </plugin>
54         <plugin>
55           <artifactId>maven-compiler-plugin</artifactId>
56           <version>3.7.0</version>
57         </plugin>
58         <plugin>
59           <artifactId>maven-surefire-plugin</artifactId>
60           <version>2.20.1</version>
61         </plugin>
62         <plugin>
63           <artifactId>maven-war-plugin</artifactId>
64           <version>3.2.0</version>
65         </plugin>
66         <plugin>
67           <artifactId>maven-install-plugin</artifactId>
68           <version>2.5.2</version>
69         </plugin>
70         <plugin>
71           <artifactId>maven-deploy-plugin</artifactId>
72           <version>2.8.2</version>
73         </plugin>
74       </plugins>
75     </pluginManagement>
76   </build>
77 </project>
View Code

上述:Maven多模块项目已经搭建完成。

下述:整合spring+springmvc+mybatis。

参考网址:http://www.imooc.com/article/19789

原文地址:https://www.cnblogs.com/kaixinyufeng/p/9109085.html