What is it?#
- Apache Maven is a tool for software project management and automatic building written in Java.
Apache Maven core
What can it do?#
Project Build and Dependency Management#
- Dependency management is completed by adding dependencies in the Project Object Model (POM) file.
Perform operations during the project build lifecycle#
- Lifecycle goal management is completed by configuring plugins in Maven.
Phase | Process | Description |
---|---|---|
Clean | Clean project | Clean up the JAR/WAR and build information produced before cleaning the project. |
Validate | Validate project | Validate that the project is correct and all necessary information is available. |
Compile | Execute compilation | Source code compilation is completed at this stage. |
Test | Test | Run tests using an appropriate unit testing framework (e.g., JUnit). |
Package | Package | Create JAR/WAR packages as mentioned in the pom.xml. |
Verify | Verify | Check the results of integration tests to ensure quality standards are met. |
Install | Install | Install the packaged project into the local repository for use by other projects. |
Deploy | Deploy | Copy the final project package to a remote repository to share with other developers and projects. |
How to use it?#
Installation and Configuration#
Currently, IDEs are integrated with Maven, and the general purpose of installing Maven is to execute Maven's lifecycle operations from the command line to complete project compilation and building.
Download#
Official download address: http://maven.apache.org/download.cgi
C:.
│ LICENSE
│ NOTICE
│ README.txt
│
├─bin
│ m2.conf
│ mvn
│ mvn.cmd
│ mvnDebug
│ mvnDebug.cmd
│ mvnyjp
│
├─boot
│ plexus-classworlds-2.8.0.jar
│ plexus-classworlds.license
│
├─conf
│ │ settings.xml
│ │ toolchains.xml
│ │
│ └─logging
│ simplelogger.properties
│
└─lib
Configuration#
Environment Variable Configuration#
- Add a system variable MAVEN_HOME, with the value set to the root directory where Maven is extracted.
- Add a record to the path environment variable %MAVEN_HOME%\bin.
- Verify that the environment variable configuration is effective.
Open cmd and enter mvn -version to check if Maven's version can be queried, indicating that Maven is configured successfully.
Maven Repository Configuration#
- When a dependency JAR package needs to be introduced, Maven will look for it in the local repository, which is by default in the repository directory under the .m2 directory in the user directory.
- If the corresponding resource is not available locally, it will be downloaded from the Central Repository.
- Since the Central Repository is an overseas server, it is necessary to add the Alibaba Cloud mirror to the Maven configuration.
- Modification of the Central Repository
In the conf directory of the Maven directory, modify the settings.xml file to add the following content in the mirrors tag:
After modification, copy the settings.xml file to the .m2 directory so that it can be called globally.
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
Tip
xml (extensible markup language)
content
In xml, comments are indicated by .
- Modification of the Local Repository
<localRepository>/path/to/local/repo</localRepository>
Maven Configuration in IDEA#
In the File->Settings menu in IDEA, find the Build, Execution, Deployment menu, and in the Build tools menu
Click on Maven and confirm that the User settings file is the settings.xml file in the local .m2 directory, and the repository directory is the repository directory in the local .m2 directory.
Usage#
Managing Dependencies#
Add dependencies in the project element using dependency.
Maven introduces dependencies:
- Look in the local repository.
- Look in the Central Repository.
- Remote repository (the repository address set up in your internal network).
Project Compilation Environment Settings#
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Managing Lifecycle#
Use different Maven build plugins to complete lifecycle actions.
- Execute Test Code
- Running test code in the project can be completed by executing the test lifecycle directly.
- In projects with written test code, Maven will automatically execute the test code in the test directory when completing the packaging operation.
- If you want to customize the execution of certain test codes or have other requirements, you can configure it by introducing the surefire plugin.
- Add the following plugin in the element of the element in the pom file:
<!-- Test plugin surefire-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.19</version>
</dependency>
</dependencies>
<!-- The configuration element can be omitted, and it will default to run all files under /src/test/java -->
<!-- In the configuration, you can configure the class file names to be executed in the src/test/java directory -->
<configuration>
<!-- If you want to skip unit test execution, add skipTests-->
<!-- <skipTests>true</skipTests>-->
<!-- Configure the test code to be executed, just configure the class name. Supports using * as a wildcard, for example, *Test indicates files ending with Test, and also supports regular expressions -->
<includes>
<include>ErrorLoginTest</include>
<include>MavenJunitTest</include>
</includes>
<!-- Configure test classes that should not be executed -->
<excludes>
<exclude>LoginSuite</exclude>
</excludes>
</configuration>
</plugin>
- Create an Executable JAR
Can be run directly using the java -jar command.
- If you only need to package the project as a Jar without generating an executable Jar, just sequentially execute the clean and package operations.
- If you need to produce an executable Jar package, you need to configure the main class entry point in the pom file by adding the element, adding the plugin configuration, and adding the assembly plugin configuration as follows:
<build>
<plugins>
<plugin>
<artifactId> maven-assembly-plugin </artifactId>
<configuration>
<!-- Indicates that the jars used in the project dependencies are needed -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- If an executable jar is needed -->
<archive>
<manifest>
<!--Fill in the class to run here. -->
<mainClass>com.testing.class12</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>