划分多模块,也就是方便多人开发,自己开发自己的那块没有多大冲突
项目结构
整个项目目录是这样的:
GitHub地址 :https://github.com/thecattle/maven_model
—- app-parent
|– pom.xml (pom)
|
|– app-dao
| |– pom.xml (jar)
|
|– app-service
| |– pom.xml (jar)
|
|– app-web
|– pom.xml (war)
注意他们的packaging类型,有jar,war和pom三种
dao–作为jar导入–>service–作为jar导入–>web–build成war项目–>运行
项目搭建
父模块仅仅是帮助聚合其他模块构建的工具,它本身并无实际的内容。
父模块与其他模块的目录结构并非一定是父子关系,还可以是平行关系。更改下modules路径即可。
- 首先new一个maven项目作为parent项目。
pom文件如下:
<?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>com.sun</groupId>
<artifactId>maven_test</artifactId>
<version>1.0-SNAPSHOT</version>
<!--首先在parent中声明打包类型为 pom -->
<packaging>pom</packaging>
<!-- 声明包含的所有子模块 -->
<modules>
<module>maven_service</module>
<module>maven_web</module>
<module>maven_dao</module>
</modules>
<!--不声明会提示警告: Warning:java: 源值1.5已过时, 将在未来所有发行版中删除 -->
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring MVC support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>
- 再new一个maven项目作为dao模块。pom文件如下:
<?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">
<!--要声明父项目-->
<parent>
<artifactId>maven_test</artifactId>
<groupId>com.sun</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<!--打包类型为 jar -->
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<artifactId>maven_dao</artifactId>
</project>
- 再new一个maven项目作为service模块,pom如下:
<?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">
<!--要声明父项目-->
<parent>
<artifactId>maven_test</artifactId>
<groupId>com.sun</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!--打包类型为 jar -->
<packaging>jar</packaging>
<artifactId>maven_service</artifactId>
<dependencies>
<!--将dao模块作为jar包引进来 -->
<dependency>
<groupId>com.sun</groupId>
<artifactId>maven_dao</artifactId>
<!--版本号为父项目版本号 -->
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
- 再new一个maven项目作为web模块,pom如下:
<?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">
<!--声明父项目-->
<parent>
<artifactId>maven_test</artifactId>
<groupId>com.sun</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!--打包类型为 war -->
<packaging>war</packaging>
<artifactId>maven_web</artifactId>
<dependencies>
<!-- 引入 service模块 jar -->
<dependency>
<groupId>com.sun</groupId>
<artifactId>maven_service</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
现在这三个模块就已经关联起来了
配置SpringMVC
在web模块中,web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd"
id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!--spring mvc 配置 -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup> <!-- 是启动顺序,让这个Servlet随Servletp容器一起启动。 -->
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern> <!-- 会拦截/的请求。 -->
</servlet-mapping>
</web-app>
spring-mvc.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描com.sun下的所有包-->
<context:component-scan base-package="com.sun.**" />
</beans>