Maven (3): The use of Maven (on)

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

insert image description here

Using x, y, zand 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 Maventhe repositoryjar .

  1. groupId: of a company or organizationid
  2. artifactId: a project or a module in the projectid
  3. version:version number

⭕How to get the value of the three vectors

  1. 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
  2. artifactId: The name of the module, which will be used as the project name of Maventhe project
  3. version: The version number of the module, set according to your needs
    For example: SNAPSHOTIndicates the snapshot version, in the process of iteration, unstable version
    For example: RELEASEIndicates the official version

⭕ Example:

  1. groupId:com.atguigu.maven
  2. artifactId:pro01-atguigu-maven
  3. 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>

jarThe location of the package corresponding to the above coordinates Mavenin 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:

  1. MavenCore program: Zhongjun Big Account
  2. MavenLocal Depot: Barracks
  3. Local Workspace: Battlefield

2.2 Open the command line window in the workspace directory

2.3 Generate Maven project using command

insert image description here

run mvn archetype:generatecommand

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

MavenThe project generated by default junitdepends the lower 3.8.1version, we can change it to a more suitable 4.12version .

The automatically generated App.javaand AppTest.javacan 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.POMDOM(Document Object Model)

3.2 Modeling thinking

POMIt 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

POMThe idea is concentrated Mavenin pom.xmlthe configuration file under the project root directory. So this pom.xmlconfiguration file is Maventhe core configuration file of the project. In fact, learning Mavenis 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

insert image description here

There is also a targetdirectory dedicated to storing the output of the build operation.

4.2 Significance of agreed directory structure

MavenIn order to automate the build process as much as possible, it is necessary to agree on the role of the directory structure. For example: MavenTo execute compiling operation, you must first go to Javathe source program directory to read Javathe source code, then execute compiling, and finally store the compiling result in targetthe compiling directory .

4.3 Convention is greater than configuration

MavenFor 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 create Mavena 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

insert image description here

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

insert image description here

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 Mavenin , you must enter pom.xmlthe directory where . If you do pom.xmlnot run Maventhe 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 -vThe command has nothing to do with the build operation, as long as it is configured correctly PATH, it can be executed in any directory. And build-related commands should be run in the directory pom.xmlwhere it is located - whichever project to operate, enter pom.xmlthe directory of this project.

2. Cleaning operation

mvn clean

Effect: delete targetdirectory

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 - jarpackage, 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 jarthe packages into Maventhe local repository. The jarpackage Maven'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 Maventhe 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.xmlthe file into XXX.poma file and store it in the local warehouse. So when Mavenwe want to see the original pom.xmlfile of a jar package in the local warehouse, we can just check the corresponding XXX.pomfile . They have changed their names and are essentially the same file.

Guess you like

Origin blog.csdn.net/weixin_52533007/article/details/125323930