Article directory
Maven
1. apache background
1. apache----Software Foundation
Open source software: Dubbo, Log4j, Maven, rocketMq, Tomcat…
2. The origin of the name
The name of the Indian tribe
a patchy server ---- patch service
3. The way to upgrade the project in apache
Preparation, Incubation, Graduation Stages
2. Getting to know Maven for the first time
1. The concept of Maven
maven is an automated build tool
- Build: is the process of deploying the results of project compilation to the server
- Compile: The .java file is compiled into a .class bytecode file through the javac compiler
2. The traditional way
Use the jar package
to download – put it in the lib directory – if other jars are referenced – continue to download the
jar package version is inconsistent / needs to be upgraded, the jar package is updated
3. Small knowledge
I heard that many seniors call Maven "Ma Wen"
, in fact, they should read "Mei Wen"
3. Advanced Maven
1. Warehouse
There is a concept of warehouse, unified management of jar packages, and each jar uses coordinates to correspond to the location.
For a jar package, the folder + name + version number can locate a unique jar.
Local warehouse (folder on this machine) -> central warehouse (provided by maven) -> remote warehouse (private server)
Can the jar package be found in the local warehouse? If the jar does not exist, go to the remote warehouse to find it, and if the remote does not exist, go to the central warehouse to find it.
If it is not found, an error will be reported.
It can reuse jar packages, and multiple projects share jar packages.
2. Maven project
Quickly create a Maven project for JavaWeb
- Choose to create a new project
- Select maven from the left function bar
- SDK chooses its own jdk version
- Check Create Template and select maven-archetype-webapp
- next
- Fill in groupId artifactId version (three elements of coordinates)
groupId represents the source channel of the jar, corresponding to the name of the folder
com.uncle
artifactId represents the unique name of the project csdn-maven-test.jar
version represents the version number 1.0 2.0 ...
-SNAPSHOT (snapshot version, unstable) -RELEASE
(release version, stable)
- Configure your own Maven (the most important step)
Choose the Maven
you downloaded and choose the Maven that you downloaded and configured – config – settings.xml
next
- Is the prompt to automatically import the package after the project is created?
Select automatic import - enable auto-import
or right-click the pom.xml file and select Reimport to import manually
! The new version of IDEA cannot set automatic import!
- The maven project has an agreed directory structure
The design idea "convention is better than configuration" is a unified norm
The src code directory
is divided into main (core code and resources) and test (test code and resources),
where main contains java (code storage) and resources (resource storage) folders
The core configuration file of pom.xml mvn
must have three elements of coordinates corresponding to the project after it is packaged (filled in when the project is created)
3, maven download and configuration
Official website address: https://maven.apache.org/download.cgiDownload
apache-maven-3.6.3-bin.zip
settings.xml is the configuration file of the maven software itself
1) Unzip -> store in English path -> enter bin folder
-> copy path to configure environment variables (right click on my computer)
2) Verify -> Command Prompt -> Enter mvn -v (check the version number)
3) Associate idea -> File -> settings
-> search for maven
-> change maven home directory to the maven directory you downloaded
-> set settings.xml and local warehouse 4) Set settings.xml in conf directory
a) Change local The configuration of the repository is
C:\Users${username}.m2\repository
b) It is better to download jar packages from the central repository through mirroring.
Usually, the mirror provided by Alibaba Cloud is used https://yq.aliyun.com/articles/ 703623
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
c) Copy the above address to the tab of the settings file
5) Find the file you just configured in the user settings file option of idea.
6) If an error occurs, check the error log, Help -> show log in explorer
If the error is
ERROR - #org.jetbrains.idea.maven - com.google.inject.CreationException: Unable to create injector, see
the following errors
is a version incompatibility problem Change to version 3.6.1
4. Maven dependencies
存放到<dependencies>标签下
1)查找依赖坐标,通过https://mvnrepository.com网站找到
2)以引用guava为例(google提供的使用最广泛的jar)
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
</dependencies>
3) 依赖有递进关系
当引入依赖时,默认引入它依赖的其他jar。
项目A -> jar包B -> 相当于引入了jar包C
4)剔除依赖的方式
使用<exclusions>标签, 以findbugs为例
<exclusions>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
</exclusions>
5)依赖使用的范围
构建包含的流程:编译、测试、运行、打包、安装、部署
compile test package install deploy
在右侧 maven projects标签栏中 打开项目名下面的lifecycle
如果没有 去View - Tool windows中查找
使用<scope>标签 放在artifactId下面
compile缺省值 伴随着项目的整个生命周期而存在。
provided已提供的 不需要将此依赖打包到项目最终的jar包里。
runtime运行时使用
比如<scope>runtime</scope>
test 测试时使用
system 自定义jar包位置 (不推荐)
5. Maven's life cycle
clean validate compile test package verify install site deploy
1) clean 项目清理
2) validate 校验项目的可用性
3) compile 编译
4) test 执行单元测试
5) package 打包
6) verify 校验测试结果
7) install 安装
8) site 网站站点文件的创建(用的非常少)
9) deploy 部署
命令效果:
1) compile 编译
注意点1:再次验证mvn -v里面的java运行目录,是否是jdk所在目录
如果不是,说明环境变量缺少JAVA_HOME的配置
注意点2:编译时需要指定jdk的版本
maven提供对全局jdk的配置(settings文件中<profiles>标签下)
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8
</maven.compiler.compilerVersion>
</properties>
</profile>
正式编译mvn clean compile
编译结果出现在根目录下的target文件夹中
执行打包 mvn clean package
打包结果,出现了项目的jar包
执行安装 mvn clean install (打包文件存储到本地仓库中)
本地仓库中出现对应的文件夹和jar
执行部署 mvn clean deploy
首先要配置远程仓库的地址,然后将打包文件上传到远程仓库中
2) 跳过测试阶段
方式一: mvn package -DskipTests 虽然跳过单元测试的执行,但仍然会编译代码,不是很推荐使用
方式二: mvn package -Dmaven.test.skip=true (最最常用的命令之一)既不会执行测试代码也不会编译。
3) 查看依赖树
方式一:pom.xml文件中右键 -> diagrams -> show dependencies
方式二:mvn dependency:tree
功能: 更好的解决jar包冲突问题