Maven依赖范围的介绍
添加jar包的坐标时,还可以指定这个jar包将来的作用范围:
依赖范围包括:
1、compile
编译范围,默认依赖范围。
编译范围的依赖会用在编译、测试、运行,由于运行时需要,所以该依赖会被打包。
2、provided
provided依赖只有当容器已提供该依赖之后才能使用,比如Tomcat容器提供了servlet和jsp的jar。
provided依赖在编译和测试时需要,在运行期不需要。
3、runtime
runtime依赖在运行和测试时需要,在编译时不需要。
由于在运行时需要,所以会被打包。
4、test
test范围依赖,在编译和运行时都不需要,只有测试阶段可用。比如junit。
由于运行时不需要所以不会打包。
5、system
不推荐使用。
Maven模块开发
创建父工程
由于父工程中不编写Java代码,所以可以删除src目录,只保留pom.xml来管理子模块所依赖的jar包。
创建Web模块
在MavenProject项目上,右键,New Module,
Finish创建完成。
查看MavenProject父工程的pom.xml
<?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>org.westos</groupId>
<artifactId>MavenProject</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>WebModule</module>
</modules>
</project>
多了一个<modules>标签。
查看WebModule模块的pom.xml
<?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>MavenProject</artifactId>
<groupId>org.westos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>WebModule</artifactId>
<packaging>war</packaging>
<name>WebModule Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>WebModule</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
对于web.xml中的较低版本,可以修改为下面的代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
创建Service模块
Finsih创建完成。
查看MavenProject父工程的pom.xml
<?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>org.westos</groupId>
<artifactId>MavenProject</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>WebModule</module>
<module>ServiceModule</module>
</modules>
</project>
又多了一个子模块。
查看ServiceModule模块的pom.xml
<?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>MavenProject</artifactId>
<groupId>org.westos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ServiceModule</artifactId>
</project>
默认打包方式为jar。
创建Dao模块
创建方式和ServiceDao类似,截图省略。
查看MavenProject的pom.xml
<?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>org.westos</groupId>
<artifactId>MavenProject</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>WebModule</module>
<module>ServiceModule</module>
<module>DaoModule</module>
</modules>
</project>
查看DaoModule的pom.xml
<?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>MavenProject</artifactId>
<groupId>org.westos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>DaoModule</artifactId>
</project>
聚合三个模块
由于在实际开发中,WebModule模块明显调用ServiceModule模块的方法,ServiceModule模块调用DaoModule模块的方法,那么三个模块之间是需要关联的。
编写查询,连接三个模块
在MavenProject父工程中引入的jar依赖,在子模块中会被自动引入。
MavenProject的pom.xml
<?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>org.westos</groupId>
<artifactId>MavenProject</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>WebModule</module>
<module>ServiceModule</module>
<module>DaoModule</module>
</modules>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>
</dependencies>
</project>
1、编写DaoModule
package org.westos.dao;
/**
* @author lwj
* @date 2020/8/27 14:59
*/
public class UserDao {
public String find(int id) {
//模拟查询数据库
System.out.println("请求到达DaoModule");
return "User{id:1,username:\"john\"}";
}
}
2、编写ServiceModule
package org.westos.service;
import org.westos.dao.UserDao;
/**
* @author lwj
* @date 2020/8/27 15:02
*/
public class UserService {
public String find(int id) {
//模拟Service层
//调用dao模块
UserDao userDao = new UserDao();
//因为在上一步已经建立了ServiceModule和DaoModule模块的依赖关系,所以可以直接用它里面的类
return userDao.find(id);
}
}
3、编写WebModule
编写一个Servlet调用Service模块
package org.westos.servlet;
import org.westos.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author lwj
* @date 2020/8/27 14:58
*/
@WebServlet(name = "MyServlet", value = "/myServlet")
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("请求到达WebModule");
request.setCharacterEncoding("utf-8");
String s = new UserService().find(1);
System.out.println(s);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
4、发布WebModule
使用传统的发布Tomcat方式发布
集中管理依赖版本 ※
在MavenProject父工程中集中管理依赖的版本号,子模块需要哪个依赖引入即可,方便之处在于子模块引入时不需要写版本号。
<?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>org.westos</groupId>
<artifactId>MavenProject</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>WebModule</module>
<module>ServiceModule</module>
<module>DaoModule</module>
</modules>
<!--集中管理依赖版本号-->
<properties>
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
<javax.servlet.jsp-api.version>2.3.3</javax.servlet.jsp-api.version>
<mysql-connector-java.version>5.1.47</mysql-connector-java.version>
<druid.version>1.1.23</druid.version>
<commons-dbutils.version>1.7</commons-dbutils.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${javax.servlet.jsp-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>${commons-dbutils.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
dependencyManagement只是依赖管理,而非真实依赖。
如果想要真实依赖,还是需要在子模块手动引入,只是不再需要在子模块写依赖版本。
重新发布项目,测试