前言
承接前文《Spring+SpringMVC+MyBatis+easyUI整合基礎篇(五)講一下maven》,本篇所講述的是如何使用maven與原ssm項目整合,使得一個普通的JavaWeb項目變為由maven管理的規范化項目,使項目變得簡單。如果你已經安裝maven並在開發軟件中配置好maven后,即可開始體驗maven帶給你的便利,當然,僅僅一個項目是不可能讓你迅速喜歡上maven的,這一篇只是上車而已,慢慢來。
因為已經有了項目代碼,所以新建maven步驟這里可以忽略的看一下,你可以自行下載代碼直接導入到工程即可。
第一階段余下的文章中所有關於bug修復、功能增加、代碼修改都會在此maven項目中進行,原來的項目不會繼續更新了。
1、打開編輯器,File -> New -> Project, 新建maven項目。
2、如圖:

選擇創建maven項目,配置JDK,第3步中,Create from archetype這個選項是一定要勾選的,不然無法進入下一步,第四步也要注意,選擇
org.apache.maven.archetypes:maven-archetype-webapp,因為可能和其他選項相似,一定要看清楚。
3、項目命名

GroupID是項目組織唯一的標識符,實際對應JAVA的包的結構,是main目錄里java的目錄結構,為了和ssm-demo項目區分開來,我們就命名為com.ssm.maven.core。
ArtifactID就是項目的唯一的標識符,實際對應項目的名稱,就是項目根目錄的名稱,與maven整合,因此命名為ssm-maven。
Version是項目版本號,idea已經自動生成了。
以上三個配置項的命名都可以根據個人習慣或者公司要求來做,是一個較為主觀的事情。

選擇已經配置好的maven及目錄即可,下面Properties即為上一步里設置的參數。
這里有一個參數需要注意,archetypeCatalog參數,詳細說明可以看一下我寫的這篇文章《解決新建maven項目速度慢的問題》。
這里有一個參數需要注意,archetypeCatalog參數,詳細說明可以看一下我寫的這篇文章《解決新建maven項目速度慢的問題》。
5、存儲位置設置

項目在本機的存儲目錄設置完之后,點擊Finish即可。
6、mvn生成項目
全部設置成功后,等待mvn將項目架構生成即可,如下,控制台中出現提示信息即生成項目成功。

初始的項目結構如下:

7、代碼整合
原項目結構如下:

那么,將原項目src目錄下中的java包復制到ssm-maven項目的main目錄下,原項目中的配置文件復制到resources文件夾下,
mappers文件也復制到resources下,WebRoot中的文件復制到webapp文件夾下,得到如下目錄結構的maven項目:

其實,目錄結構的差別倒是不大,主要在於pom.xml文件,整個項目的描述文件及相關配置都在此文件中。兩個項目的下載地址分別為ssm-demo和ssm-maven,可以下載到本地對比一下,看一看其中的差異。最明顯的差異就是路徑的差異,因為要區分兩個項目,所以對原先的包名進行重新命名了。到這里,普通JavaWeb項目改造為maven項目就完成了,可以自己動動手試一下,也可以直接導入源碼。
pom文件
pom.xml配置如下:

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/maven-v4_0_0.xsd">
3 <modelVersion>4.0.0</modelVersion>
4 <groupId>com.ssm.maven.core</groupId>
5 <artifactId>ssm-maven</artifactId>
6 <packaging>war</packaging>
7 <version>1.0-SNAPSHOT</version>
8 <name>ssm-maven</name>
9 <url>http://maven.apache.org</url>
10
11 <properties>
12 <!-- 數據庫相關版本 -->
13 <jdbc.driver.version>5.1.25</jdbc.driver.version>
14 <mybatis.version>3.2.5</mybatis.version>
15 <mybatis-spring.version>1.2.2</mybatis-spring.version>
16 <!-- spring版本 -->
17 <spring.version>4.2.4.RELEASE</spring.version>
18 <!-- encoding -->
19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20 <!-- autoconfig -->
21 <autoconfig-plugin-version>1.2</autoconfig-plugin-version>
22 <maven.test.skip>true</maven.test.skip>
23 </properties>
24
25 <dependencies>
26 <dependency>
27 <groupId>junit</groupId>
28 <artifactId>junit</artifactId>
29 <version>4.9</version>
30 <scope>test</scope>
31 </dependency>
32 <dependency>
33 <groupId>org.springframework</groupId>
34 <artifactId>spring-test</artifactId>
35 <version>${spring.version}</version>
36 <scope>test</scope>
37 </dependency>
38 <dependency>
39 <groupId>commons-logging</groupId>
40 <artifactId>commons-logging</artifactId>
41 <version>1.1.3</version>
42 </dependency>
43 <dependency>
44 <groupId>commons-collections</groupId>
45 <artifactId>commons-collections</artifactId>
46 <version>3.2.1</version>
47 </dependency>
48 <dependency>
49 <groupId>commons-io</groupId>
50 <artifactId>commons-io</artifactId>
51 <version>2.4</version>
52 </dependency>
53 <dependency>
54 <groupId>commons-lang</groupId>
55 <artifactId>commons-lang</artifactId>
56 <version>2.6</version>
57 </dependency>
58
59 <!-- Begin: 數據庫依賴包 -->
60 <dependency>
61 <groupId>org.mybatis</groupId>
62 <artifactId>mybatis</artifactId>
63 <version>${mybatis.version}</version>
64 </dependency>
65 <dependency>
66 <groupId>org.mybatis</groupId>
67 <artifactId>mybatis-spring</artifactId>
68 <version>${mybatis-spring.version}</version>
69 </dependency>
70 <dependency>
71 <groupId>mysql</groupId>
72 <artifactId>mysql-connector-java</artifactId>
73 <version>${jdbc.driver.version}</version>
74 <scope>runtime</scope>
75 </dependency>
76 <!-- End: 數據庫依賴包 -->
77
78 <!-- Begin: 日志依賴包 -->
79 <dependency>
80 <groupId>org.slf4</groupId>
81 <artifactId>slf4j-api</artifactId>
82 <version>20160310</version>
83 </dependency>
84 <dependency>
85 <groupId>org.slf4j</groupId>
86 <artifactId>slf4j-log4j12</artifactId>
87 <version>1.7.7</version>
88 </dependency>
89 <dependency>
90 <groupId>log4j</groupId>
91 <artifactId>log4j</artifactId>
92 <version>1.2.16</version>
93 </dependency>
94 <!-- End: 日志依賴包 -->
95
96 <!-- Begin: aspectj相關jar包-->
97 <dependency>
98 <groupId>org.aspectj</groupId>
99 <artifactId>aspectjrt</artifactId>
100 <version>1.7.4</version>
101 </dependency>
102 <dependency>
103 <groupId>org.aspectj</groupId>
104 <artifactId>aspectjweaver</artifactId>
105 <version>1.7.4</version>
106 </dependency>
107 <!-- End: aspectj相關jar包-->
108
109 <!-- Begin: spring依賴 -->
110 <dependency>
111 <groupId>org.springframework</groupId>
112 <artifactId>spring-context-support</artifactId>
113 <version>${spring.version}</version>
114 </dependency>
115 <dependency>
116 <groupId>org.springframework</groupId>
117 <artifactId>spring-jdbc</artifactId>
118 <version>${spring.version}</version>
119 </dependency>
120 <dependency>
121 <groupId>org.springframework</groupId>
122 <artifactId>spring-tx</artifactId>
123 <version>${spring.version}</version>
124 </dependency>
125 <dependency>
126 <groupId>org.springframework</groupId>
127 <artifactId>spring-webmvc</artifactId>
128 <version>${spring.version}</version>
129 </dependency>
130 <!-- End: spring依賴 -->
131
132 <dependency>
133 <groupId>javax.servlet</groupId>
134 <artifactId>javax.servlet-api</artifactId>
135 <version>3.1.0</version>
136 <scope>provided</scope>
137 </dependency>
138 <dependency>
139 <groupId>javax.servlet.jsp</groupId>
140 <artifactId>jsp-api</artifactId>
141 <version>2.2</version>
142 </dependency>
143 <dependency>
144 <groupId>javax.servlet</groupId>
145 <artifactId>jstl</artifactId>
146 <version>1.2</version>
147 </dependency>
148
149 <dependency>
150 <groupId>net.sf.json-lib</groupId>
151 <artifactId>json-lib</artifactId>
152 <version>2.2.3</version>
153 <classifier>jdk15</classifier>
154 </dependency>
155
156 <dependency>
157 <groupId>com.fasterxml.jackson.core</groupId>
158 <artifactId>jackson-databind</artifactId>
159 <version>2.5.3</version>
160 </dependency>
161
162 <dependency>
163 <groupId>com.alibaba</groupId>
164 <artifactId>fastjson</artifactId>
165 <version>1.2.4</version>
166 </dependency>
167
168 <dependency>
169 <groupId>commons-codec</groupId>
170 <artifactId>commons-codec</artifactId>
171 <version>1.4</version>
172 </dependency>
173
174 </dependencies>
175
176 <build>
177 <finalName>ssm-maven</finalName>
178 <plugins>
179
180 <plugin>
181 <groupId>org.apache.maven.plugins</groupId>
182 <artifactId>maven-compiler-plugin</artifactId>
183 <version>3.2</version>
184 <configuration>
185 <source>1.7</source>
186 <target>1.7</target>
187 </configuration>
188 </plugin>
189
190 <plugin>
191 <groupId>org.apache.tomcat.maven</groupId>
192 <artifactId>tomcat7-maven-plugin</artifactId>
193 <version>2.2</version>
194 <configuration>
195 <port>8080</port>
196 <charset>${project.build.sourceEncoding}</charset>
197 <server>tomcat7</server>
198 </configuration>
199 </plugin>
200
201 </plugins>
202 </build>
203
204 </project>
如果有什么問題的話,留言或者發私信即可。