Version | date | Revised | describe |
---|---|---|---|
V1.0 | 2025/3/7 | nick huang | Create a document |
background
During the project, for Maven's files, I often use various references and imitations to finally achieve the desired effect.
But in fact, I was a little confused and didn't know which basic configuration achieved the effect.
Today, I will look back and understand the basics of Maven so that it can be more organized in the future.
The easiest Maven project
useIntelliJ IDEA
Create the easiestMaven
Project, you can seeThe contents are as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId>simple-idea-maven-project</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<>8</>
<>8</>
<>UTF-8</>
</properties>
</project>
Through the Windows tree command, view the directory structure of the entire project:
└─src
├─main
│ ├─java
│ │ └─com
│ │ └─nicchagil
│ └─resources
└─test
└─java
The simplest content below
Through the following operations, we can view the simplest "Effevtive POM
」:
1. Right-click the "file content" in "IntelliJ IDEA"
2. Select "Show Effevtive POM"
Viewed "Effevtive POM
”, checked the functions of some of the nodes and added comments in Chinese:
Tips
In order to improve readability, the following Effevtive POM content will be appropriately deleted.
<!-- Effective POM for project -->
<!-- ':simple-idea-maven-project:jar:1.0-SNAPSHOT' -->
<!-- -->
<!-- ======================================================================================== -->
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
<!-- Specify the Maven project object model version that the current file follows -->
<modelVersion>4.0.0</modelVersion>
<!-- Used to uniquely identify a company (or project, team, etc.) -->
<groupId></groupId>
<!-- Uniquely identify a project or module with groupId and version -->
<artifactId>simple-idea-maven-project</artifactId>
<!-- Version that uniquely identifies the project -->
<version>1.0-SNAPSHOT</version>
<properties>
<!-- Specify the version that Java source code follows -->
<>8</>
<!-- Specify the Java virtual machine version that is compatible with the bytecode generated by the Java compiler -->
<>8</>
<!-- Specify the character encoding of the source code file -->
<>UTF-8</>
</properties>
<!-- Warehouse -->
<repositories>
<repository>
<snapshots>
<enabled>false</enabled> <!-- means disable support for snapshot versions in this repository -->
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>/maven2</url> <!-- The URL (/maven2) configured here is Maven central repository -->
</repository>
</repositories>
<!-- Plug-in Repository -->
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy> <!-- The update policy for the release version is never, that is, the release version plug-in in the repository will never be checked for updates -->
</releases>
<snapshots>
<enabled>false</enabled> <!-- Disable support for snapshot version plugins in this repository -->
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>/maven2</url>
</pluginRepository>
</pluginRepositories>
<!-- Project construction process -->
<build>
<!-- Global build configuration -->
<!-- Specify the directory of the project's source code, from which Maven obtains the source code at compile time -->
<sourceDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\src\main\java</sourceDirectory>
<!-- No corresponding information for scriptSourceDirectory was found -->
<scriptSourceDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\src\main\scripts</scriptSourceDirectory>
<!-- Specify the directory for the project test code -->
<testSourceDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\src\test\java</testSourceDirectory>
<!-- Specify the output directory of the compiled class file -->
<outputDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\target\classes</outputDirectory>
<!-- Specify the class file output directory after the test code is compiled -->
<testOutputDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\target\test-classes</testOutputDirectory>
<!-- Project Resource File -->
<resources>
<resource>
<directory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\src\main\resources</directory>
</resource>
</resources>
<!-- Resource files related to project testing -->
<testResources>
<testResource>
<directory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\src\test\resources</directory>
</testResource>
</testResources>
<!-- build output directory -->
<directory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\target</directory>
<!-- The name of the final file generated after the project is packaged (excluding the extension) -->
<finalName>simple-idea-maven-project-1.0-SNAPSHOT</finalName>
<!-- Centrally manage and configure the plug-in version, configuration and other information. Usually located in the parent project, providing a unified plug-in configuration template for child modules. The configuration of this node will not take effect directly. The submodule will only take effect if it is required.
<pluginManagement>
<plugins>
<!-- Support for Apache Ant tasks -->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- Used to package project output and related dependencies into separate distribution files to facilitate project distribution and deployment -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
</plugin>
<!-- Dependencies for managing and operating projects -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
</plugin>
<!-- Used to simplify the release process of the project -->
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.1</version>
</plugin>
</plugins>
</pluginManagement>
<!-- Plugins that should be used in the project construction process -->
<plugins>
<!-- Used to clean up files and directories generated during project construction -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Used to process resource files in the project. The resource files will be copied to the specified output directory, and these resource files can be filtered and other operations -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Core plugin for creating JAR (Java Archive) files -->
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- for compiling Java source code -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Used to perform unit tests and integration tests during project construction -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Used to install output generated by project builds (such as JAR files) to the local Maven repository -->
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>default-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Used to deploy the product built by the project to a remote Maven repository -->
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Site document used to generate the project -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
<!-- Note: Because the configuration content of maven-site-plugin is large, in order to improve readability, the configuration of maven-site-plugin has been omitted -->
<!-- The configuration of maven-site-plugin has been omitted -->
</plugin>
</plugins>
</build>
<!-- Display for configuring the generation of project reports -->
<reporting>
<outputDirectory>D:\idea_workspace\maven-test-project\simple-idea-maven-project\target\site</outputDirectory>
</reporting>
</project>
Common commands for Maven
By reading the aboveEffevtive POM
",At onceEasy to understandThe basic configuration of the Maven command we often use is corresponding to the default plug-in above.
For example: we usemvn clean
The command, which actually works is Maven's default pluginmaven-clean-plugin
。
Common commands for Maven:
-
mvn clean
: Clean up files and directories generated by project build -
mvn compile
: Compile the source code of the project -
mvn test
: Execute the test of the project -
mvn package
: Package the compiled code of the project into a distributed format, such as a JAR package -
mvn install
: Install the packaged product in the local Maven warehouse -
mvn deploy
: Deploy the packaged product to the remote Maven repository
Subsequent
In the future, if you have time, you will understand several ways of relating Maven projects (such as dependency, inheritance, aggregation), and their applicable scenarios.
at last
I am not talented and have limited knowledge. If there are any mistakes, please feel free to correct me.
If this article is helpful to you, rememberOne-click three consecutive
”(“Like
”、“Comment
”、“collect
")oh!