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 └─pom.xml
Detailed description of each directory structure:
- src/main/java – store the. java file of the project, that is, the source code of the project.
- src/main/resources – store project resource files, such as configuration files at the end of yaml and properties.
- src/test/java – store all unit test. java files, such as junit test classes
- src/test/resources – test resource file
- targe - the output location of the project. The compiled class file will be output to this directory
- 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.
- 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 project.build.sourceEncoding,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: <properties> <!--Source code compilation jdk edition--> <maven.compiler.source>1.8</maven.compiler.source> <!--To run code jdk edition--> <maven.compiler.target>1.8</maven.compiler.target> <!--Code used for project construction,Avoid Chinese garbled code--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--Code of the generated report--> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> 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: ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--Maven Version of the model,about Maven2 and Maven3 Say,It can only be 4.0.0--> <modelVersion>4.0.0</modelVersion> <!--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 --> <groupId>com.atguigu.gulimall</groupId> <artifactId>gulimall</artifactId> <version>0.0.1-SNAPSHOT</version> <!--name--> <name>gulimall</name> <!--describe--> <description>polymerization</description> <!--pack--> <packaging>pom</packaging> <!--modular--> <modules> <module>gulimall-member</module> <module>gulimall-order</module> <module>gulimall-coupon</module> <module>gulimall-ware</module> <module>gulimall-product</module> <module>renren-fast</module> <module>renren-generator</module> <module>gulimall-common</module> <module>gulimall-gateway</module> </modules> <!--plug-in unit--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins> </build> </project> ``` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--Maven Version of the model,about Maven2 and Maven3 Say,It can only be 4.0.0--> <modelVersion>4.0.0</modelVersion> <!--Coordinates of the parent project--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--Reverse the domain name of the company name or organization name--> <groupId>com.atguigu.gulimall</groupId> <!--Module name--> <artifactId>gulimall-order</artifactId> <!--Version number--> <version>0.0.1-SNAPSHOT</version> <!--name--> <name>gulimall-order</name> <!--describe--> <description>Grain mall-Order service</description> <!--configuration information--> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties> <!--rely on--> <dependencies> <dependency> <groupId>com.atguigu.gulimall</groupId> <artifactId>gulimall-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
3, Maven warehouse
- 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:
<localRepository>G:\devSoft\apache-maven-3.6.3\maven_repository</localRepository>
- Remote warehouse:
1. Central warehouse: a remote warehouse address is built in Maven: http://repo1.maven.org/maven2
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
- Clean Lifecycle: do some cleanup work before the real build
- Default Lifecycle: the core part of building, compilation, testing, packaging, deployment, etc
- 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
- mvn clean: delete the target directory, but the packages install ed in the warehouse will not be deleted
- mvn compile: compile (execute the code under the main program)
- mvn test: Test (not only compiled the code under src/main/java, but also compiled the code under java/test/java)
- 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)
- 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)
- 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
- 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 - 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 - 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 - 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 - 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 - 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
- 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
- groupId: organization name, usually company or organization domain name in reverse order + project name
- artifactId: module name, usually project name
- Version: version number
8, dependency
- 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
- 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
- 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
- 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
- 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
- Runtime: runtime dependencies are required to run and test the system, but not at compile time.
- System: system range dependency is similar to provide
example:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!--runtime Indicates that the compiler does not use it,Use it at run time--> <scope>runtime</scope> <version>8.0.17</version> </dependency> <!--Import servlet Correlation dependency,request No error reporting--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <!--provided It means to use it at compile time,Not used at runtime--> <scope>provided</scope> </dependency> <--jsp--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <!--system Compilation test is useful and will not run jar--> <dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <scope>system</scope> <optional>true</optional> <version>${java.version}</version> <systemPath>${java.home}/../lib/tools.jar</systemPath> </dependency>
10, Common settings for Maven
- 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:
<properties> <spring.version>4.3.10.RELEASE</spring.version> </properties> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency>
- Variables used in maven system
<properties> <!--Source code compilation jdk edition--> <maven.compiler.source>1.8</maven.compiler.source> <!--To run code jdk edition--> <maven.compiler.target>1.8</maven.compiler.target> <!--Code used for project construction,Avoid Chinese garbled code--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--Code of the generated report--> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties>
- 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
<resources> <!--Path to resource file,Default at ${basedir}/src/main/resources/Directory--> <directory>src/main/resources</directory> <!--A matching pattern for a set of file names,The matched resource files will be processed by the build process--> <includes> <include>**/*</include> <include>*</include> </includes> <!--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--> <filtering>true</filtering> </resource> </resources>
- 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}$ {project.build.directory}
Build directory, the default is target ${project.build.sourceEncoding}, indicating the encoding format of the main source code;
${project.build.sourceDirectory} indicates the main source code path;
${project.build.finalName} indicates the name of the output file;
${project.build.outputDirectory} build process output directory. The default is target/classes
11, Maven project dependency and dependency conflict
- 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
- 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
- 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
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <exclusions> <exclusion> <groupId>org.skyscreamer</groupId> <artifactId>jsonassert</artifactId> </exclusion> </exclusions> </dependency>
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 <dependencyManagement> <dependencies> <dependency> <groupId>com.glory</groupId> <artifactId>glory-auth-entity</artifactId> <version>${project.version}</version> </dependency> <dependencyManagement> 2.stay dependencies The label declares the to be imported maven coordinate
- 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