Article directory
1. Introduction to Maven
What is Maven
Maven is a tool specifically used to manage and build Java projects. Its main functions are:
- Provides a standardized project structure
- Provides a set of dependency management mechanisms (no need to manually import jar packages)
- Provides a set of standardized build processes (compile, test, package, release...)
Maven standardized project structure
Maven provides a set of standardized project structures. The project structures built by all IDEs using Maven are exactly the same. Maven projects created by all IDEs can also be used universally.
2. Maven warehouse
Warehouse classification
-
**Local warehouse:** A directory on your own computer
-
**Central Warehouse: **The only warehouse in the world maintained by the Mavne team
Address: https://repo1.maven.org/maven2
-
**Remote warehouse (private server): **Private warehouse generally built by the company team
Implementation process
When the corresponding dependent jar package is introduced using coordinates in the project, it will first check whether the local warehouse has the corresponding jar package.
- If not, go to the central warehouse to download the corresponding jar package to the local warehouse.
- If so, reference it directly in the project
To build a remote warehouse, the search order for jar packages is
- Local warehouse --> Remote warehouse --> Central warehouse
3. Maven installation
Maven is a green software, you can install it by unzipping it.
Download location:Maven – Download Apache Maven
- Follow two files
- bin/mvn.cmd executable file
- conf/settings.xml configuration file
- Be careful not to put it in the Chinese directory
4. Maven configuration
Maven three configuration methods
-
Configure the local warehouse: modify line 55 in conf/settings.xml to a specified directory
<localRepository>D:\software\mvn_resp</localRepository>
-
Configure Alibaba Cloud Private Server: Modify the tag on line 160 in conf/settings.xml and add the following subtags to it
<mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
-
Maven uses the jdk1.5 version by default. The label on line 188 can be modified or it can be left unchanged. It is upwardly compatible.
<profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile>
5. IDEA configures Maven
点击file -> Settings -> Build Execution Deployment -> Build Tools -> Maven
Fill in your Mavne address
6. Maven coordinates
what are coordinates
- Coordinates in Maven are unique identifiers of resources
- Use coordinates to define the project or introduce the dependencies needed in the project
The main components of Maven coordinates
- groupId: defines the name of the organization that the current Maven project belongs to (usually the domain name is written in reverse)
- artifactId: defines the current Mavne project name
- version: defines the current project version number
7. IDEA creates Mavne module
Create steps
- Create the module, select Maven, and click Next
- Fill in the module name and coordinate information, click finish, and the creation is completed.
- Write HelloWorld and test run
Creation process
Select the new project and click Maven
Check the box below to modify the module name and coordinate information.
Click Finish again
8. Maven dependency management
Import jar package using coordinates
-
Write tags in pom.xml
<dependencies> </dependencies>
-
Use imported coordinates in labels
<dependencies> <dependency> </dependency> </dependencies>
-
grupId, artifactld, version that defines coordinates
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> </dependencies>
-
Click the refresh button to make the coordinates effective
Use shortcuts to import jar packages
-
Press alt+insert in pom.xml and select Dependency
-
Search for the corresponding coordinates in the pop-up panel, then double-click to select the corresponding coordinates
-
Click the refresh button to make the coordinates effective
Query jar package dependencies online
-
Search dependencies through the official website: https://mvnrepository.com
-
Select the corresponding resources and copy them to the pom.xml file.
**Note:** For the same jar package, the later one will replace the earlier one.
9. Common Maven commands
IDEA operates Maven commands
Order | illustrate |
---|---|
mvn clean | Delete target directory |
mvn compile | Compile main directory |
mvn test | Execute test directory |
mvn package | Package to target directory |
mvn install | Install into local repository |
Case presentation
-
mvn clean
-
mvn compile
After compilation, the target directory will be regenerated.
-
mvn test
There are relatively few tests done here.
-
mvn package
After packaging, a jar package will be generated in the target.
10. Maven life cycle
-
The Maven build project life cycle describes how many events a build process has gone through.
-
Maven divides the project construction life cycle into three sets
- clean: cleaning work
- default: core work, such as compilation, testing, packaging, installation, etc.
- site: generate reports and publish sites
-
There is no way to skip it during the build process
For example: when we are testing, there is no way to skip compilation and directly test.
-
overall life cycle
11. Maven dependency management
By setting the dependency range (scope) of the coordinates, you can set the scope of the corresponding jar package.
When not set, the default is the scope of compile, that is, all paths can be used
dependency scope | Compile classpath (main) | Test classpath(test) | Run classpath(target) | example |
---|---|---|---|---|
compile | AND | AND | AND | logback |
test | - | AND | - | Junit |
provided | AND | AND | - | servlet-api |
runtime | - | AND | AND | jdbc driver |
code demo
-
Import druid
<!-- druid连接池依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> <scope>compile</scope> </dependency>
-
When nothing is configured, both the main directory and the test directory are available.
-
Add scope in pom.xml file
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> <scope>test</scope> </dependency>
-
Found that only the test directory can be used