Maven automated build tool

Keywords: JavaEE Maven

Maven automated build tool

This post is reproduced from the Up Master of station B: Guard the most beautiful old Du in the world

Chapter I Introduction to Maven

1.1 stages in software development

Need analysis: analyze the specific functions completed by the project, what requirements are there, and how to implement them.

Design stage: according to the analysis results, what technology is used in the design project and solve the difficulties.

Development stage: coding to realize functions. Compile the code. Self test

Test stage: professional testers test the function of the whole project, which is very in line with the design requirements. Make a test report.

Project packaging and publishing stage: install the project for users

1.2 what can Maven do

1) The automatic construction of the project helps developers to compile, test, package, install and deploy the project code.

2) Manage dependencies (manage the various jar packages used in the project).

Dependency: other resources that need to be used in the project, most commonly jar. For example, the project should use mysql driver. Let's say that the project depends on mysql driver.

1.3 how to manage dependencies without maven

To manage jars, you need to download a jar separately from the network

The correct version needs to be selected

Manually handle dependencies between jar files. a. The class of b.jar should be used in jar.

1.4 what is maven

Maven is an open source project of apache foundation, which is developed using java syntax. The original meaning of Maven is: expert, expert. The pronunciation is ['me ɪ v( ə) n] Or ['mevn].

maven is an automated build tool for projects. Manage project dependencies.

1.5 concepts in maven

② Agreed directory structure
③ Coordinates
④ Dependency management
⑤ Warehouse management
⑥ Life cycle
⑦ Plug ins and targets
⑧ Inherit
⑨ Aggregate

1.6 acquisition and installation of Maven tools

Address: Download the. Zip file from. Use


  1. Determine JAVA_HOME to specify the installation directory of jdk. If there is no JAVA_HOME, you need to create JAVA_HOME in the environment variable of windows, and its value is the installation directory of jdk

  2. Unzip and put the unzipped files into a directory.

    The path of the directory should not have Chinese or spaces.

  3. Add the path of bin in maven installation directory to path

  4. Test maven installation. Execute mvn -v on the command line

    C:\Users\NING MEI>mvn -v
    Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
    Maven home: D:\tools\apache-maven-3.3.9\bin\..
    Java version: 1.8.0_101, vendor: Oracle Corporation
    Java home: C:\Program Files\Java\jdk1.8.0_101\jre
    Default locale: zh_CN, platform encoding: GBK
    OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"

Directory structure after maven decompression

Other installation methods of maven:

  1. Determine whether JAVA_HOME is valid

  2. In the environment variable, create a file called M2_HOME (or MAVEN_HOME), whose value is Maven's installation directory


  3. In the path environment variable, add% M2_HOME%\bin

  4. Test the installation of maven and execute mvn -v on the command line

    C:\Users\NING MEI>mvn -v
    Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
    Maven home: D:\tools\apache-maven-3.3.9\bin\..
    Java version: 1.8.0_101, vendor: Oracle Corporation
    Java home: C:\Program Files\Java\jdk1.8.0_101\jre
    Default locale: zh_CN, platform encoding: GBK
    OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"

The second chapter is the core concept of Maven

2.1 agreed directory structure

The maven project uses a directory structure that most people follow. It is called the agreed directory structure.

A maven project is a folder. For example, the project is called Hello

Hello Project folder
    	\main				It is called the main program directory (the code and configuration files that complete the project functions)
             \java          Source code (package and related class definitions)
    		 \resources	    configuration file
    	\test               Place the test program code (the test code written by the developer)
    		 \java          Test code( junit)
    		 \resources     Configuration files required by the test program
    \pom.xml                maven Configuration file, core file

How maven is used:

1) maven can be used independently: creating projects, compiling code, testing programs, packaging, deploying, and so on

2) maven and idea are used together: coding, testing, packaging and so on are realized through idea and maven

2.2 POM

POM: Project Object Model. maven treats the project as a model. Operating this model is operating the project.

maven realizes project construction and dependency management through pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>

<!-- project Is the root tag, followed by the constraint file -->
<project xmlns="" xmlns:xsi=""
  <!-- pom The version of the model is 4.0.0 -->  

  <!-- coordinate -->  

2.3 coordinates

The coordinate composition is groupid, artifactid, version. The coordinate concept comes from mathematics·

Coordinate function: the function of determining resources is the unique identification of resources. In maven, each resource is a coordinate. The coordinate value is unique. It is called gav for short


groupId: Organization name, code. Identification of company, group or unit. This value is often used as the reverse of the company domain name.
         For example: school website, groupId: com.bjpowernode

         If the project scale is relatively large, the domain name can also be written backwards+Name of large project.
	     For example: ,  Unmanned vehicle:
artifactId:Project name, if groupId There are items in the. At this time, the current value is the sub item name. The item name is unique.
version: Version, the version number of the project, and the number used. It consists of three digits. For example, the main version number.Minor version number.Minor version number, for example: 5.2.5. 
         Note: there are in the version number-SNAPSHOT, Indicates a snapshot, not a stable version.      
packaging Types of project packaging, including jar ,war, ear, pom Wait, the default is jar

gav used in the project:

1. Each maven project needs to have its own gav

2. To manage dependencies, you need to use other jar s and gav as identification.

Address to search for coordinates:

2.4 dependency

Dependency: other resources (jar s) to be used in the project.

maven needs to be used to represent dependencies and manage dependencies. Dependencies are used together with gav

In the pom.xml file, you need to use dependencies, dependency, and gav to complete the dependency description.


    <!-- journal -->
    <!-- mysql drive -->


maven use gav As an identification, Download dependent from the Internet jar.  Download to your local computer maven Manage these used by the project jar

2.5 warehouse

The warehouse stores things. maven's warehouse stores:

  1. maven tool has its own jar package.

  2. Other jar s of the third party, such as mysql driver, should be used in the project.

  3. The program written by yourself can be packaged into jar and stored in the warehouse.

Classification of warehouse:

  1. Local warehouse (local warehouse): located on your own computer, it is a directory on disk.

    Local warehouse: the default path is /. m2/repository in the directory of the account you log in to the operating system

         C:\Users\NING MEI\.m2\repository

    Modify the location of the local warehouse: modify the configuration file of maven tool (maven installation path \ conf\setting.xml)


    1) create a directory to be used as a warehouse. The directory should not have Chinese and spaces. The directory should not be too deep.

    For example: D:\openrepository

    2) modify the setting.xml file and specify the directory D:\openrepository


    3) copy the warehouse resources I provided to you to D:/openrepository

  2. Remote warehouse: a warehouse that needs to be accessed through the Internet

    1) Central warehouse: an ftp server that stores all resources.

    2) The image of the central warehouse is the copy of the central warehouse. There are mirrors in major cities.

    3) Private server: used in a local area network. The private server is its own warehouse server. Used within the company.

maven uses the warehouse: maven automatically uses the warehouse. After the project is started, maven executes the maven command. maven first accesses the local warehouse and obtains the required jar s from the warehouse. If the local warehouse does not exist, it needs to access the private server, central warehouse or image.

2.6 maven life cycle, plug-ins and commands

maven's life cycle: each stage of project construction. Including cleaning, compiling, testing, reporting, packaging, installation and deployment

Plug in: to complete each stage of the construction project, you need to use maven commands. The function of executing commands is completed through the plug-in. Plug ins are jar s and classes.

Command: the execution of maven function is issued by the command. For example, mvn compile

Unit test (junit):

junit is a unit testing tool, which is often used in java.

Unit: in java, it refers to a method. A method is a unit, and a method is the smallest unit of test.

Function: use junit to test whether the method meets the requirements. Developer self-test.

Using unit tests:

1) Add junit dependencies (some classes and methods)


2) Create a test class file in the src/test/java directory. Write test code

Suggestions for unit testing:

1. Definition of Test class. The name is generally Test + the name of the class to be tested

2. The package name of the test class is the same as that of the class to be tested.

3. Define the method in the class and test the code.

Method definition: public method,

No return value

Custom method name (suggested Test + Test method name)

Method has no parameters

4. The methods in the test class can be executed separately. Test classes can also be executed separately

5. Add @ Test to the method


1) mvn clean: clean command, which is used to delete previously generated data and delete the target directory.

Plug in: Maven clean plugin, version 2.5

2) mvn compile: compile the command, compile the executed code, and compile the java code in the src/main/java directory into a class file.

At the same time, copy the class file to the target/classes directory. This directory, classes, is the root directory where the class files are stored (also called classpath)

Plug in: Maven compiler plugin version 3.1. Plug in for compiling code

Maven resources plugin version 2.6. Resource plug-in to process files. The function is to put the in the src/main/resources directory

Copy the file to the target/classes directory.

3) MVN test compile: compile the command, compile the source files in the src/test/java directory, and copy the generated classes to the target / test classes directory. At the same time, copy the files in src/test/resources directory to test claim directory

Plug in: Maven compiler plugin version 3.1. Plug in for compiling code

Maven resources plugin version 2.6. Resource plug-in to process files

4) mvn test: the test command is used to execute the program in the test classes directory and test whether the main program code in the src/main/java directory meets the requirements.

Plug in: Maven surefire plugin version 2.12.4

5) mvn package: packaging. It is used to put the resource class files and configuration files in the project into a compressed file. The default compressed file is jar type. The web application is of war type, and the extension is jar.

Plug in: Maven jar plugin version 2.4. Perform packaging processing. Generate a jar extension file and put it in the target directory

The packaged file contains all generated class es and configuration files in the src/main directory, and has nothing to do with test.

Generated is ch01-maven-1.0-SNAPSHOT.jar

Packaged file name: artifactId-version.packaging

6) mvn install: install the generated packaged files into maven warehouse.

Plug in: Maven install plugin version 2.4. Install the generated jar file into the local repository.

To view the jar file in:


groupId The value in, if any "." The front and back are separate folders. com\bjpowernode
artifactId, Separate folders
version,Separate folders

2.7 custom configuration plug-in

In the pom.xml file, in the build tag. Setting plug-ins

<!-- Set up content related to the build project -->
    <!-- Setting plug-ins -->
			<source>1.8</source> <!-- Specifies the of the compiled code jdk edition -->
			<target>1.8</target> <!-- function java Program used jdk edition-->

Chapter III integration of Maven and idea

3.1 integration of maven in idea

There is a maven in idea. We want idea to use Maven installed by itself.

  1. Select File- Settings

Setting item: - DarchetypeCatalog=internal

2) File - Other Settings

Same as above

3.2 creating a normal java project based on maven

3.3 creating a web project

3.4 importing module s into idea

4. Chapter IV dependency management

Dependency scope: use scope to represent the dependency scope.

Dependency scope means that this dependency (jar and inner classes) works at that stage of project construction.

Dependency scope:

compile: by default, participate in all phases of the build project

Test: test, which is used in the test phase. For example, junit will be used when executing mvn test.

provided: provider. When the project is deployed to the server, it does not need to provide the dependent jar, but the dependent jar package of the server

It is obvious that servlet s and JSPS depend on each other

5. Chapter V common settings

1) It's about the configuration in properties

	<maven.compiler.source>1.8</maven.compiler.source> Source code compilation jdk edition
	<>1.8</> To run code jdk edition
	<>UTF-8</> Codes used in project construction to avoid Chinese garbled code
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> Code of the generated report
  1. global variable

Define the tag in properties. The tag is a variable, and the text of the tag is the value of the variable.

Use a global variable to represent the version number used by multiple dependencies.

Use steps:

1. In the properties tag, define a tag to specify the value of the version

  <!--Custom variable-->
  1. Use global variables, syntax ${variable name}


3) Using resource plug-ins

Information about the configuration file to be processed. maven processes the configuration file by default

① : maven will copy the files in the src/main/resources directory to the target/classes directory

② : maven only processes. Java files in the src/main/java directory, compiles these java files into classes, and copies them to the target/classes directory. Do not process other files.

  <!--Resource plug-in
      tell maven hold src/main/java Copy the file with the specified extension in the directory to target/classes In the directory.
      <directory>src/main/java</directory><!--Directory where-->
      <!--Including,.xml All files will be scanned-->
        <!--  filtering option false Do not enable filters, *.property Has played a role
        The role of filtration -->

Posted by pearjam on Fri, 26 Nov 2021 08:27:10 -0800