Common operations in Maven

Keywords: Java Maven unit testing

Common operations in Maven

1, Maven's directory structure:

Hello  Project name
├─src  Source code
│  ├─main  main program
│  │  ├─java  core java code
│  │  └─resources  configuration file
│  └─test  Test procedure
│      ├─java  test java code
│      └─resources  Test configuration file or resource file

Detailed description of each directory structure:

  1. src/main/java – store the. java file of the project, that is, the source code of the project.
  2. src/main/resources – store project resource files, such as configuration files at the end of yaml and properties.
  3. src/test/java – store all unit test. java files, such as junit test classes
  4. src/test/resources – test resource file
  5. targe - the output location of the project. The compiled class file will be output to this directory
  6. pom.xml – the core configuration file of maven project. Here, you should pay attention to whether it is a java project of maven or a dynamic web project of maven. For example, a dynamic web project will have a webapp directory

2, pom.xml in Maven

Function: introduce the required information in the form of coordinates jar Package, a configuration file for building the project.
  1. Core composition:
groupId: Generally, it is the reverse of the company domain name
artifactId: Generally, it is the module name, that is, the sub module name in the project
version: Version number of the project
name: Name of the project
packaging: The packaging type of the project. The default is jar package,web The of the project is war package
dependencies|dependency: Maven An important role of is management jar package,For a project can be built or run,Inevitable in the project,Will depend on many others jar package,stay Maven in,these jar It is called dependency,Use label dependency To configure. This dependent configuration is located by coordinates,It is not difficult for us to see from this,maven Put all the jar Packages are also considered to exist as projects.
properties: Is used to define some configurations shuxing For example, how to build source code,You can set the coding method of the project, and vice versa. You can also manage the version numbers of various dependencies in the project(Facilitate unified modification),For example, the following configuration:
		<!--Source code compilation jdk edition-->		
		<!--To run code jdk edition-->
		<!--Code used for project construction,Avoid Chinese garbled code-->
		<!--Code of the generated report-->
build: Represents a build related configuration, such as setting the configuration of a compiled plug-in jdk edition
parent: stay Maven If multiple modules need to declare the same configuration, for example: groupId,version,There are the same dependencies, or the same component configuration, and so on java Inheritance mechanism, using parent Declare the name of the parent project to inherit pom to configure
modules: stay Maven In multi module development,In order to build all modules of the whole project uniformly,An additional module can be provided,The module is packaged as pom,And used in it modules Other modules aggregated,In this way, through this module, you can automatically identify the dependencies between modules with one click to build all modules,call Maven Aggregation of
description: Description information
relativePath: Parent project pom.xml The relative path of the file. Examples of the above Tags:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
    <!--Maven Version of the model,about Maven2 and Maven3 Say,It can only be 4.0.0-->
    <!--groupId,artifactId,version Three elements generate one Maven Basic coordinates of the project,
    In many maven A project can be uniquely located in the project.
    The coordinates also determine the path and name of future projects in the warehouse -->

    <!--plug-in unit-->

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
	<!--Maven Version of the model,about Maven2 and Maven3 Say,It can only be 4.0.0-->
	<!--Coordinates of the parent project-->
        <relativePath/> <!-- lookup parent from repository -->
	<!--Reverse the domain name of the company name or organization name-->
	<!--Module name-->
	<!--Version number-->
    <description>Grain mall-Order service</description>

	<!--configuration information-->

	<!--rely on-->


3, Maven warehouse

  1. Local warehouse: it is used to store the plug-ins and jar packages downloaded from the remote warehouse or central warehouse. It is stored on the local hard disk. The location setting of the local warehouse is as follows:
  1. Remote warehouse:
    1. Central warehouse: a remote warehouse address is built in Maven:
    2. Private server: the server deployed in the LAN and serves all maven services used in the current LAN
    3. Image of central warehouse: set up in different locations to share traffic for the central warehouse
    The execution process is shown in the figure:

4, Maven's life cycle

  1. Clean Lifecycle: do some cleanup work before the real build
  2. Default Lifecycle: the core part of building, compilation, testing, packaging, deployment, etc
  3. Site Lifecycle: generate project reports, sites, and publishing sites

    Note: for us, no matter what stage of construction, we can directly execute the corresponding commands, and Maven will help us with automatic construction.

5, Maven common commands

  1. mvn clean: delete the target directory, but the packages install ed in the warehouse will not be deleted
  2. mvn compile: compile (execute the code under the main program)
  3. mvn test: Test (not only compiled the code under src/main/java, but also compiled the code under java/test/java)
  4. mvn package: package (it will be marked as a war package during execution, and the code under src/main/java and java/test/java are compiled)
  5. mvn install: install (when executing, it will be marked as a war package, and the code under src/main/java and java/test/java will be compiled, and the package will be installed in the local warehouse)
  6. mvn deploy: Deployment (configuration required)

Note: the maven command must be run in the pom.xml file directory

6, Plug in

Definition: functions provided by maven to execute programs for cleaning, compiling, testing, reporting and installation

  1. Clean plug-in: Maven clean plugin: 2.5
    The clean phase is an independent phase. Its function is to clear the target directory under the current project
  2. Resources plugin: Maven resources plugin: 2.6
    The function of the resource plug-in is to copy the configuration files required by the project to the specified destination. By default, copy the files in the src\main\resources directory to the classes directory
  3. compile plug-in: Maven compiler plugin
    When the compile plug-in executes, it calls the resources plug-in first. Its function is to compile the src\mainjava source code into bytecode, generate class files, and output the compiled class files to the target\classes directory
  4. test plug-in:
    The compile and resources plug-ins used for unit testing are the same as the main code, but the execution target is not good. The target testCompile and testResources compile the code under src\test\java into bytecode and output it to target \ test classes, and copy the configuration file under src\test\resources to target \ test classes
  5. Package package plug-in: Maven jar plugin
    This plug-in is to type the class file and configuration file into a jar(war or other format) package
  6. deploy release plug-in: Maven install plugin
    The function of publishing plug-ins is to deploy the built artifacts to the local warehouse, and a deploy plug-in is to deploy the built artifacts to the remote warehouse

7, Coordinate gav

  1. Maven manages any plug-in as a project in the warehouse, represented by a set of three-dimensional coordinates, so that a maven project can be uniquely located
  2. groupId: organization name, usually company or organization domain name in reverse order + project name
  3. artifactId: module name, usually project name
  4. Version: version number

8, dependency

  1. The normal operation of a maven project requires the support of other projects. Maven will automatically search in the local warehouse according to the coordinates. The Maven project of programmers needs to be installed before it can be saved to the warehouse
  2. When you don't use maven, all jars are not yours. You need to download copies everywhere. All jar packages with Maven are yours. Just call who you want. Maven help you download

9, Maven's dependency scope

maven's dependencies include compile, provide, runtime, test, and system

  1. compile: the dependency of the compilation scope will be used for compilation, testing and running. Due to the need of runtime, the dependency of the compilation scope will be packaged
  2. Test: the test scope is not required during compilation and running, but only during test compilation and test running. Test scope dependencies are not packaged because they are not required at runtime
  3. Provide: the provide dependency has only a jdk or a container for use after providing the dependency. The provide dependency is required during compilation and testing, but not during runtime
  4. Runtime: runtime dependencies are required to run and test the system, but not at compile time.
  5. System: system range dependency is similar to provide
	<!--runtime Indicates that the compiler does not use it,Use it at run time-->
<!--Import servlet Correlation dependency,request No error reporting-->
	<!--provided It means to use it at compile time,Not used at runtime-->
<!--system Compilation test is useful and will not run jar-->

10, Common settings for Maven

  1. Global settings: in Maven's pom.xml file, properties is used to define global variables, and the value of variables is referenced in the form of ${property_name} in POM. Define global variables:
  1. Variables used in maven system
	<!--Source code compilation jdk edition-->		
	<!--To run code jdk edition-->
	<!--Code used for project construction,Avoid Chinese garbled code-->
	<!--Code of the generated report-->
  1. Specify the resource location: all *. Java files in the src/main/java and src/test/java directories will be compiled in the comile and test comiple phases respectively, and the compilation results will be placed in the target/classes and targe / test classes directories respectively, but other files in the two directories will be ignored. If you need to put the file package under the src directory into the target/classes directory, As part of the output jar. Resource file location needs to be specified. The following contents are put into the buid tag, that is, files other than java files in src/mian/java and src/test/java cannot be compiled into the target directory, which can be set by adding the tag build in pom.xm
            <!--Path to resource file,Default at ${basedir}/src/main/resources/Directory-->
            <!--A matching pattern for a set of file names,The matched resource files will be processed by the build process-->
            <!--filtering default false,true Indicates that the parameters in the resource file ${key}
            Make dynamic changes at compile time. Substitution source compactability-Dkey and pom Medium<properties>value
            or<filters>Specified in properties file-->
  1. Default attributes in Maven:

${basedir} project root
${version} indicates the project version;
p r o j e c t . b a s e d i r with {project.basedir} same as project.basedir is the same as {basedir};
p r o j e c t . v e r s i o n surface show term order edition book , And {project.version} indicates the project version, and project.version refers to the project version, which is the same as {version}$ {}
Build directory, the default is target ${}, indicating the encoding format of the main source code;
${} indicates the main source code path;
${} indicates the name of the output file;
${} build process output directory. The default is target/classes

11, Maven project dependency and dependency conflict

  1. What is dependency passing

In maven, dependency is transitive. It is assumed that there are three projects: project A, project B and project C. Assuming that C depends on B and B depends on A, it is not difficult to deduce that project C also depends on A according to the characteristics of Maven project dependence

  1. What is dependency conflict:

Because spring webmvc relies on spring core, while spring core relies on Commons logging (1.1.3), and we have introduced Commons loging1.2, resulting in conflicts

  1. Resolution of dependency conflicts:
    3.1 dependency adjustment principle: the first declaration takes precedence (define dependency in pom file, and the dependency declared earlier shall prevail)
    3.2 according to the route, the near one is preferred
    3.3 if two identical jar packages are introduced into the same pom, the last one introduced shall prevail
    3.4 use optional for dependency. optional=true. Dependency will not be passed. The project depends on A. if you want to use a, you need to re introduce it.
    3.5 you can use the exclusions tag to exclude the passed dependencies

3.6 version locking:
3.6.1: use the method of directly locking the version to determine the version of the dependent jar package. After version locking, the dependent declaration order or dependent version will not be considered
3.6.2: version locking method:

1.stay dependencyManagement Lock dependent version in tag 
2.stay dependencies The label declares the to be imported maven coordinate
  1. Build maven project by module
    4.1 sub module construction maven engineering analysis
    Regardless of the following splitting method, a parent project is usually provided to extract some public code and configuration into the parent project for unified management and configuration
    4.2 inheritance of Maven project
    4.2.1: the inherited Maven project is usually called the parent project, and the packaging method of the parent project must be pom. Therefore, whether a maven project is the parent project depends on whether the packaging method of the project is pom
    4.2.2: those that inherit other maven parent projects are usually called child projects, and the parent project is inherited through the parent tag in the pom.xml file
    4.3 aggregation of Maven project:
    4.3.1: in the pom.xml file of maven project, tags can be used to aggregate other maven projects. The purpose of aggregation is to carry out unified operation
    4.3.2: for example, there are multiple maven projects after splitting. If you want to package, you need to execute the packaging command for each project, which is very cumbersome. At this time, you can use the modules tag to aggregate these projects into maven projects. When packaging is needed, you only need to execute the packaging command in this project once, and the aggregated projects under it will be packaged

Posted by tentaguasu on Mon, 04 Oct 2021 14:29:01 -0700