一 war永远依赖于jar
从来只有war包中含有jar包,而没有jar包中含有war包
web工程依赖的java工程,就是jar包,这个jar包经过自动化部署后,会放在web工程的web-inf/lib目录下
1. 在web工程的项目2中,加入项目1的路径依赖
在 pro02-maven-web 工程的 pom.xml 中,找到 dependencies 标签,在 dependencies 标签中做如下配置
<dependency>
<!--
通过指定被依赖工程的坐标完成依赖
-->
<groupId>com.xyt.maven</groupId>
<artifactId>pro01-maven-java</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compiler</scope>
</dependency>
2 在web工程中,加入测试代码
证明在Web工程中可以使用Java工程中创建的那个类:Calculator
2.1 创建目录
mkdir src\test\java\com\xyt\maven
2.2 确认依赖junit
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2.3创建测试类
2.4 运行测试
mvn test
2.5 打包
D:\maven_workspace\spaceVideo\pro02-maven-web>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro02-maven-web >--------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- resources:3.0.2:resources (default-resources) @ pro02-maven-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_workspace\spaceVideo\pro02-maven-web\src\main\resources
确实生成了一个jar包
2.6 查看依赖列表
D:\maven_workspace\spaceVideo\pro02-maven-web>mvn dependency:list
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro02-maven-web >--------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] The following files have been resolved:
[INFO] junit:junit:jar:4.11:test
[INFO] com.xyt.maven:pro01-maven-java:jar:1.0-SNAPSHOT:compile
[INFO] org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] javax.servlet:javax.servlet-api:jar:3.1.0:provided
2.7 树形结构查看
D:\maven_workspace\spaceVideo\pro02-maven-web>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro02-maven-web >--------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- dependency:2.8:tree (default-cli) @ pro02-maven-web ---
[WARNING] Parameter 'localRepository' is deprecated core expression; Avoid use of ArtifactRepository type. If you need access to local repository, switch to '${repositorySystemSession}' expression and get LRM from it instead.
[INFO] com.xyt.maven:pro02-maven-web:war:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:4.11:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] \- com.xyt.maven:pro01-maven-java:jar:1.0-SNAPSHOT:compile
[INFO] ------------------------------------------------------------------------
二 测试依赖的范围
一个depency里面有四个元素
<dependency>
<!--
通过指定被依赖工程的坐标完成依赖
-->
<groupId>com.xyt.maven</groupId>
<artifactId>pro01-maven-java</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
scope代表作用范围,标签的作用范围
scope提供这么多依赖的原因就是,把它测试用的test,本地提供的provided,和常规complie区分开来
compile/test/provided/system/runtime/import
开发过程有效的含义就是,在IDEA中写项目时候,我们能不能把它点出来
部署到服务器有效的含义是,把war包丢到tomcat里面时,是否包含对应文件
将这个文件里面导入
1 compile的编译过程
1.1 compile编译Calulator
在 HelloServlet 类中导入 Calculator 类,然后编译就说明有效。
D:\maven_workspace\spaceVideo\pro02-maven-web\src\main\java\xyt\maven\HelloServlet.java
引入一个测试文件
/**
* 通过import语句将要测试的类引入当前类
引入后,编译通过,可以使用,这个范围的依赖对当前类有效
引入后,编译失败,不可以使用,这个范围的依赖对当前类无效
*main目录下的java类:HelloServlet
*complie范围引入的·依赖:pro01-maven-java
*pro01-maven-java中的类:Calculator
*验证的目标:
*
*
*
*
*/
import com.xyt.maven.Calculator;
public class HelloServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("hello maven web");
}
}
D:\maven_workspace\spaceVideo\pro02-maven-web>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro02-maven-web >--------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- clean:3.1.0:clean (default-clean) @ pro02-maven-web ---
[INFO] Deleting D:\maven_workspace\spaceVideo\pro02-maven-web\target
[INFO]
[INFO] --- resources:3.0.2:resources (default-resources) @ pro02-maven-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_workspace\spaceVideo\pro02-maven-web\src\main\resources
[INFO]
[INFO] --- compiler:3.8.0:compile (default-compile) @ pro02-maven-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\maven_workspace\spaceVideo\pro02-maven-web\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
D:\maven_workspace\spaceVideo\pro02-maven-web\pom.xml
<dependency>
<!--
通过指定被依赖工程的坐标完成依赖
-->
<groupId>com.xyt.maven</groupId>
<artifactId>pro01-maven-java</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
1.2 编译CalculatorTest
D:\maven_workspace\spaceVideo\pro02-maven-web>mvn clean test-compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro02-maven-web >--------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- clean:3.1.0:clean (default-clean) @ pro02-maven-web ---
[INFO] Deleting D:\maven_workspace\spaceVideo\pro02-maven-web\target
[INFO]
[INFO] --- resources:3.0.2:resources (default-resources) @ pro02-maven-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_workspace\spaceVideo\pro02-maven-web\src\main\resources
[INFO]
[INFO] --- compiler:3.8.0:compile (default-compile) @ pro02-maven-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\maven_workspace\spaceVideo\pro02-maven-web\target\classes
[INFO]
[INFO] --- resources:3.0.2:testResources (default-testResources) @ pro02-maven-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_workspace\spaceVideo\pro02-maven-web\src\test\resources
[INFO]
[INFO] --- compiler:3.8.0:testCompile (default-testCompile) @ pro02-maven-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\maven_workspace\spaceVideo\pro02-maven-web\target\test-classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
-
通过import语句将要测试的类引入当前类
引入后,编译通过,可以使用,这个范围的依赖对当前类有效
引入后,编译失败,不可以使用,这个范围的依赖对当前类无效 -
main目录下的java类:HelloServlet
-
complie范围引入的·依赖:pro01-maven-java
-
pro01-maven-java中的类:Calculator
1.3 打包成war包
主体功能放在这个compile里面99%
打war包:
D:\maven_workspace\spaceVideo\pro02-maven-web>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro02-maven-web >--------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- clean:3.1.0:clean (default-clean) @ pro02-maven-web ---
[INFO] Deleting D:\maven_workspace\spaceVideo\pro02-maven-web\target
[INFO]
[INFO] --- resources:3.0.2:resources (default-resources) @ pro02-maven-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_workspace\spaceVideo\pro02-maven-web\src\main\resources
[INFO]
[INFO] --- compiler:3.8.0:compile (default-compile) @ pro02-maven-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\maven_workspace\spaceVideo\pro02-maven-web\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
2 test的编译过程
2.1 main空间里面编译信息
在HttpSevelet里面导入信息
import org.junit.Test;
编译失败:验证main空间下编译这个Test依赖,会出问题
D:\maven_workspace\spaceVideo\pro02-maven-web>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro02-maven-web >--------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- clean:3.1.0:clean (default-clean) @ pro02-maven-web ---
[INFO] Deleting D:\maven_workspace\spaceVideo\pro02-maven-web\target
[INFO]
[INFO] --- resources:3.0.2:resources (default-resources) @ pro02-maven-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_workspace\spaceVideo\pro02-maven-web\src\main\resources
[INFO]
[INFO] --- compiler:3.8.0:compile (default-compile) @ pro02-maven-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\maven_workspace\spaceVideo\pro02-maven-web\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /D:/maven_workspace/spaceVideo/pro02-maven-web/src/main/java/xyt/maven/HelloServlet.java:[25,17] 程序包org.junit不存在
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.318 s
[INFO] Finished at: 2023-05-04T23:43:22+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project
pro02-maven-web: Compilation failure
[ERROR] /D:/maven_workspace/spaceVideo/pro02-maven-web/src/main/java/xyt/maven/HelloServlet.java:[25,17] 程序包org.junit不存在
在pro01-maven-java\src\main\java\com\xyt\maven目录下修改Calculator.java
2.2 test空间下编译
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
没问题,test必然可以在test下编译,本身test就是为test服务的
在HttpSevelet里面注释信息
//import org.junit.Test;
```bash
D:\maven_workspace\spaceVideo\pro02-maven-web>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro02-maven-web >--------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- clean:3.1.0:clean (default-clean) @ pro02-maven-web ---
[INFO] Deleting D:\maven_workspace\spaceVideo\pro02-maven-web\target
[INFO]
[INFO] --- resources:3.0.2:resources (default-resources) @ pro02-maven-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_workspace\spaceVideo\pro02-maven-web\src\main\resources
[INFO]
[INFO] --- compiler:3.8.0:compile (default-compile) @ pro02-maven-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\maven_workspace\spaceVideo\pro02-maven-web\target\classes
[INFO]
[INFO] --- resources:3.0.2:testResources (default-testResources) @ pro02-maven-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven_workspace\spaceVideo\pro02-maven-web\src\test\resources
[INFO]
[INFO] --- compiler:3.8.0:testCompile (default-testCompile) @ pro02-maven-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\maven_workspace\spaceVideo\pro02-maven-web\target\test-classes
[INFO]
[INFO] --- surefire:2.22.1:test (default-test) @ pro02-maven-web ---
[WARNING] Parameter 'localRepository' is deprecated core expression; Avoid use of ArtifactRepository type. If you need access to local repository, switch to '${repositorySystemSession}' expression and get LRM from it instead.
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.xyt.maven.CalculatorTest
输出测试的程序代码
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.03 s - in com.xyt.maven.CalculatorTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
3 provided的编译过程
3.1 test和provided不参与部署到服务器
- compile范围依赖的jar包会放入war包,通过test范围依赖的jar包不会放入war包。
- 所有东西都发给服务器,会增加服务器的负担
- 编译程序类似于火箭发射,junit类似于脚手架,不会交房交个脚手架
3.2 provided已经提供的意思
说明Tomcat里面有这个依赖,如果非要提供,会造成依赖冲突的问题
比方说jsp-api没有注明provided带进war包部署,会造成视图显示错误,造成空指针异常
NULLPOINTEREXCEPTION,
三 测试依赖的总结
1 compile:
使用第三方框架的jar包,或者项目真正需要部署的功能依赖,都用这个scope修饰
详细解释:
1)编译依赖范围(compile),该范围就是默认依赖范围,此依赖范围对 于编译、测试、运行三种classpath都有效,举个简单的例子,假如项目中有spring-core的依赖,那么spring-core不管是在编译,测试,还是运行都会被用到,因此spring-core必须是编译范围(构件默认的是编译范围,所以依赖范围是编译范围的无须显示指定)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>2.5</version>
<scope>compile</scope> <!--默认为该依赖范围,无须显示指定--〉
</dependency>
2 test
测试过程中使用的jar包,以test范围写进来,比如junit
详细解释
顾名思义就是针对于测试的,使用此依赖范围的依赖,只对测试classpath有效,在编译主代码和项目运行时,都将无法使用该依赖,最典型的例子就是 Junit, 构件在测试时才需要,所以它的依赖范围是测试,因此它的依赖范围需要显示指定为test ,当然不显示指定依赖范围也不会报错,但是该依赖会被加入到编译和运行的classpath中,造成不必要的浪费 。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
3 provided
主要是为了减轻服务器的负担,避免依赖冲突,比方说本地项目中有jsp-api.jar,但是tomcat里面也有这个文件,如果硬要导入,就会产生冲突,报空指针异常文章来源:https://www.toymoban.com/news/detail-434316.html
典型的例子就是servlet-api, 编译和测试该项目的时候需要该依赖,但是在运行时,web容器已经提供的该依赖,所以运行时就不再需要此依赖,如果不显示指定该依赖范围,并且容器依赖的版本和maven依赖的版本不一致的话,可能会引起版本冲突,造成不良影响
。文章来源地址https://www.toymoban.com/news/detail-434316.html
到了这里,关于maven从入门到精通 第三章 Maven中形成web对Java工程的依赖的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!