Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common Maven plug-ins?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the commonly used Maven plug-ins, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

We all know that Maven is essentially a plug-in framework, and its core does not perform any specific build tasks, all of which are left to plug-ins, such as compiling source code by maven- compiler-plugin.

Furthermore, each task corresponds to a plug-in goal, and each plug-in has one or more targets. For example, the compile target of maven- compiler-plugin is used to compile the main source code in the src/main/java/ directory, and the testCompile target is used to compile the test source code in the src/test/java/ directory.

Users can invoke the Maven plug-in target in two ways. The first way is to bind the plug-in target to the lifecycle phase (lifecycle phase), so that the user is only the input lifecycle phase on the command line. For example, Maven binds the compile target of maven-compiler-plugin to the lifecycle phase of compile by default, so the command mvn compile actually locates to the lifecycle phase of compile first, and then invokes the compile target of maven-compiler-plugin according to the binding relationship.

The second way is to specify the plug-in target to be executed directly on the command line. For example, mvn archetype:generate represents the generate target of calling maven-archetype-plugin, which has nothing to do with the life cycle.

Understanding the basic concepts of the above Maven plug-ins can help you understand how Maven works, but to use Maven more efficiently, it is necessary to understand some commonly used plug-ins to help you avoid accidentally reinventing the wheel. For a series of Maven tutorials, you can follow the official account Java technology stack reply Maven to get.

Over the years, the Maven community has accumulated a great deal of experience and has formed a mature plug-in ecosystem. Maven officially has two plug-in lists, the first list of GroupId is org.apache.maven.plugins, where the plug-ins are the most mature, the specific address is: http://maven.apache.org/plugins/index.html. The GroupId of the second list is org.codehaus.mojo, where the plug-ins are less core, but many are very useful, with the address: http://mojo.codehaus.org/plugins.html.

Next, based on my own experience, the author introduces some of the most commonly used Maven plug-ins, each of which has its own excellent performance in different environments. Using them skillfully can make your daily build work get twice the result with half the effort.

Maven-antrun-plugin

Http://maven.apache.org/plugins/maven-antrun-plugin/

Maven-antrun-plugin allows users to run Ant tasks in a Maven project. Users can write Target as Ant directly in the configuration of the plug-in, and then give it to the plug-in's run target to execute. This plug-in is particularly useful in some projects that migrate from Ant to Maven.

In addition, when you find that you need to write some highly customized tasks, and at the same time feel that Maven is not flexible enough, you can also implement it in an Ant way. The run goal of maven-antrun-plugin is usually run with lifecycle bindings.

Maven-archetype-plugin

Http://maven.apache.org/archetype/maven-archetype-plugin/

Archtype refers to the skeleton of the project. The first Maven command executed by Maven beginners may be mvn archetype:generate, which actually allows maven-archetype-plugin to generate a very simple project skeleton to help developers get started quickly.

Some people may also see some documents that write * * mvn * * archetype:create, but in fact the create goal has been deprecated and replaced by the generate goal, which uses an interactive way to prompt the user to enter the necessary information to create a project, and the experience is better.

Maven-archetype-plugin has other goals to help users define their own project prototypes. For example, if you need to deliver a product to many customers for secondary development, you can provide them with an Archtype to help them get started quickly.

Maven-assembly-plugin

Http://maven.apache.org/plugins/maven-assembly-plugin/

The purpose of maven-assembly-plugin is to make a project distribution, which may contain project executables, source code, readme, platform scripts, and so on.

Maven-assembly-plugin supports a variety of mainstream formats such as zip, tar.gz, jar and war. Which files are packaged is highly controllable. For example, users can control packaging by file-level granularity, fileset-level granularity, module-level granularity, and dependency-level granularity. In addition, include and exclude configurations are also supported.

Maven-assembly- plugin requires users to use a metadata file called assembly.xml to represent packaging, and its single target can be invoked directly on the command line or bound to the lifecycle. For a series of Maven tutorials, you can follow the official account Java technology stack reply Maven to get.

Maven-dependency-plugin

Http://maven.apache.org/plugins/maven-dependency-plugin/

The biggest use of maven-dependency-plugin is to help analyze project dependencies, dependency:list can list the list of dependencies that the project will eventually resolve, dependency:tree can further depict the project dependency tree, dependency:analyze can tell you about potential problems with project dependencies, and this goal will warn you if you have directly used but undeclared dependencies.

Maven-dependency-plugin also has many goals to help you manipulate dependency files, such as dependency:copy-dependencies 's ability to copy project dependencies from the local Maven repository to a specific folder.

Maven-enforcer-plugin

Http://maven.apache.org/plugins/maven-enforcer-plugin/

In a larger organization or team, you can't guarantee that all members are familiar with Maven, so it's normal for them to do some stupid things, such as introducing external SNAPSHOT dependencies into the project and causing build instability, using an inconsistent version of Maven and often complaining about weird build problems.

Maven-enforcer- plugin can help you avoid such problems, it allows you to create a series of rules to force people to follow, including setting the Java version, setting the Maven version, banning certain dependencies, prohibiting SNAPSHOT dependencies.

As long as the rule is configured in a parent POM and then inherited by everyone, when the rule is broken, the Maven will report an error. In addition to the standard rules, you can extend the plug-in to write your own rules. The enforce target of maven-enforcer-plugin is responsible for checking the rules, which are bound to the validate phase of the lifecycle by default.

Maven-help-plugin

Http://maven.apache.org/plugins/maven-help-plugin/

Maven-help-plugin is a small helper, and the simplest help:system can print all available environment variables and Java system properties. Help:effective-pom and help:effective-settings are the most useful. They print the valid POM and valid settings of the project, respectively. Effective POM refers to the XML after merging all the parent POM (including Super POM). When you are not sure where some information about POM comes from, you can check the valid POM.

The same goes for valid settings, especially if you find that your configured settings.xml doesn't work, you can use help:effective-settings to verify it. In addition, maven-help-plugin 's describe goals can help you describe information about any Maven plug-in, as well as all-profiles goals and active-profiles goals to help you view the Profile of the project.

Maven-release-plugin

Http://maven.apache.org/plugins/maven-release-plugin/

The purpose of maven-release-plugin is to help automate the release of project versions, which relies on SCM information in POM. 30 commonly used Maven commands, this article is recommended to take a look at.

Release:prepare is used to prepare for release, including checking whether there is uncommitted code, checking whether there are SNAPSHOT dependencies, upgrading the SNAPSHOT version of the project to the RELEASE version, tagging the project, and so on.

Release:perform is to check out the RELEASE source code in the tag, build and release. Release is a very trivial task, it involves all kinds of checks, and because the work is only occasionally needed, it is easy to leave out some details manually, and maven-release-plugin makes the work very fast and easy to make mistakes. The various goals of maven-release-plugin are usually invoked directly from the command line, because version releases are clearly not part of the daily build lifecycle.

Maven-resources-plugin

Http://maven.apache.org/plugins/maven-resources-plugin/

To make the project structure clearer, Maven distinguishes between Java code files and resource files, maven-compiler-plugin is used to compile Java code, and maven-resources-plugin is used to process resource files.

The default main resource file directory is src/main/resources, and many users will need to add additional resource file directories, which can be achieved by configuring maven-resources-plugin.

In addition, resource file filtering is also a major feature of Maven. You can use the Maven attribute in the form of _ ${propertyName} _ in the resource file, and then configure maven-resources-plugin to enable filtering of the resource file, and then you can pass in the value of the attribute through the command line or Profile for different environments to achieve more flexible construction.

Maven-surefire-plugin

Http://maven.apache.org/plugins/maven-surefire-plugin/

Probably due to historical reasons, the plug-in used to perform tests in Maven 2 move 3 is not maven-test-plugin, but maven-surefire-plugin. In fact, most of the time, as long as your test class follows a general command convention (ending with Test, ending with TestCase, or starting with Test), you are almost unaware of the existence of the plug-in.

However, when you want to skip testing, exclude certain test classes, or use some TestNG features, it is useful to understand some of the configuration options for maven-surefire-plugin.

The effect of a command such as mvn test-Dtest=FooTest is to run only the FooTest test class, which is achieved by controlling the test parameter of the maven-surefire-plugin.

Build-helper-maven-plugin

Http://mojo.codehaus.org/build-helper-maven-plugin/

Maven allows you to specify only one main Java code directory and one test Java code directory by default, although this is a convention that should be followed as much as possible, but occasionally you want to be able to specify multiple source directories (for example, to deal with legacy projects). The add-source goal of build-helper-maven-plugin serves this purpose, and it is usually bound to the generate-sources phase of the default life cycle to add additional source directories.

It is important to emphasize that this practice is not recommended because it breaks the Maven convention and may encounter other strictly compliant plug-in tools that fail to correctly identify additional source directories.

Another very useful goal of build-helper-maven-plugin is attach-artifact, with which you can select some project files in the form of classifier to generate accessory components, and install to the local warehouse at the same time, or you can deploy to the remote warehouse.

Exec-maven-plugin

Http://mojo.codehaus.org/exec-maven-plugin/

Exec-maven-plugin is easy to understand, as the name implies, it allows you to run any local system program, in certain cases, running a program outside of Maven may be the easiest solution to the problem, this is the purpose of exec:exec, of course, the plug-in also allows you to configure the relevant program running parameters.

In addition to the exec goal, exec-maven-plugin also provides a java goal, which requires you to provide a mainClass parameter, which can then use the current project dependency as a classpath to run the mainClass in the same JVM. Sometimes, to simply demonstrate a command-line Java program, you can configure the relevant running parameters of exec-maven-plugin in POM, and then run mvn exec:java directly in the command to see how it works.

Jetty-maven-plugin

Http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin

In Web development, it is almost inevitable to open a browser to test the application manually, which is usually packaged into a war file, then deployed into a Web container, and then started for verification, which is obviously very time-consuming.

In order to help developers save time, jetty-maven-plugin came into being. It is fully compatible with the directory structure of Maven projects, can periodically check the source files, and automatically update to the built-in Jetty Web container when changes are found.

After doing some basic configuration (such as the contextPath of the Web application and the interval between automatically scanning changes), you just need to execute mvn jetty:run, and then modify the code in IDE, the code is automatically compiled by IDE, and then updated to the Jetty container after it is detected by jetty-maven-plugin, then you can test the Web page directly.

It is important to note that jetty-maven-plugin is not an official plug-in hosted on Apache or Codehaus, so you need to configure the pluginGroups element of settings.xml to add the pluginGroup org.mortbay.jetty when you use it.

Versions-maven-plugin

Http://mojo.codehaus.org/versions-maven-plugin/

Many Maven users have encountered the problem that when a project contains a large number of modules, it becomes tiresome to update the version for them collectively. Is there any automation tool that can help with this? (of course you can use text manipulation tools such as sed, but beyond the scope of this article.) the answer is yes, versions-maven- plugin provides a number of goals to help you manage various versions of Maven projects.

For example, the most commonly used command mvn versions:set-DnewVersion=1.1-SNAPSHOT can help you update the version of all modules to 1.1-SNAPSHOT. The plug-in also provides some other useful goals, display-dependency- updates can tell you which updates the project depends on; similar display-plugin-updates can tell you which plug-ins are available; and then use- latest-versions can automatically help you upgrade all dependencies to the latest version. Finally, if you are satisfied with the changes you have made, you can use mvn versions:commit to submit, or you can use mvn versions:revert to undo if you are not satisfied.

Thank you for reading this article carefully. I hope the article "what are the commonly used Maven plug-ins" shared by the editor will be helpful to you? at the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report