<build> tag
The <build> tag is an important tag in the file that is used to configure the build process of a Maven project. Under the <build> tag, you can configure the build-related settings, including source code directory, output directory, plug-in configuration, etc.
The following are the detailed usage methods and applicable scenarios of the <build> tag:
<build> <!-- Configure the source code directory of the project,Default is "src/main/java" --> <sourceDirectory>src/main/java</sourceDirectory> <!-- Configure the test code directory for the project,Default is "src/test/java" --> <testSourceDirectory>src/test/java</testSourceDirectory> <!-- Configure the resource file directory of the project,Default is "src/main/resources" --> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> <!-- Configure the project's test resource file directory,Default is "src/test/resources" --> <testResources> <testResource> <directory>src/test/resources</directory> </testResource> </testResources> <!-- Configure the output directory of the project,Default is "target/classes" --> <outputDirectory>target/classes</outputDirectory> <!-- Configure the test output directory of the project,Default is "target/test-classes" --> <testOutputDirectory>target/test-classes</testOutputDirectory> <!-- A list of plug-ins for configuring the project,Used to customize the project construction process --> <plugins> <plugin> <!-- Coordinate information of the plug-in --> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <!-- Plugin configuration information --> <configuration> <!-- Configuration plugin parameters --> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- Other plugins can be declared --> </plugins> <!-- Other build-related configurations --> </build>
Applicable scenarios:
1. Specify the source code and test code directory:
Use the <sourceDirectory> and <testSourceDirectory> tags to customize the source code and test code directory of the project. If the project structure is different from the default directory structure (such as multi-module projects, etc.), you can use these configurations to specify the actual code directory.
2. Configure the resource file directory:
The <resources> and <testResources> tags allow you to specify the resource file directory of the project and the test resource file directory. Resource files usually contain configuration files, attribute files, templates, etc., and the location of resource files can be adjusted as needed.
3. Configure the output directory:
Use the <outputDirectory> and <testOutputDirectory> tags to configure the output directory of the project and the test output directory. The compiled class files and test class files will be output to these directories.
4. Custom build process:
The <plugins> tag is used to configure the Maven plug-in used in the project. By customizing the plug-in's goals and parameters, more complex construction processes can be implemented, such as code generation, compression, static analysis, etc.
5. Use other build-related configurations:
You can also use other build-related configurations within the <build> tag, such as declaring the Maven extension to use when building a project using the <extensions>` tag, or specifying the default build target using the <defaultGoal> tag.
Summary: The <build> tag is used to configure the construction process of Maven projects, including source code directory, output directory, resource file directory, plug-in list, etc. By configuring under this tag, the construction process can be customized according to the project's needs, thereby achieving more flexible and efficient project construction.
Maven plugins (Plugins) are tools for extending and customizing the Maven build process. The Maven plug-in can help perform various tasks, such as compiling code, running tests, packaging projects, deploying applications, etc. In the file, configure the Maven plug-in via the <plugins> and <plugin> tags.
Plugins
1. Declare plugin dependencies:
Within the <build> tag, there is usually a <plugins>` tag that declares the plugin to use. Under the <plugins> tag, each plugin is defined by a <plugin> subtitle, which can contain the following information:
- groupId: The Group ID of the plugin, usually .
- artifactId: The Artifact ID of the plug-in, identifying a specific plug-in.
- version: The version number of the plugin.
Example:
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> <!-- Other plugins can be declared --> </plugins> </build>
2. Configure the plug-in target (Goal):
The functions of the Maven plug-in are implemented through Goals, and a plug-in can have multiple goals. The goal is the execution unit of the plug-in. By specifying the target in the plug-in configuration, different functions of the plug-in can be executed as needed.
Example:
<plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>
In the above example,maven-compiler-pluginThe goal of the plugin is to compile Java Code。 The <configuration> subtag is used to configure the target of the plugin, specifying the source code version and target version of Java compiled.
3. Custom plug-in configuration:
Most Maven plugins support custom configurations, which can be achieved by specifying parameters within the <configuration> subtitle. The specific configuration of a plug-in depends on the functionality of the plug-in itself and the supported configuration options.
Example:
<plugin> <groupId></groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> </archive> </configuration> </plugin>
In the example above, the maven-jar-plugin plugin is used to create a JAR file and package the dependency library into the JAR via custom configuration.
4. Execute plugin target:
Execute the plug-in function by entering the mvn command in the command line and adding the target name of the plug-in. For example, to execute the compile target (compile code) of maven-compiler-plugin, you can use the following command:
mvn compile
This will trigger the Maven compile plugin to compile the source code of the project.
5. Lifecycle binding:
Maven plugins are often bound to the lifecycle of a build, which means that certain plugin goals are automatically executed during a specific build phase. For example, the compile target of the maven-compiler-plugin plugin is bound to the compile stage of Maven build, so when the mvn compile command is executed, the compiled code target will be automatically executed.
Summary: The Maven plug-in implements project building and other customization operations by declaring and configuring the plug-in in a `` file, and then executing the plug-in goals using the command line. Plug-ins are a key component of Maven builds, and they provide developers with rich features to help automate the build process and other development tasks.
There are many plug-ins in Maven that can be used for different build tasks and workflows. Here, I list some common Maven plugins and their usage scenarios, and provide simple use cases and comments.
1. maven-compiler-plugin plugin
- Usage scenario: Compile the Java source code of the project.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>
Note: The above configuration specifies that the source code is compiled using Java 8 and set the generated bytecode and target version to Java 8.
2. maven-jar-plugin plugin
- Usage scenario: Create a JAR file.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> </archive> </configuration> </plugin>
Note: The above configuration adds classpath information to the JAR file and places the dependency library in the "lib/" directory within the JAR.
3. maven-surefire-plugin plugin
- Usage scenario: Run unit tests of the project.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <includes> <include>**/*</include> </includes> </configuration> </plugin>
Note: The above configuration specifies running all unit test classes ending with "".
4. maven-assembly-plugin plugin
- Usage scenario: Create a custom distribution package, such as an executable JAR file that contains all dependencies.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin>
Note: The above configuration creates an executable JAR file with all dependencies.
5. maven-clean-plugin plugin
- Usage scenario: Clean up temporary files and directories generated by project build.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin>
Note: The above configuration can use the `mvn clean` command to clean up temporary files and directories generated by project builds.
6. maven-install-plugin plugin
- Usage scenario: Install the project to the local Maven repository.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-install-plugin</artifactId> <version>3.0.0-M1</version> </plugin>
Note: The above configuration can be used to install the project to the local Maven repository using the `mvn install` command.
7. maven-deploy-plugin plugin
- Usage scenario: Deploy project build results to a remote Maven repository, usually a private or public remote repository.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.0.0-M1</version> </plugin>
Note: The above configuration is used to deploy build results to a remote Maven repository, usually used with the `mvn deploy` command.
8. maven-shade-plugin plugin
- Usage scenario: Create an executable JAR file with all dependencies and can exclude conflicting dependencies.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <configuration> <!-- Configuration exclusion dependencies --> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </plugin>
Note: The above configuration creates an executable JAR file with all dependencies and excludes some conflicting dependencies.
9. maven-site-plugin plugin
- Usage scenario: Generate project site documents, including project information, dependencies, test reports, etc.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-site-plugin</artifactId> <version>3.12.1</version> </plugin>
Note: The above configuration is used to generate project site documents and can be executed using the `mvn site` command.
10. maven-checkstyle-plugin plugin
- Usage scenario: Perform static code analysis of project code to check whether it complies with coding specifications.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.1.2</version> <configuration> <!-- Configuration Checkstyle Rules of --> <configLocation></configLocation> </configuration> <executions> <!-- Execute in the verification phase Checkstyle examine --> <execution> <id>validate</id> <phase>validate</phase> <goals> <goal>check</goal> </goals> </execution> </executions> </plugin>
Note: The above configuration is used to perform Checkstyle checks during the verification phase, and you can customize the Checkstyle's rule file.
11. maven-release-plugin plugin
- Usage scenario: Release the official version of the project, including tagging, upgrading the version number, etc.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-release-plugin</artifactId> <version>3.0.0-M1</version> <configuration> <!-- Information about configuring the release version --> </configuration> </plugin>
Note: The above configuration is used to configure the relevant information about the release version. You can use the mvn release:prepare and mvn release:perform commands to execute the release process.
12. maven-assembly-plugin plugin
- Usage scenario: Create a custom distribution package that can contain executable JAR files and other resource files for the project.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <!-- Description of configuration package --> <descriptor></descriptor> </configuration> <executions> <execution> <!-- The package is executed during the packaging phase --> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
Note: The above configuration is used to perform the creation of a custom distribution package during the packaging phase. You can define the content and structure of the distribution package through the `` file.
13. maven-javadoc-plugin plugin
- Usage scenario: Generate a Javadoc document for the project, which describes the interface and comments of the code.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.3.0</version> <configuration> <!-- Configuration Generation Javadoc Related settings --> </configuration> </plugin>
Note: The above configuration is used to configure the relevant settings for generating Javadoc. You can use the `mvn javadoc:javadoc` command to execute the generation of Javadoc documents.
14. maven-antrun-plugin plugin
- Usage scenario: Execute Ant tasks during the Maven build process, which can be used to perform custom build tasks.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-antrun-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>custom-task</id> <phase>compile</phase> <configuration> <!-- Configure executed Ant Task --> <target> <echo message="Executing custom task..."/> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
Note: The above configuration is used to execute a custom Ant task during the compilation phase, and here is a simple output of a message.
15. maven-resources-plugin plugin
- Usage scenario: Process the project's resource files, such as copying, filtering, replacing, etc.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <configuration> <!-- Configure resource file processing rules --> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </configuration> </plugin>
Note: The above configuration is used to filter resource files in the `src/main/resources` directory, replace the attribute placeholders, etc.
16. maven-enforcer-plugin plugin
- Usage scenario: Enforce specific build rules, such as enforce specific Java versions.
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>3.0.0-M3</version> <executions> <execution> <id>enforce-java-version</id> <goals> <goal>enforce</goal> </goals> <configuration> <!-- Configuring mandatory rules --> <rules> <requireJavaVersion> <version>1.8</version> </requireJavaVersion> </rules> </configuration> </execution> </executions> </plugin>
Note: The above configuration is used to mandate the Java 8 version.
17. maven-war-plugin plugin
- Usage scenario: Package the project into a WAR file for deployment to Servlet containers (such as Tomcat, Jetty, etc.).
- Use case:
<plugin> <groupId></groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.1</version> <configuration> <!-- Configuration WAR Properties of the file --> </configuration> </plugin>
Note: The above configuration is used to configure the properties of WAR files, such as web resource directories, web application manifest files, etc.
These are more common Maven plugins and their use cases, use cases, and comments. Maven provides numerous powerful plugins that can meet a variety of build and project management needs. Developers can choose appropriate plug-ins according to the characteristics and requirements of the project and flexibly configure them to improve the efficiency of project development and construction.
This is the article about the use of build and plugin tags in Maven files. For more related Maven build and plugin tag content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!