maven教程
maven教程
真正的大师永远怀着一颗学徒的心!学会只是起点,熟练为了提升,因为爱好所以了解
maven基础(黑马程序员 P1~P14)
1、maven简介



2、安装
bin目录

可以看出需要配置这两个环境变量

boot目录
maven的类加载器

conf目录
核心配置
lib目录
类库
看到下面的页面就说明maven配好了

3、maven基本概念
仓库


坐标

仓库配置


默认放在 用户名/.m2/repository

<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

用户setting放在本地的repository:

4、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.0</modelVersion>
<!-- 基本设置 The Basics -->
<groupId>com.itheima</groupId>
<artifactId>project-java</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
5、maven 项目构建命令
5.1编译
在项目根目录输入
mvn compile
最终结果编译了一个源文件:
生成文件:
5.2清理
mvn clean
删除文件:
5.3测试
mvn test
测试后生成了两个测试报告文件夹:
surefire-reports
主要是测试环境和测试结果
test-classes
5.4打包
mvn package
生成一个jar

里面是只打包了源程序

5.5 安装
部署把你打包的东西放到仓库里面
mvn install

group id 决定他放的文件夹的层次
6、插件创建工程

mvn archetype:generate -DgroupId=com.itheima -DartifactId=java-project -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=0.0.1-snapshot -DinterativeMode=false
自动生成:

pom.xml里面默认带着junit

默认只创建了java文件夹

自动创建了APP.java

mvn archetype:generate -DgroupId=com.itheima -DartifactId=web-project -DarchetypeArtifactId=maven-archetype-webapp -Dversion=0.0.1-snapshot -DinterativeMode=false
唯一不同的地方就是package这里要打war包
相比普通的java项目,这里面多了一个webapp的文件夹


7、idea版本创建maven工程

配置maven
手工创建maven项目
进入project structure

进入选择new module

选择maven

填写相关信息:

这里finish创建完成之后,这里文档结构还都是灰的(新版的不用自己标识),并不是一个正确的项目:

会生成:

新建一个resources:

在pom.xml中引入junit依赖:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
刷新maven
新建Demo:
package com.itheima;
public class Demo {
public String say(String name){
System.out.println("hello "+name);
return "hello "+name;
}
}
新建测试类:
public class DemoTest {
@Test
public void testSay(){
Demo d = new Demo();
String ret = d.say("itheima");
Assert.assertEquals("hello itheima",ret);
}
}
Assert.assertEquals([String message],T expected,T actual)
比较两个参数是否相等,message是可选的消息,假如加入了该参数,则发生错误时会报告该消息。如果 expected,actual,返回true。否则调用 expected.equals(actual)来判断。
maven命令执行
进行编译:
进行测试:
配置maven的运行环境:

选择add new configuration->maven


8、idea使用骨架创建maven工程
8.1使用原型创建Java项目 quickstart
project structure->new module->勾选使用模板,输入quickstart

输入相关信息:

生成项目结构如图所示:

8.2 使用原型创建web项目 webapp
这次选中webapp这个模板


生成目录结构如图所示:

剩余目录自己补充:

9、Tomcat插件
搜索tomcat找到这个包
https://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->
<dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
</dependency>
要想添加插件需要在pom.xml中添加构件
这里可以设置端口


<!-- 指定pom的模型版本-->
<modelVersion>4.0.0</modelVersion>
<!--组织id-->
<groupId>com.itheima</groupId>
<!-- 项目id-->
<artifactId>web01</artifactId>
<!-- 版本号 snapshot是开发版 release 是完成版 -->
<version>1.0-SNAPSHOT</version>
<!--打包方式,web工程打包为war,java工程打包为jar-->
<packaging>war</packaging>
10、依赖配置与依赖传递
10.1依赖管理
依赖指当前项目运行所需的jar,一个项目可以设置多个依赖
格式:

10.2依赖传递
将project03的groupid、artifactid、version粘贴到其他的pom里面,可以产生依赖传递

在project02中粘贴,可以看出来形成了对于project03的依赖传递



特殊优先:同一个pom里面后面写的覆盖前面的

10.3可选依赖

如果想让其他的不知道你依赖的东西这里可以使用<optional>
标签,这个标签的默认值是false
<dependency>
<optional>true</optional>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
10.4排除依赖

<dependency>
<groupId>org.example</groupId>
<artifactId>project03</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
这里不需要写version
11、依赖范围
<scope>
默认值是compile

当scope是test的时候,如果pom里面写的是:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
<scope>test</scope>
</dependency>
只有在test这里可以使用pom里面的jar包,主程序不可以

12、生命周期与插件
生命周期可以看做是几岁,插件就是几岁干的那件事





配置jar就是对源代码打包,配置test-jar就是对测试代码打包

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<phase>generate-test-resources</phase>
</execution>
</executions>
</plugin>
</plugins>
会生成这样一个jar包:

里面就是这个源码:


对test-jar打包:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<phase>generate-test-resources</phase>
</execution>
</executions>
</plugin>
</plugins>
也可以同时打两个jar包

maven高级(P15~P29)
1、分模块开发与设计 🌟

首先先导入项目,这里需要project structure->import module

选择maven

拆分前:

1.1pojo模块拆分

新建模块:


1.2dao模块拆分

直接选中原来项目的java目录和resources目录,将项目粘贴到现在的main目录下,然后删掉一些无用的目录

然后到原来的项目将pom粘贴到现在的项目里面
删减一下依赖,就解决第一个报错了

然后解决User这个包报错,将pojo的相关信息,粘贴到dao的pom.xml里面的dependencies
<dependency>
<groupId>com.itheima</groupId>
<artifactId>ssm_pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
解决这个:
但是此时compile还是报错:

这是因为你这个项目回去本地仓库里面找,但是本地仓库中这个是空的
这时我们点击pojo的install,将其安装
此时仓库中出现:
1.3service模块拆分

将原项目的src粘贴到现在service部分的项目根目录下
删掉一些无用的

将原来的pom的依赖粘贴过来
再加上
<!-- 导入资源文件dao-->
<dependency>
<groupId>com.itheima</groupId>
<artifactId>ssm_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

整理一下applicationContext.xml

重新命名,方便区分:
测试类的修改:
因为我们把xml的文件名改了,所以需要将原来的xml删掉并且粘贴新的文件进来,并且把测试文件中使用的文件名重新修改一下
修改完:
数据库代码:
-- 创建ssm_db数据库
CREATE DATABASE IF NOT EXISTS ssm_db CHARACTER SET utf8;
-- 使用ssm_db数据库
USE ssm_db;
CREATE TABLE `user` (
`uuid` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`realName` varchar(100) DEFAULT NULL,
`gender` int(1) DEFAULT NULL,
`birthday` date DEFAULT NULL,
PRIMARY KEY (`uuid`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8
select version();可以查看当前的mysql版本
修改jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_db?useUnicode=true&serverTimezone=GMT&characterEncoding=UTF-8&useSSL=false
jdbc.username=root
jdbc.password=root
修改pom.xml
<!--mysql环境-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
dao和pojo模块再修改驱动之后重新clean,install,然后再进行service模块的测试
1.4controller模块拆分

新建一个webapp

将主项目中java和resources的内容放到controller模块的main下面
解决IDEA中webapp没有小蓝点
文件描述:新建的 maven 的 Module 项目,webapp 文件夹也是在创建完项目后手动添加的,出现了 webapp 文件夹不能被识别的情况。
1、选中项目 右键,然后点击 Open Module Settings

2、 选中对应项目下的 Web,如果没有 Web, 点击左上角的加号,找到 Web 项目,添加进去
3、点开 Type 下 和 Web Resource Directory 的节点,选择自己的webapp路径

将web.xml里面的applicationContext.xml变为applicationContext-*.xml,这样就可以匹配两个applicationContext-dao.xml
和applicationContext-service.xml
2、聚合🌟

聚合:通过一个模块管理其它某些模块的构建,即同时执行生命周期指令
新建一个maven项目,将src删除掉,只留一个空项目,然后做聚合
在pom.xml中
<!-- 管理的工程列表-->
<modules>
<!-- 具体的工程名称-->
<module>../ssm_service</module>
<module>../ssm_dao</module>
<module>../ssm_pojo</module>
<module>../controller</module>
</modules>
<packaging>pom</packaging>
modules没有先后顺序
“./”:代表目前所在的目录。
" . ./"代表上一层目录。
“/”:代表根目录。
pom.xml里面的<packing>
如果什么都不写,默认打jar包
3、继承🌟




ctrl+shift+/ 取消注释
在父工程中添加依赖:
<dependencyManagement>
<!-- 具体的依赖-->
<dependencies>
<!--spring环境-->
<!--spring环境-->
<!--spring环境-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
....
</dependencies>
</dependencyManagement>
在子工程中添加:
<!-- 定义该工程的父工程-->
<parent>
<groupId>com.itheima</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 填写父工程的pom文件-->
<relativePath>../ssm/pom.xml</relativePath>
</parent>
原则上子工程应该与父工程属于同一个groupId,所以这里groupid可以省略不写

同时我们的工程版本应该与父工程的版本保持一致,所以version也可以不写

接下来可以删掉子工程中所使用的dependency的版本号
这里插件启动错误:

因 为controller的打包方式存在问题
<packaging>war</packaging>
将子工程中的依赖添加到ssm父工程中,就可以将子工程 的版本号删掉了:
<!-- 添加自己的工程模块依赖-->
<dependency>
<version>1.0-SNAPSHOT</version>
<groupId>com.itheima</groupId>
<artifactId>ssm_service</artifactId>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>ssm_pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>ssm_dao</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
4、属性🌟

4.1自定义属性配置使用
编写一个属性,用${}去引用这个属性,这样改了一个地方其他地方也都会改。
<!-- 定义自定义属性-->
<properties>
<spring.version>5.1.9.RELEASE</spring.version>
</properties>
特别的其他属性:
因为子模块的version和父模块的一样,这里不用设置属性直接使用version
<dependency>
<groupId>com.itheima</groupId>
<artifactId>ssm_service</artifactId>
<version>${version}</version>
</dependency>
4.2属性类别


这里也可以使用
project.version


在cmd中敲入
mvn help:system
这里会下载两个部分System.properties、Environment Variables

这里使用的时候直接使用等号前面的字符串就可以取到这个属性


5、版本管理

6、资源配置


同样也可以在ssm的pom.xml中存储属性的值
<!-- 定义自定义属性-->
<properties>
<jdbc.driver>com.mysql.cj.jdbc.Driver</jdbc.driver>
<jdbc.url>jdbc:mysql://localhost:3306/ssm_db?useUnicode=true&serverTimezone=GMT&characterEncoding=UTF-8&useSSL=false</jdbc.url>
<jdbc.username>root</jdbc.username>
<jdbc.password>root</jdbc.password>
</properties>
在jdbc.properties里面引用:
jdbc.driver=${jdbc.driver}
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
同时需要在父工程的pom文件中写:
<build>
<!-- 配置资源文件对应的信息-->
<resources>
<resource>
<directory>../ssm_dao/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
directory这里对应的是目录
因为其他的项目也要使用这个资源,所以我们改成通配的资源路径
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
${project.basedir}指的是这个项目的所有路径
<filtering>true</filtering>
开启pom属性过滤功能
但还有test目录需要使用,我们可以
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
7、多环境开发配置


要想让程序识别多环境:
1.定义多环境
<profiles>
<!-- 定义生产环境-->
<profile>
<!-- 定义环境对应的唯一名称-->
<id>pro_env</id>
<!-- 定义环境中专用的属性值-->
<properties>
<jdbc.url>jdbc:mysql://localhost:3306/ssm_db?useUnicode=true&serverTimezone=GMT&characterEncoding=UTF-8&useSSL=false</jdbc.url>
</properties>
</profile>
<!-- 定义开发环境-->
<profile>
<id>dep_env</id>
<properties>
<jdbc.url>jdbc:mysql://127.1.1.1:3306/ssm_db?useUnicode=true&serverTimezone=GMT&characterEncoding=UTF-8&useSSL=false</jdbc.url>
</properties>
</profile>
</profiles>
2.设置启动命令

可以设置默认的环境:
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<!-- 定义生产环境-->
<profile>
<!-- 定义环境对应的唯一名称-->
<id>pro_env</id>
<!-- 定义环境中专用的属性值-->
<properties>
<jdbc.url>jdbc:mysql://localhost:3306/ssm_db?useUnicode=true&serverTimezone=GMT&characterEncoding=UTF-8&useSSL=false</jdbc.url>
</properties>
<!-- 设置默认启动-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
8、跳过测试

8.1使用界面操作跳过测试
点击这个可以跳过测试:

IDEA中 右击项目没有run maven按钮解决办法(快速使用maven命令)
去setting中下载maven helper插件
右击pom.xml文件选择 run maven->new goal

输入:

install -D skipTests
-D:代表要加参数;skip:跳过;Tests:所有的测试
8.2配置跳过测试
<plugin>
<artifactId>maven-surefire-plugin-2.12.4</artifactId>
<version>2.1</version>
<!-- 设置跳过测试-->
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
设置跳过测试用例 <configuration><includes>
是包含的用例, <configuration><excludes>
是排除的用例
<plugin>
<artifactId>maven-surefire-plugin-2.12.4</artifactId>
<version>2.1</version>
<!-- 设置跳过测试-->
<!-- <configuration>-->
<!-- <skipTests>true</skipTests>-->
<!-- </configuration>-->
<configuration>
<includes>
<!-- 包含指定的用例-->
<include>
**/UserServiceTest
</include>
</includes>
<excludes>
<!-- 排除-->
<exclude>
</exclude>
</excludes>
</configuration>
</plugin>
9、私服🌟
9.1Nexus安装、启动与配置

可以实现在小范围内的共享

9.1.1启动服务器
进入D:\nexus\nexus-3.20.1-01\bin>
nexus /run nexus
到浏览器中输入http://localhost:8081/
9.1.2修改基础配置文件
如果想要修改端口,可以到nexus/etc目录下看properties文件

9.1.3 修改服务器运行配置信息
如果内存不够可以在这个文件中进行调节:

9.2仓库分类与手动上传资源
我们有些资源需要到私服去拿,有些资源需要到中央仓库去拿。怎么样能统一一下呢?

可以扩充私服

不同版本可以放在不同仓库中,然后将仓库放到仓库组中,这样可以方便我们定位具体的位置

仓库分类:
宿主仓库:
有一些第三方的库,我们需要自己下载放进去
代理仓库:
本身是空的,去中央仓库中访问去拿
登录中央仓库:
密码在
登录进来之后可以重新设置密码:


进行创建仓库> maven2(hosted)

将其添加到群组里面:
上传组件:
点击browse—选中库->点击Upload component
这里会报一个snapshot不允许放在release库里面的警告

修改为RELEASE(这里会有一个小校验,如果是snapshot的不能上传到release库)

再次回到browse页面这里就会有刚才上传的组件

9.3本地仓库访问私服

再次创建一个snapshot的仓库:

本地仓库:
在settings.xml中配置servers:

<!-- 配置访问服务器的权限,让本地仓库可以访问我们自己搭建的私服 -->
<servers>
<server>
<id>heima-release</id>
<username>admin</username>
<password>admin</password>
</server>
<server>
<id>heima-snapshots</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
配置mirrors:
原来配的是:中央仓库的东西从阿里云拿
<!--更换私服将阿里云的私服屏蔽-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
现在再配一个除此之外的东西都从nexus私服拿
<!--自定义的黑马私服-->
<mirror>
<id>nexus-heima</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
这里的url从

这里点击copy

注意把这个settings.xml文件复制到maven的conf目录和本地仓库文件夹里面
9.4idea访问私服与组件上传
<!-- 发布配置管理-->
<distributionManagement>
<repository>
<id>heima-release</id>
<url>http://localhost:8081/repository/heima-release/</url>
</repository>
<snapshotRepository>
<id>heima-snapshots</id>
<url> http://localhost:8081/repository/heima-snapshots/</url>
</snapshotRepository>
</distributionManagement>
点击deploy
到我们的私服中查看,都上传上来了
之后开发 的时候只需要发布到公司局域网的私服上就可以了