注意本篇是需要一定的maven基础的 如果没有请移步Maven教程–上
用手工的方式,创建maven 项目, 深刻理解Maven 工作机制
public class Hello {
public String sum(int n1, int n2) {
System.out.println("sum= " + (n1 + n2));
return "sum= " + (n1 + n2);
}
}
public class HelloTest {
@Test
public void testSum() {
Hello hello = new Hello();
String res = hello.sum(1, 9);
//使用断言
/**
* 1.Assert 是一个断言类,(就是判断实际返回的值和期望值是否相同)
* 2.如果res 和 "sum=10" 是相同的,则通过断言
* 3.如果res 和 "sum=10" 是不相同的,则给出提示
*
*/
Assert.assertEquals("sum=10", res);
}
<modelVersion>4.0.0modelVersion>
<groupId>com.wedugroupId>
<artifactId>java-project-mavenartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
● 说明: Maven 构建命令使用mvn 开头,后面添加功能参数,可以一次执行多个命令,使用空格分隔
mvn compile #编译
mvn clean #清理
mvn test #测试
mvn package #打包
mvn install #安装
使用IDEA 创建Maven WEB 项目, 可以运行hello,world 页面
注意体会Maven WEB 项目和前面创建的Maven java 项目不同
web-hello-maven 工程
<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.0modelVersion>
<groupId>com.wyxedugroupId>
<artifactId>web-hello-mavenartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>web-hello-maven Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
dependencies>
<build>
<finalName>web-hello-mavenfinalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.1.0version>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>3.0.2version>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.0version>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.1version>
plugin>
<plugin>
<artifactId>maven-war-pluginartifactId>
<version>3.2.2version>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.5.2version>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.8.2version>
plugin>
plugins>
pluginManagement>
build>
project>
war:先打包成war 包,再把该war 包部署到服务器上
war exploded:直接把文件夹、class 文件等等移到Tomcat 上进行部署。因此这种方式支持热部署,一般在开发的时候都是使用这种模式
一句话: 依赖指当前项目运行需要的jar,一个项目可以设置多个依赖
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>
一句话: 在当前项目中通过依赖配置建立的依赖关系
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
dependencies>
一句话: 当前项目直接依赖资源(比如m1), 而m1 又依赖资源(m2),我们就说当前项目间接依赖资源(m2)
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
<dependencies>
<dependency>
<groupId>com.wyxgroupId>
<artifactId>maven_BartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
dependencies>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13version>
dependency>
dependencies>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.wyxgroupId>
<artifactId>maven_BartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>com.wyxgroupId>
<artifactId>maven_CartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
project>
图解项目A 依赖情况
<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.0modelVersion>
<groupId>com.wyxgroupId>
<artifactId>maven_DartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
dependency>
dependencies>
project>
<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.0modelVersion>
<groupId>com.wyxgroupId>
<artifactId>maven_AartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>com.wyxgroupId>
<artifactId>maven_DartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.wyxgroupId>
<artifactId>maven_BartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
dependencies>
project>
<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.0modelVersion>
<groupId>com.wyxgroupId>
<artifactId>maven_AartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>com.wyxgroupId>
<artifactId>maven_DartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.wyxgroupId>
<artifactId>maven_BartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.43version>
dependency>
dependencies>
project>
一句话: 可选依赖指对外隐藏当前所依赖的资源- 不透明
<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.0modelVersion>
<groupId>com.wyxgroupId>
<artifactId>maven_DartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<optional>trueoptional>
dependency>
dependencies>
project>
一句话: 排除依赖指主动断开依赖的资源, 被排除的资源无需指定版本- 不需要
<dependency>
<groupId>com.wyxgroupId>
<artifactId>maven_DartifactId>
<version>1.0-SNAPSHOTversion>
<exclusions>
<exclusion>
<groupId>junitgroupId>
<artifactId>junitartifactId>
exclusion>
exclusions>
dependency>
一句话: 隐藏依赖是不让其他项目来引用(我不给), 排除依赖是主动不要引入的某个资源(我不要)
一句话: 依赖的jar 默认情况可以在任何地方使用, 通过scope 标签设定其作用范围
解读
<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.0modelVersion>
<groupId>com.wyxgroupId>
<artifactId>maven_DartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<optional>falseoptional>
<scope>testscope>
dependency>
dependencies>
project>
import org.junit.Test;
public class T2 {
@Test
public void f1(){
}
}
<optional>falseoptional>
<scope>compilescope>
一句话: Maven 构建生命周期描述的是一次构建过程经历了多少个事件
清理工作
核心工作,例如编译,测试,打包,部署等
产生报告,发布站点等
一句话: 项目构建生命周期分很多阶段,并不是每次都完整执行,而是根据用户的要求来执行的【比如你执行compile, 那么就执行到complie 这个阶段,如果你执行install, 则会执行compile->test->package->install】
1、演示compile
2、演示install
1、插件与生命周期内的某个阶段绑定,在执行到对应生命周期时, 由对应插件来完成任务/功能.
2、maven 插件很多,先看一张图:
3、通过插件可以自定义其他功能
4、文档: http://maven.apache.org/plugins/index.html
需求: 在pom.xml 加入自定义插件,能够在对maven_D 项目打包时,能输出主程序和测试程序的源码
<project xmlns="http://maven.apache.org/POM/4.0.0"
韩顺平Java 工程师
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.0modelVersion>
<groupId>com.wyxgroupId>
<artifactId>maven_DartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<optional>falseoptional>
<scope>compilescope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-source-pluginartifactId>
<version>2.2.1version>
<executions>
<execution>
<goals>
<goal>jargoal>
<goal>test-jargoal>
goals>
<phase>generate-test-resourcesphase>
execution>
executions>
plugin>
plugins>
build>
project>
图解
在maven 项目构建生命周期中,每个阶段的执行都有相应的插件完成
各个插件执行过程中,会附带输出内容,比如jar/war/xml/源码
程序员可以使用maven 默认的插件,也可以自定义插件,完成定制任务.
自定义插件引入成功, 是可以看到