Maven (3): The use of Maven (on)
- foreword
- 1. Experiment 1: Create a Maven project based on coordinates
- 2. Experiment 2: Writing code in Maven project
- 3. Experiment 3: Execute the Maven build command
foreword
This blogger will use CSDN to record the experience and knowledge he has personally gained and learned on the way to study software development. Interested friends can pay attention to the blogger! Perhaps a person can go fast alone, but a group of people can go farther!
1. Experiment 1: Create a Maven project based on coordinates
1. Maven core concept: coordinates
1.1 Coordinates in mathematics
Using
x
,y
,z
and three "vectors" as the coordinate system of the space, you can uniquely locate a "point" in the space .
1.2 Coordinates in Maven
⭕Vector specification uses three vectors to uniquely locate a package in
Maven
the repositoryjar
.
groupId
: of a company or organizationid
artifactId
: a project or a module in the projectid
version
:version number⭕How to get the value of the three vectors
groupId
: The reverse order of the domain name of the company or organization, and the project name is usually added, for example:com.atguigu.maven
artifactId
: The name of the module, which will be used as the project name ofMaven
the projectversion
: The version number of the module, set according to your needs
For example:SNAPSHOT
Indicates the snapshot version, in the process of iteration, unstable version
For example:RELEASE
Indicates the official version
⭕ Example:
groupId:com.atguigu.maven
artifactId:pro01-atguigu-maven
version:1.0-SNAPSHOT
1.3 The correspondence between the coordinates and the storage path of the jar package in the warehouse
coordinate:
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
jar
The location of the package corresponding to the above coordinatesMaven
in the local warehouse:
Maven本地仓库根目录\javax\servlet\servlet-api\2.5\servlet-api-2.5.jar
2. Experimental operation
2.1 Create a directory as a workspace for subsequent operations
For example:
D:\maven-workspace\space201026
WARNING
At this point we already have three directories, namely:
Maven
Core program: Zhongjun Big AccountMaven
Local Depot: Barracks- Local Workspace: Battlefield
2.2 Open the command line window in the workspace directory
2.3 Generate Maven project using command
run
mvn archetype:generate
command
Follow the instructions below
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7:【直接回车,使用默认值】
Define value for property 'groupId': com.atguigu.maven
Define value for property 'artifactId': pro01-maven-java
Define value for property 'version' 1.0-SNAPSHOT: :【直接回车,使用默认值】
Define value for property 'package' com.atguigu.maven: :【直接回车,使用默认值】
Confirm properties configuration: groupId: com.atguigu.maven artifactId: pro01-maven-java version: 1.0-SNAPSHOT package: com.atguigu.maven Y: :【直接回车,表示确认。如果前面有输入错误,想要重新输入,则输入 N 再回车。】
2.4 Adjustment
Maven
The project generated by defaultjunit
depends the lower3.8.1
version, we can change it to a more suitable4.12
version .The automatically generated
App.java
andAppTest.java
can be deleted.
<!-- 依赖信息配置 -->
<!-- dependencies复数标签:里面包含dependency单数标签 -->
<dependencies>
<!-- dependency单数标签:配置一个具体的依赖 -->
<dependency>
<!-- 通过坐标来依赖其他jar包 -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- 依赖的范围 -->
<scope>test</scope>
</dependency>
</dependencies>
2.5 Interpretation of automatically generated pom.xml
<!-- 当前Maven工程的坐标 -->
<groupId>com.atguigu.maven</groupId>
<artifactId>pro01-maven-java</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 当前Maven工程的打包方式,可选值有下面三种: -->
<!-- jar:表示这个工程是一个Java工程 -->
<!-- war:表示这个工程是一个Web工程 -->
<!-- pom:表示这个工程是“管理其他工程”的工程 -->
<packaging>jar</packaging>
<name>pro01-maven-java</name>
<url>http://maven.apache.org</url>
<properties>
<!-- 工程构建过程中读取源码时使用的字符集 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 当前工程所依赖的jar包 -->
<dependencies>
<!-- 使用dependency配置一个具体的依赖 -->
<dependency>
<!-- 在dependency标签内使用具体的坐标依赖我们需要的一个jar包 -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- scope标签配置依赖的范围 -->
<scope>test</scope>
</dependency>
</dependencies>
3. Maven core concept: POM
3.1 Meaning
POM:Project Object Model
, the project object model. Similar
to is: , the Document Object Model. They are all concrete manifestations of modeling ideas.POM
DOM(Document Object Model)
3.2 Modeling thinking
POM
It means that the project is abstracted as a model, and then the objects in the program are used to describe the model. In this way, we can use the program to manage the project. In our development process, the most basic approach is to abstract things in real life into models, and then encapsulate the data related to the model as an object, so that the data related to real things can be calculated in the program.
3.3 Corresponding configuration files
POM
The idea is concentratedMaven
inpom.xml
the configuration file under the project root directory. So thispom.xml
configuration file isMaven
the core configuration file of the project. In fact, learningMaven
is learn how to configure this file and what is the use of each configuration.
4. Maven core concept: agreed directory structure
4.1 The role of each directory
There is also a
target
directory dedicated to storing the output of the build operation.
4.2 Significance of agreed directory structure
Maven
In order to automate the build process as much as possible, it is necessary to agree on the role of the directory structure. For example:Maven
To execute compiling operation, you must first go toJava
the source program directory to readJava
the source code, then execute compiling, and finally store the compiling result intarget
the compiling directory .
4.3 Convention is greater than configuration
Maven
For the issue of directory structure, there is no configuration method, but based on convention. This will make it very convenient for us during the development process. It must be very troublesome if you need to configure the location of each directory in detail every time you createMaven
a project .The current technological development trend in the development field is: agreement is greater than configuration, and configuration is greater than coding .
2. Experiment 2: Writing code in Maven project
1. Main program
The main program refers to the program being tested, and it is also the program that will actually be used in the project in the future.
package com.atguigu.maven;
public class Calculator {
public int sum(int i, int j){
return i + j;
}
}
2. Test procedure
package com.atguigu.maven;
import org.junit.Test;
import com.atguigu.maven.Calculator;
// 静态导入的效果是将Assert类中的静态资源导入当前类
// 这样一来,在当前类中就可以直接使用Assert类中的静态资源,不需要写类名
import static org.junit.Assert.*;
public class CalculatorTest{
@Test
public void testSum(){
// 1.创建Calculator对象
Calculator calculator = new Calculator();
// 2.调用Calculator对象的方法,获取到程序运行实际的结果
int actualResult = calculator.sum(5, 3);
// 3.声明一个变量,表示程序运行期待的结果
int expectedResult = 8;
// 4.使用断言来判断实际结果和期待结果是否一致
// 如果一致:测试通过,不会抛出异常
// 如果不一致:抛出异常,测试失败
assertEquals(expectedResult, actualResult);
}
}
3. Experiment 3: Execute the Maven build command
1. Requirements
When running commands related to build operations
Maven
in , you must enterpom.xml
the directory where . If you dopom.xml
not runMaven
the build command in the same directory as , you will see the following error message:
The goal you specified requires a project to execute but there is no POM in this directory
TIP:
mvn -v
The command has nothing to do with the build operation, as long as it is configured correctlyPATH
, it can be executed in any directory. And build-related commands should be run in the directorypom.xml
where it is located - whichever project to operate, enterpom.xml
the directory of this project.
2. Cleaning operation
mvn clean
Effect: delete
target
directory
3. Compile operation
Order | effect |
---|---|
mvn compile |
Compile the main program |
mvn test-compile |
Test program compilation |
target/classes |
The directory where the compiled results of the main program are stored |
target/test-classes |
The directory where the test program compilation results are stored |
4. Test operation
mvn test
The directory where the test report is stored:
target/surefire-reports
5. Packaging operation
mvn package
The result of packaging -
jar
package, storage directory:target
6. Installation operation
mvn install
[INFO] Installing D:\maven-workspace\space201026\pro01-maven-java\target\pro01-maven-java-1.0-SNAPSHOT.jar to D:\maven-rep1026\com\atguigu\maven\pro01-maven-java\1.0-SNAPSHOT\pro01-maven-java-1.0-SNAPSHOT.jar
[INFO] Installing D:\maven-workspace\space201026\pro01-maven-java\pom.xml to D:\maven-rep1026\com\atguigu\maven\pro01-maven-java\1.0-SNAPSHOT\pro01-maven-java-1.0-SNAPSHOT.pom
The effect of the installation is to store
jar
the packages intoMaven
the local repository. Thejar
packageMaven
's path in the repository is generated from its coordinates.
The coordinate information is as follows:
<groupId>com.atguigu.maven</groupId>
<artifactId>pro01-maven-java</artifactId>
<version>1.0-SNAPSHOT</version>
The paths generated in
Maven
the repository are as follows:
D:\maven-rep1026\com\atguigu\maven\pro01-maven-java\1.0-SNAPSHOT\pro01-maven-java-1.0-SNAPSHOT.jar
In addition, the installation operation will also convert
pom.xml
the file intoXXX.pom
a file and store it in the local warehouse. So whenMaven
we want to see the originalpom.xml
file of a jar package in the local warehouse, we can just check the correspondingXXX.pom
file . They have changed their names and are essentially the same file.