用eclipse插件创建一个Maven项目

本文预计阅读时间 40 分钟

首先创建一个Maven的Project如下图

 

用eclipse插件创建一个Maven项目

 

我们勾选上Create a simple project (不使用骨架)

 

用eclipse插件创建一个Maven项目

 

 

这里的Packing 选择 war的形式

 

用eclipse插件创建一个Maven项目

 

由于packing是war包,那么下面也就多出了webapp的目录

用eclipse插件创建一个Maven项目

 

由于我们的项目要使用eclipse发布到tomcat下面,这里我们需要先把项目转成dynamic web project

 

在我们的项目上点击右键,选择properties 并找到 Project Facets ,并点击Convert to faceted form…   如下图:

 

用eclipse插件创建一个Maven项目

 

 

 

然后勾选Dynamic Web Module 并点击ok  如下图:(3.0只有tomcat7才支持)

 

接下来观察我们的项目结构,多了一个web content目录

用eclipse插件创建一个Maven项目

 

 

虽然此时我们可以发布到tomcat中,但这不符合maven的结构,我们还要做如下修改

把上图WebContent下面两个目录 META-INF ,WEB-INF 直接剪切到src/main/webapp目录下,并删掉WebContent目录,那么现在的项目结构如下图:

 

然后我们要修改发布规则,右键点击项目, 选择 Deployment Assembly

用eclipse插件创建一个Maven项目

 

选择WebContent,把它remove掉,测试类我们也不需要发布,test的两个目录页可以remove

 

 

接着重新指定一个web的路径,点击add,选择Folder -- 〉  next

 

在src下找到webapp目录,然后finish

用eclipse插件创建一个Maven项目

 

最后一步,我们要把当前的build path 指向 Maven Dependency, 直接点击add,选择Java Build Path Entries 然后next

 

然后再点击finish完成

用eclipse插件创建一个Maven项目

 

完成后如下图:

用eclipse插件创建一个Maven项目

 

 

至此一个基于maven的webapp就建立好了,并可以直接从eclipse中发布到tomcat中

 

补充:我们需要在src/main/webapp/WEB-INF下面创建一个web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  3. <display-name>项目名称</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.html</welcome-file>
  6. <welcome-file>index.htm</welcome-file>
  7. <welcome-file>index.jsp</welcome-file>
  8. <welcome-file>default.html</welcome-file>
  9. <welcome-file>default.htm</welcome-file>
  10. <welcome-file>default.jsp</welcome-file>
  11. </welcome-file-list>
  12. <filter>
  13. <filter-name>encoding</filter-name>
  14. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  15. <init-param>
  16. <param-name>encoding</param-name>
  17. <param-value>UTF-8</param-value>
  18. </init-param>
  19. <init-param>
  20. <param-name>forceEncoding</param-name>
  21. <param-value>true</param-value>
  22. </init-param>
  23. </filter>
  24. <filter-mapping>
  25. <filter-name>encoding</filter-name>
  26. <url-pattern>*.action</url-pattern>
  27. </filter-mapping>
  28. <filter>
  29. <filter-name>hibernateFilter</filter-name>
  30. <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  31. </filter>
  32. <filter-mapping>
  33. <filter-name>hibernateFilter</filter-name>
  34. <url-pattern>*.action</url-pattern>
  35. </filter-mapping>
  36. <filter>
  37. <filter-name>struts2</filter-name>
  38. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  39. </filter>
  40. <filter-mapping>
  41. <filter-name>struts2</filter-name>
  42. <url-pattern>*.action</url-pattern>
  43. <dispatcher>REQUEST</dispatcher>
  44. <dispatcher>FORWARD</dispatcher>
  45. </filter-mapping>
  46. <filter-mapping>
  47. <filter-name>struts2</filter-name>
  48. <url-pattern>*.jsp </url-pattern>
  49. </filter-mapping>
  50. <listener>
  51. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  52. </listener>
  53. <context-param>
  54. <param-name>contextConfigLocation</param-name>
  55. <param-value>classpath:spring-sms-service.xml</param-value>
  56. </context-param>
  57. </web-app>
  58. 在pom.xml文件中填入下面的代码:
  59. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  60. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  61. <modelVersion>4.0.0</modelVersion>
  62. <groupId>com.briup</groupId>
  63. <artifactId>sms_ssh</artifactId>
  64. <version>0.0.1-SNAPSHOT</version>
  65. <packaging>war</packaging>
  66. <properties>
  67. <springframework.version>3.1.2.RELEASE</springframework.version>
  68. <spring.simple-xml.version>2.6.7</spring.simple-xml.version>
  69. <struts2.version>2.3.15.2</struts2.version>
  70. <junit.version>4.8.1</junit.version>
  71. <aspectj.version>1.7.0</aspectj.version>
  72. <hibernate.version>3.5.6-Final</hibernate.version>
  73. <slf4j.version>1.5.8</slf4j.version>
  74. <javassist.version>3.11.0.GA</javassist.version>
  75. <cglib.version>2.2.2</cglib.version>
  76. <mysql.version>5.1.22</mysql.version>
  77. <log4j.version>1.2.16</log4j.version>
  78. <servlet.version>3.0.1</servlet.version>
  79. <dom4j.version>1.6.1</dom4j.version>
  80. <jaxen.version>1.1.4</jaxen.version>
  81. <json-lib.version>2.4</json-lib.version>
  82. <servlet.jsp.version>tomcat7</servlet.jsp.version>
  83. <jstl.version>1.2</jstl.version>
  84. <jxl.version>2.6.12</jxl.version>
  85. <sysesp.tag.version>1.0</sysesp.tag.version>
  86. <cas.client.version>3.2.4</cas.client.version>
  87. <com.caucho.hessian>4.0.7</com.caucho.hessian>
  88. <log4j-extend-jdbc.version>1.0.RELEASE</log4j-extend-jdbc.version>
  89. <quartz.version>1.8.5</quartz.version>
  90. <commons-pool2.version>2.2</commons-pool2.version>
  91. <jedis.version>2.3.1</jedis.version>
  92. <spring-data-redis.version>1.3.4.RELEASE</spring-data-redis.version>
  93. </properties>
  94. <dependencies>
  95. <!-- <dependency> <groupId>com.shiyue.sysesp</groupId> <artifactId>sysesp-core-service</artifactId>
  96. <version>${sysesp.core.service.version}</version> </dependency> -->
  97. <!-- ========= Spring begin ======== -->
  98. <dependency>
  99. <groupId>org.springframework</groupId>
  100. <artifactId>spring-context</artifactId>
  101. <version>${springframework.version}</version>
  102. </dependency>
  103. <dependency>
  104. <groupId>org.springframework</groupId>
  105. <artifactId>spring-orm</artifactId>
  106. <version>${springframework.version}</version>
  107. </dependency>
  108. <dependency>
  109. <groupId>org.springframework.security</groupId>
  110. <artifactId>spring-security-core</artifactId>
  111. <version>${springframework.version}</version>
  112. </dependency>
  113. <dependency>
  114. <groupId>org.springframework</groupId>
  115. <artifactId>spring-web</artifactId>
  116. <version>${springframework.version}</version>
  117. </dependency>
  118. <dependency>
  119. <groupId>org.springframework</groupId>
  120. <artifactId>spring-webmvc</artifactId>
  121. <version>${springframework.version}</version>
  122. </dependency>
  123. <dependency>
  124. <groupId>org.springframework</groupId>
  125. <artifactId>spring-context-support</artifactId>
  126. <version>${springframework.version}</version>
  127. </dependency>
  128. <dependency>
  129. <groupId>org.springframework</groupId>
  130. <artifactId>spring-core</artifactId>
  131. <version>${springframework.version}</version>
  132. </dependency>
  133. <dependency>
  134. <groupId>commons-logging</groupId>
  135. <artifactId>commons-logging</artifactId>
  136. <version>1.1.1</version>
  137. </dependency>
  138. <dependency>
  139. <groupId>aopalliance</groupId>
  140. <artifactId>aopalliance</artifactId>
  141. <version>1.0</version>
  142. </dependency>
  143. <dependency>
  144. <groupId>com.googlecode.ehcache-spring-annotations</groupId>
  145. <artifactId>ehcache-spring-annotations</artifactId>
  146. <version>1.2.0</version>
  147. </dependency>
  148. <dependency>
  149. <groupId>org.simpleframework</groupId>
  150. <artifactId>simple-xml</artifactId>
  151. <version>${spring.simple-xml.version}</version>
  152. </dependency>
  153. <!-- ========= Hibernate begin ======== -->
  154. <dependency>
  155. <groupId>org.hibernate</groupId>
  156. <artifactId>hibernate-core</artifactId>
  157. <version>${hibernate.version}</version>
  158. </dependency>
  159. <dependency>
  160. <groupId>org.hibernate</groupId>
  161. <artifactId>hibernate-annotations</artifactId>
  162. <version>${hibernate.version}</version>
  163. </dependency>
  164. <dependency>
  165. <groupId>junit</groupId>
  166. <artifactId>junit</artifactId>
  167. <version>4.12</version>
  168. </dependency>
  169. <dependency>
  170. <groupId>org.slf4j</groupId>
  171. <artifactId>slf4j-log4j12</artifactId>
  172. <version>${slf4j.version}</version>
  173. </dependency>
  174. <dependency>
  175. <groupId>org.slf4j</groupId>
  176. <artifactId>slf4j-api</artifactId>
  177. <version>${slf4j.version}</version>
  178. </dependency>
  179. <dependency>
  180. <groupId>javassist</groupId>
  181. <artifactId>javassist</artifactId>
  182. <version>${javassist.version}</version>
  183. </dependency>
  184. <dependency>
  185. <groupId>cglib</groupId>
  186. <artifactId>cglib</artifactId>
  187. <version>${cglib.version}</version>
  188. </dependency>
  189. <dependency>
  190. <groupId>javax.transaction</groupId>
  191. <artifactId>jta</artifactId>
  192. <version>1.1</version>
  193. </dependency>
  194. <!-- ========= Struts2.3.4 begin========= -->
  195. <dependency>
  196. <groupId>org.apache.struts</groupId>
  197. <artifactId>struts2-core</artifactId>
  198. <version>${struts2.version}</version>
  199. </dependency>
  200. <dependency>
  201. <groupId>org.apache.struts</groupId>
  202. <artifactId>struts2-convention-plugin</artifactId>
  203. <version>${struts2.version}</version>
  204. </dependency>
  205. <dependency>
  206. <groupId>org.apache.struts</groupId>
  207. <artifactId>struts2-json-plugin</artifactId>
  208. <version>${struts2.version}</version>
  209. </dependency>
  210. <dependency>
  211. <groupId>org.apache.struts</groupId>
  212. <artifactId>struts2-spring-plugin</artifactId>
  213. <version>${struts2.version}</version>
  214. <exclusions>
  215. <exclusion>
  216. <groupId>org.springframework</groupId>
  217. <artifactId>spring-web</artifactId>
  218. </exclusion>
  219. <exclusion>
  220. <groupId>org.springframework</groupId>
  221. <artifactId>spring-beans</artifactId>
  222. </exclusion>
  223. <exclusion>
  224. <groupId>org.springframework</groupId>
  225. <artifactId>spring-core</artifactId>
  226. </exclusion>
  227. <exclusion>
  228. <groupId>org.springframework</groupId>
  229. <artifactId>spring-context</artifactId>
  230. </exclusion>
  231. </exclusions>
  232. </dependency>
  233. <dependency>
  234. <groupId>commons-io</groupId>
  235. <artifactId>commons-io</artifactId>
  236. <version>2.2</version>
  237. </dependency>
  238. <!-- ========= Struts2.3.4 end========= -->
  239. <!-- ========= mysql begin ======== -->
  240. <dependency>
  241. <groupId>mysql</groupId>
  242. <artifactId>mysql-connector-java</artifactId>
  243. <version>5.1.22</version>
  244. </dependency>
  245. <!-- ========= mysql end ======== -->
  246. <!-- ========= log4j begin ======== -->
  247. <dependency>
  248. <groupId>log4j</groupId>
  249. <artifactId>log4j</artifactId>
  250. <version>${log4j.version}</version>
  251. </dependency>
  252. <!-- ========= log4j end ======== -->
  253. <dependency>
  254. <groupId>javax.servlet</groupId>
  255. <artifactId>javax.servlet-api</artifactId>
  256. <version>${servlet.version}</version>
  257. </dependency>
  258. <dependency>
  259. <groupId>javax.servlet.jsp</groupId>
  260. <artifactId>jsp-api</artifactId>
  261. <version>2.2</version>
  262. </dependency>
  263. <dependency>
  264. <groupId>dom4j</groupId>
  265. <artifactId>dom4j</artifactId>
  266. <version>${dom4j.version}</version>
  267. </dependency>
  268. <dependency>
  269. <groupId>jaxen</groupId>
  270. <artifactId>jaxen</artifactId>
  271. <version>${jaxen.version}</version>
  272. </dependency>
  273. <dependency>
  274. <groupId>org.aspectj</groupId>
  275. <artifactId>aspectjweaver</artifactId>
  276. <version>${aspectj.version}</version>
  277. </dependency>
  278. <dependency>
  279. <groupId>net.sf.json-lib</groupId>
  280. <artifactId>json-lib</artifactId>
  281. <version>${json-lib.version}</version>
  282. <classifier>jdk15</classifier>
  283. <exclusions>
  284. <exclusion>
  285. <artifactId>commons-collections</artifactId>
  286. <groupId>commons-collections</groupId>
  287. </exclusion>
  288. </exclusions>
  289. </dependency>
  290. <dependency>
  291. <groupId>jstl</groupId>
  292. <artifactId>jstl</artifactId>
  293. <version>${jstl.version}</version>
  294. </dependency>
  295. <dependency>
  296. <groupId>net.sourceforge.jexcelapi</groupId>
  297. <artifactId>jxl</artifactId>
  298. <version>${jxl.version}</version>
  299. </dependency>
  300. <!-- Extreme Table begin -->
  301. <dependency>
  302. <groupId>org.extremecomponents</groupId>
  303. <artifactId>extremecomponents</artifactId>
  304. <version>1.0.1</version>
  305. <exclusions>
  306. <exclusion>
  307. <groupId>commons-beanutils</groupId>
  308. <artifactId>commons-beanutils</artifactId>
  309. </exclusion>
  310. <exclusion>
  311. <groupId>commons-collections</groupId>
  312. <artifactId>commons-collections</artifactId>
  313. </exclusion>
  314. <exclusion>
  315. <groupId>commons-lang</groupId>
  316. <artifactId>commons-lang</artifactId>
  317. </exclusion>
  318. <exclusion>
  319. <groupId>commons-logging</groupId>
  320. <artifactId>commons-logging</artifactId>
  321. </exclusion>
  322. </exclusions>
  323. </dependency>
  324. <!-- Extreme Table end -->
  325. </dependencies>
  326. <build>
  327. <plugins>
  328. <!-- 资源文件编码 -->
  329. <plugin>
  330. <groupId>org.apache.maven.plugins</groupId>
  331. <artifactId>maven-resources-plugin</artifactId>
  332. <configuration>
  333. <encoding>UTF-8</encoding>
  334. </configuration>
  335. </plugin>
  336. <plugin>
  337. <groupId>org.apache.maven.plugins</groupId>
  338. <artifactId>maven-compiler-plugin</artifactId>
  339. <configuration>
  340. <source>1.8</source>
  341. <target>1.8</target>
  342. </configuration>
  343. </plugin>
  344. </plugins>
  345. </build>
  346. </project>
weinxin
七墨微信
请留下您宝贵的意见,或者每天来本站的文章页面签到,七墨将不胜感激~~
  • 用eclipse插件创建一个Maven项目已关闭评论
  • 98
  • A+
所属分类:Java篇
七墨