myeclipse新建maven多模块web项目

本文详细介绍了如何使用MyEclipse构建Maven多模块Web项目,包括设置Java版本、创建子模块、共享资源和依赖,以及配置Web模块和启动项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

myeclipse新建maven多模块web项目

2018年04月19日 11:16:22 飞儿姐姐 阅读数:825

版权声明:本文为博主原创文章,未经博主允许不得转载。你想转载请附加连接哦 https://blog.youkuaiyun.com/dmw412724/article/details/80001126

完整资源位置:https://download.youkuaiyun.com/download/dmw412724/10361959

1.构建父级项目

 

 

 

 

这里有个问题,maven默认的java是1.5的,它的子模块什么的都是1.5,以后updateProject也会还原成1.5,很恶心,所以要在父级模块里添加1.8

 
  1. <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">

  2. <modelVersion>4.0.0</modelVersion>

  3. <groupId>community</groupId>

  4. <artifactId>community-parent</artifactId>

  5. <version>0.0.1-SNAPSHOT</version>

  6. <packaging>pom</packaging>

  7. <build>

  8. <plugins>

  9. <plugin>

  10. <groupId>org.apache.maven.plugins</groupId>

  11. <artifactId>maven-compiler-plugin</artifactId>

  12. <version>3.1</version>

  13. <configuration>

  14. <source>1.8</source>

  15. <target>1.8</target>

  16. </configuration>

  17. </plugin>

  18. </plugins>

  19. </build>

  20. </project>

 

2.创建子级项目

一路next即可.然后结果是

可以看到父级模块下多了一个子级模块 community-common

我们再创建一个子级模块 叫 community-service

现在 整个项目长这样

 

3.现在让community-service里面可以访问community-common里面的资源

community-common里新建个类

 
  1. package org.community.common;

  2.  
  3. /**

  4. * Hello world!

  5. *

  6. */

  7. public class CommonUtil

  8. {

  9. public static void SayHello()

  10. {

  11. System.out.println( "Hello World!" );

  12. }

  13. }

那么现在在community-service里面是无法调用的.

需要在community-service里的pom文件里添加以下依赖

 
  1. <dependencies>

  2. <dependency>

  3. <groupId>maventest-common</groupId>

  4. <artifactId>maventest-common</artifactId>

  5. <version>0.0.1-SNAPSHOT</version>

  6. </dependency>

  7. </dependencies>

这时候,我们在community-service里面调用上面的那个CommonUtil就可以直接使用的.

但这个功能不局限于此.而是把community-comon的pom.xml里的jar包也都引入了过来.

我们修改下community-comon的pom.xml,加入ssm的一些包

    

 
  1. <properties>

  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  3. <spring.version>4.0.5.RELEASE</spring.version>

  4. <!-- mybatis版本号 -->

  5. <mybatis.version>3.2.6</mybatis.version>

  6. <!-- log4j日志文件管理包版本 -->

  7. <slf4j.version>1.7.7</slf4j.version>

  8. <log4j.version>1.2.17</log4j.version>

  9. </properties>

  10. <dependencies>

  11. <dependency>

  12. <groupId>junit</groupId>

  13. <artifactId>junit</artifactId>

  14. <version>4.12</version>

  15. <scope>test</scope>

  16. </dependency>

  17. <!-- spring核心包 -->

  18. <dependency>

  19. <groupId>org.springframework</groupId>

  20. <artifactId>spring-core</artifactId>

  21. <version>${spring.version}</version>

  22. </dependency>

  23.  
  24. <dependency>

  25. <groupId>org.springframework</groupId>

  26. <artifactId>spring-web</artifactId>

  27. <version>${spring.version}</version>

  28. </dependency>

  29. <dependency>

  30. <groupId>org.springframework</groupId>

  31. <artifactId>spring-oxm</artifactId>

  32. <version>${spring.version}</version>

  33. </dependency>

  34. <dependency>

  35. <groupId>org.springframework</groupId>

  36. <artifactId>spring-tx</artifactId>

  37. <version>${spring.version}</version>

  38. </dependency>

  39.  
  40.  
  41. <dependency>

  42. <groupId>org.springframework</groupId>

  43. <artifactId>spring-jdbc</artifactId>

  44. <version>${spring.version}</version>

  45. </dependency>

  46.  
  47.  
  48. <dependency>

  49. <groupId>org.springframework</groupId>

  50. <artifactId>spring-webmvc</artifactId>

  51. <version>${spring.version}</version>

  52. </dependency>

  53. <dependency>

  54. <groupId>org.springframework</groupId>

  55. <artifactId>spring-aop</artifactId>

  56. <version>${spring.version}</version>

  57. </dependency>

  58.  
  59.  
  60. <dependency>

  61. <groupId>org.springframework</groupId>

  62. <artifactId>spring-context-support</artifactId>

  63. <version>${spring.version}</version>

  64. </dependency>

  65.  
  66.  
  67. <dependency>

  68. <groupId>org.springframework</groupId>

  69. <artifactId>spring-test</artifactId>

  70. <version>${spring.version}</version>

  71. </dependency>

  72.  
  73.  
  74.  
  75.  
  76.  
  77. <!-- json数据 -->

  78. <dependency>

  79. <groupId>org.codehaus.jackson</groupId>

  80. <artifactId>jackson-core-asl</artifactId>

  81. <version>1.9.13</version>

  82. </dependency>

  83. <dependency>

  84. <groupId>org.codehaus.jackson</groupId>

  85. <artifactId>jackson-mapper-asl</artifactId>

  86. <version>1.9.13</version>

  87. </dependency>

  88.  
  89. <!-- 日志文件管理包 -->

  90. <!-- log start -->

  91. <dependency>

  92. <groupId>log4j</groupId>

  93. <artifactId>log4j</artifactId>

  94. <version>${log4j.version}</version>

  95. </dependency>

  96. <dependency>

  97. <groupId>org.slf4j</groupId>

  98. <artifactId>slf4j-api</artifactId>

  99. <version>${slf4j.version}</version>

  100. </dependency>

  101. <dependency>

  102. <groupId>org.slf4j</groupId>

  103. <artifactId>slf4j-log4j12</artifactId>

  104. <version>${slf4j.version}</version>

  105. </dependency>

  106. <!-- log end -->

  107. </dependencies>

等待依赖包下载好,我们再去community-service里面去调用spring的方法,发现是可以的,也引入了这些资源

4.创建一个具有web功能的子模块

先建一个community-web-back 就是网站后台的这个模块,点开pom.xml,把jar改成war

然后再右键该模块-peroperties-projectFacts

这时候我们就会发现项目已经变成了web项目.

这时候的错误是因为WebRoot里面的那个jsp需要javaEE的servlet包,而我们现在没有.加入即可

但是webRoot这个文件夹不符合maven的约定.所以,我们把webroot里面的文件复制到src/main下新建文件夹webapp,然后复制一个web.xml到里面

如果pom.xml会报错,多半是因为少了web.xml或者在properties标签里添加这个webVersion

 
  1. <properties>

  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  3. <webVersion>3.1</webVersion>

  4. </properties>

接着引入那个community-common的依赖.

5.applictionContext-spring.xml等配置文件的依赖

那么关于web启动的一些配置文件呢?我们也放到community-common里面

新建个/src/main/config

把一些资源塞进去

然后在community-web-back子模块里的Pom.xml里引入

 
  1. <build>

  2. <resources>

  3. <resource>

  4. <directory>../community-common/src/main/config</directory>

  5. <includes>

  6. <include>*</include>

  7. </includes>

  8. <targetPath>.</targetPath>

  9. </resource>

  10. </resources>

  11. </build>

这个时候就ok了.配置好你的xml.然后在community-web-back里写个testController.我们接着启动项目

6.启动web项目

按正常Tomcat把这个community-web-back放进去就行了.就启动成功了可以访问.但是如果不注意,会有几个个常见错误.

一个是:

Failed to start component .这个建议重新下载jar包,有的jar包可能错误,或者版本冲突

一个是:

org.springframework.core.io.Resource 这个是根本没有载入进去. 右键-properties-builder path把资源添加去

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值