First, what is Maven
Maven is based on POM (Project Object Model), the code of the project by a short description, report, document management into a cross-platform project management tool. Maven has two main functions: managing dependencies, project build.
Two, Maven mounting configuration
download
http://maven.apache.org/download.cgi
The installation process can be directly extracted
Configuration environment variable
Then add% MAVEN_HOME% \ bin in the Path
test
In the command line window enter: mvn -v or mvn -version
Configuring Maven
1, the global configuration
Installation directory in Maven conf inside a settings.xml file that Maven is a global configuration file. settings.xml file path set contains local repository, a remote repository server address and other configuration information.
2, User Configuration
Address of the user profile: ~ / .m2 / settings.xml, the default file does not need to copy the directory to the global profile. If the user profile does not exist, the configuration global configuration file is used.
Three, Maven command
Maven command you want to execute in the directory where the pom.xml
- mvn compile
- Compilation;
- After completion of the compilation, will generate target directory, the directory contains the byte code compiled file;
- mvn clean
- Clear command to clear the compiled class files, specifically clear that the target files in the directory;
- After machining, the target directory will be deleted;
- mvn test
- Test command, which will test the source directory to compile;
- After machining, the generated three folders in the target directory: surefire, surefire-reports (test report), test-classes (byte code files for testing);
- mvn package
- Bale;
- After the execution is completed, it will generate a file in the target directory, the file may be jar, war;
- mvn install
- Installation command, will lay the jar package released to the local repository;
- After machining, jar package after the installation will appear in a local warehouse, convenient and other reference works;
- mvn clean compile
- Combined instruction, execute Clean, and then performs the compile, usually applied before performing on-line, remove the test classes;
- mvn clean test
- Combined instruction, execute Clean, then execute Test, typically applied to the test link;
- mvn clean package
- Performed first clean, then execute package, package the item, usually applied before release;
- mvn clean install
- Performed first clean, then execute install, packaged and then publish the project to a local warehouse them;
Four, Maven's core concepts
1, the coordinates
In Maven, the role of the coordinate positioning jar package is a uniquely determined. Maven has a large build the world, through the coordinates, we can quote from the Maven repository specified jar package build.
The composition of Maven coordinates
- groupId: define the name of the current group of Maven
- artifactId: define the name of the current project
- version: custom version of the current project
2, dependency management
Dependent range
Range-dependent relationship with the tab to control classpath scope dependence and compile, test, run dependencies are three main follows:
- compile: default compiler-dependent range, for compiling, testing, Operation of Three classpath are valid;
- test: tests rely range, the test is valid only for the CLASSPATH;
- provided: provided depend on the scope, valid for compiling, testing classpath, but the run is invalid. For example: Since the container has been provided, the operation is not required, such as servlet-api;
- runtime: operating range dependent, for example: jdbc drive;
Transitive dependencies
A case has a project, B, C three modules, Module A module is dependent B, C B module dependencies module, module B then C is directly dependent on the module, the module A module C is indirectly dependent. In other words:
- C directly dependent B
- A indirectly dependent C
We were added as follows pom.xml file B and C modules in the module:
<dependencies> <dependency> <groupId>com.test.maven</groupId> <artifactId>B</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>
<dependencies> <dependency> <groupId>com.test.maven</groupId> <artifactId>A</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>
As it can be seen from the figure, although not directly dependent on the C A, but through the profile dependent, class C and Method A can also be called;
Then we introduce Junit4.12 in pom.xml file A module, as follows:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> </dependencies>
It can be seen: when the label is scope compile, directly dependent and indirectly dependent modules can be used.
to sum up:
- A compile range when A, B and C can be used.
- When the test is the range A, B and C can not be used.
- When the range A is provided, B and C can not be used;
- When the range A is the runtime, B and C can be used;
Reliance conflict
Cross-conflict Pom file
A module file Pom the Junit 4.12 version dependent, while file B Pom module dependent version of Junit 4.10, then B and C will be used Junit 4.10 version, as shown below:
Conflict with a Pom file
Pom file B module dependent of the two versions Junit 4.10 and 4.12 Junit, in places subject to the following version, the principle of proximity.
Optional dependency
Optional dependent on whether the label marked optional, default is false, indicating that the dependence will be passed on, if true, it would not pass it on.
Preclude reliance
C was added Pom file module Exclusions tag dependent negative Junit
3, inheritance
If all modules need to use a jar package, we only need to set up the project at that level Pom file.
The dependence of A, B, C is removed, the reunification of dependent jar package set in Pom project file, all the modules in the project can use the jar package.
Five, Maven repository
What is Maven repository
Unified storage location for all Maven build is shared warehouse. The Maven coordinates define each construct in the warehouse storage path only approximately: groupId / artifactId / version / artifactId-version.packaging;
Classification warehouse
- Local Warehouse
- Default Address: ~ / .m2 / repository;
- Each user has only one local warehouse
- Remote repository
- Central warehouse: Maven default remote repository does not contain copyright resources
- PW: it is a special remote repository, which is set up in the LAN warehouse
- https://maven.aliyun.com/nexus/content/groups/public/ (Ali cloud)
- Individuals and companies can also build their own private servers;
Sixth, introduce remote repository configuration
1, the central warehouse
2, the local PW