(一)创建study-parent。可以用命令创建项目,也可以用eclipse创建study-parent模块,只保留pom.xml
study-parent项目的pom.xml
<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.foo</groupId>
<artifactId>study-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>study-parent</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(二)创建study-entity。接着study-parent 右键创建 maven module study-entity
study-entity项目的pom.xml
<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>
<parent>
<groupId>com.foo</groupId>
<artifactId>study-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>study-entity</artifactId>
<name>study-entity</name>
<url></url>
</project>
(三)创建study-dao。接着study-parent 右键创建 maven module study-dao
study-dao项目的pom.xml
<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>
<parent>
<groupId>com.foo</groupId>
<artifactId>study-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<artifactId>study-dao</artifactId>
<dependencies>
<dependency>
<artifactId>study-entity</artifactId>
<groupId>com.foo</groupId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
加上<packaging>jar</packaging>,因为groupId和version会继承system-parent中的groupId和version,
packaging设置打包方式为jar,同时添加对study-entity模块的依赖,
(四)创建study-service。接着study-parent 右键创建 maven module study-service
study-service项目的pom.xml
<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>
<parent>
<groupId>com.foo</groupId>
<artifactId>study-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>study-service</artifactId>
<packaging>jar</packaging>
<name>study-service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--
system-service依赖system-dao和system-domain,
但是我们只需添加system-dao的依赖即可,因为system-dao已经依赖了system-domain
-->
<dependency>
<groupId>com.foo</groupId>
<artifactId>study-dao</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
(五)创建study-parent。study-parent 右键创建 maven module study-web
maven 添加web 支持
同时 study-web项目的pom.xml
<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>
<parent>
<groupId>com.foo</groupId>
<artifactId>study-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>study-web</artifactId>
<packaging>war</packaging>
<name>study-web</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.foo</groupId>
<artifactId>study-service</artifactId>
<version>${project.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>study-web</finalName>
<plugins>
<!-- jetty插件 -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.5</version>
<configuration>
<webAppSourceDirectory>src/main/webapp</webAppSourceDirectory>
<scanIntervalSeconds>3</scanIntervalSeconds>
<contextPath>/study-web</contextPath><!-- 英文状态 -->
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8088</port>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意这里打的是war包,还有添加了jetty 服务器
启动和访问
study-parent 项目上 maven run clean 然后install 最后study-web 项目上右击选择 maven build 输入 jetty:run
访问的路径 127.0.0.1::8088/study-web 注意“-”是英文状态下
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html;charset=UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="Cache-Control" content="max-age=28800"/>
<title>学习系统</title>
</head>
<body>
欢迎登陆学习系统!!!
</body>
</html>
key:maven 多模块web项目