Summary: We all know that Maven is essentially a plug-in framework. Its core does not perform any specific build tasks. All these tasks are left to plug-ins, such as compiling source code by maven-Compiler-plugin. This article will introduce you to some commonly used Maven plug-ins.



For image download, domain name resolution and time synchronization, clickAlibaba open source mirror site

We all know that Maven is essentially a plug-in framework. Its core does not perform any specific build tasks. All these tasks are left to plug-ins, such as compiling source code by maven-Compiler-plugin. For example, the compile target of maven-compiler-plugin is used to compile the main source code in the SRC /main/ Java/directory. The testCompile target is used to compile test source code in the SRC /test/ Java/directory. Users can invoke Maven plug-in targets in two ways. The first way is to bind the plug-in target to the lifecycle phase, so that users enter only the lifecycle phase at the command line. For example, Maven binds the compile target of maven-Compiler-plugin to the compile lifecycle phase by default. Therefore, the MVN compile command actually locates the compile lifecycle phase first, and then calls the compile target of maven-Compiler-plugin according to the binding relationship. The second option is to specify the plug-in target to execute directly on the command line, for example, MVN archetype:generate is a call to the generate target of maven-archetype-plugin with a colon. This is lifecycle independent. Understanding the basic concepts of Maven plugins above will help you understand how Maven works, but to use Maven more effectively, it’s important to know some common plugins to help you avoid accidentally reinventing the wheel. The Maven community has accumulated a great deal of experience over the years, and with it a mature plugin ecosystem. Maven has two official plugin list, the first column of table GroupId is org.. Apache Maven. Plugins, here most mature plug-in, the specific address is: maven.apache.org/plugins/ind… . The second list of GroupId is org. Codehaus. Mojo, the less the core plug-in, but there are also many useful, the address is: mojo.codehaus.org/plugins.htm… . Here are some of the most commonly used Maven plugins, based on my experience, that work well in different environments and can make your daily build work a lot easier.

maven-antrun-plugin

Maven.apache.org/plugins/mav… Maven-antrun-plugin enables users to run Ant tasks in Maven projects. You can write the Target as Ant directly in the plug-in’s configuration and hand it to the plug-in’s Run Target to execute. This plug-in is especially useful in projects that are migrating from Ant to Maven. In addition, if you find that you need to write some highly customized tasks and feel that Maven is not flexible enough, you can also do it in the Ant way. Maven-antrun-plugin’s run target is usually run tied to the lifecycle.

maven-archetype-plugin

Maven.apache.org/archetype/m… Archtype refers to the skeleton of a project. The first Maven command a beginner can execute is MVN archetype:generate. This allows maven-archetype-plugin to generate a simple skeleton to help developers get started quickly. Someone may have seen some documentation that says MVN archetype: Create, but actually the create goal has been deprecated in favor of generate, which uses an interactive way to prompt the user for the necessary information to create a project, for a better experience. Maven-archetype-plugin also has some other goals to help users define their own project prototypes. For example, if you have a product that needs to be delivered to many customers for secondary development, you can provide them with an Archtype to help them get started quickly.

maven-assembly-plugin

Maven.apache.org/plugins/mav… The purpose of the 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. The specific packaging of files is highly controllable, for example, the user can control the packaging by file level granularity, file set level granularity, module level granularity, and dependency level granularity. Include and exclude configurations are also supported. The Maven-assembly-plugin requires the user to express packaging using a metadata file called assembly.xml, whose single target can be invoked directly from the command line or bound to the lifecycle.

maven-dependency-plugin

Maven.apache.org/plugins/mav… The main use of the Maven-dependency plugin is to help analyze project dependencies. Dependency :list can list the dependencies resolved by the project, while dependency:tree can further describe the dependency tree. Dependency: Analyze alerts you to potential problems with project dependencies and alerts you if you have undeclared dependencies that you use directly. There are a number of goals that help you manipulate dependency files, such as dependency:copy-dependencies, which copies project dependencies from your local Maven repository to a specific folder.

maven-enforcer-plugin

Maven.apache.org/plugins/mav… In a larger organization or team, you can’t ensure that all members are familiar with Maven, that they do a few more stupid things will become very normal, for example, to project the introduction of external SNAPSHOT depends on building and lead to instability, USES a Maven version and inconsistent with people often complain that build a weird problem. Maven-enforcer-plugin can help you avoid such problems by allowing you to create a set of rules to enforce compliance, including setting Java versions, setting Maven versions, disabling certain dependencies, and disabling SNAPSHOT dependencies. Simply configure rules in a parent POM and let everyone inherit them. Maven will report errors when rules are broken. In addition to the standard rules, you can extend the plug-in and write your own rules. The Enforce target of maven-enforcer-plugin is responsible for checking the rule and is bound by default to the validate phase of the lifecycle.

maven-help-plugin

Maven.apache.org/plugins/mav… Maven-help-plugin is a small helper. 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 Settings of the project, respectively. The valid POM is the XML after combining all parent POM (including Super POM). When you are not sure where some information about the POM comes from, you can look at the valid POM. In the same way, you can use help:effective- Settings to verify that your settings. XML configuration does not work. In addition, the Describe goal of maven-help-plugin helps you describe information about any Maven plug-in, as well as the All-profiles goal and active-Profiles goal to help view a project’s Profile.

maven-release-plugin

Maven.apache.org/plugins/mav… The Maven-release-plugin is designed to help automate project release and relies on SCM information in the POM. Release: Prepare is used to prepare for a release. This includes checking for uncommitted code, checking for SNAPSHOT dependencies, upgrading the SNAPSHOT version of a project to a release, tagging the project, and so on. Release: Perform checks out the release source in the tag, builds it and publishes it. The release is a trivial task that involves all sorts of checks, and because it is only occasionally required, it is easy to miss details manually. The Maven-release-plugin makes it very fast, easy and error-proof. The various targets of maven-release-plugin are usually invoked directly from the command line, since release is obviously not part of the daily build life cycle.

maven-resources-plugin

Maven.apache.org/plugins/mav… To make the project structure clearer, Maven distinguishes between Java code files and resource files, with Maven-compiler-plugin being used to compile Java code and Maven-resources-plugin being used to process resource files. The default main resource file directory is SRC /main/resources. Many users need to add additional resource file directories. This can be done by configuring maven-resources-plugin. Maven-resources-plugin (${propertyName}, maven-resources-plugin, ${propertyName}, ${propertyName}, ${propertyName}) You can then pass in the values of the attributes on the command line or through a Profile for different environments for a more flexible build.

maven-surefire-plugin

Maven.apache.org/plugins/mav… Perhaps for historical reasons, the plugin used to perform tests in Maven 2/3 is not maven-test-plugin, but maven-Surefire-plugin. Most of the time, as long as your Test class follows a common command convention (ending with Test, TestCase, or Test), you’ll barely even know the plug-in exists. However, knowing the configuration options of maven-Surefire-Plugin is useful when you want to skip tests, exclude test classes, or use TestNG features. For example, a command such as MVN test-dtest =FooTest has the effect of running only the FooTest test class by controlling the test parameter of the Maven-Surefire-plugin.

build-helper-maven-plugin

Mojo.codehaus.org/build-helpe… By default, Maven only allows you to specify one main Java code directory and one test Java code directory, and while this is a convention that should be followed whenever possible, occasionally you will want to specify multiple source code directories (for legacy projects, for example). The build-helper-maven-plugin’s add-source target serves this purpose, and it is usually bound to the generate-sources phase of the default lifecycle to add additional source directories. It is important to note that this practice is not recommended because it breaks Maven’s conventions, and you may encounter other plug-in tools that adhere strictly to conventions that do not correctly identify the additional source directory. Another useful goal for build-helper-maven-plugin is a attach-artifact. With this goal, you can create an artifact by selecting part of the project file as a classifier and install it to a local repository. It can also be deployed to a remote repository.

exec-maven-plugin

Mojo.codehaus.org/exec-maven-… As the name suggests, the exec-Maven-plugin lets you run any local system application. In certain cases, running an application outside Of Maven may be the simplest solution to the problem. That’s what exec:exec is for. In addition to the exec target, the exec-Maven-plugin provides a Java target that requires you to provide a mainClass parameter, which can then run the mainClass in the same JVM using the current project’s dependencies as the classpath. Sometimes, to simply demonstrate a command-line Java program, you can configure the exec-Maven-plugin parameters in the POM and run the MVN exec: Java command directly to see the effect.

jetty-maven-plugin

Wiki.eclipse.org/Jetty/Featu… In Web development, it’s almost inevitable to open a browser to manually test your application, which typically involves packaging the project into a WAR file, deploying it to a Web container, and then launching the container for validation, which is obviously time consuming. To help developers save time, the Jetty-Maven-plugin was born. It is fully compatible with the Directory structure of maven projects, periodically checks source files, and automatically updates to the built-in Jetty Web container when changes are found. After doing some basic configuration (such as contextPath for Web applications and interval for automatic scanning of changes), you simply execute MVN Jetty :run and modify the code in the IDE. The code is automatically compiled by the IDE to generate the changes. When detected by the Jetty-Maven-plugin and updated to the Jetty container, you can test the Web page directly. It is important to note that the Jetty-Maven-plugin is not an official plugin hosted by Apache or Codehaus, so you need to configure the pluginGroups element in settings. XML. Add the pluginGroup org.mortbay.jetty.

versions-maven-plugin

Mojo.codehaus.org/versions-ma… Many Maven users have encountered the problem that when a project contains a large number of modules, it becomes a chore to update them collectively. Is there an automation tool that can help with this? (Of course, you can use a text manipulation tool like sed, but this is not the scope of this article.) Yes, the version-Maven-plugin provides many goals to help you manage the various versions of your Maven project. For example, the most commonly used command MVN versions: set-dnewversion = 1.1-snapshot will help you update all modules to 1.1-snapshot. The plugin also provides some other useful targets. Display-dependency updates tell you what updates are available for project dependencies; Similar to display-plugin-updates, which tells you about available plugin updates; Then use-latest-versions automatically updates all dependencies to the latest version. Finally, if you are happy with the changes you made, you can use MVN versions: COMMIT or MVN versions: REVERT if you are not.

summary

This article introduces some of the most commonly used Maven plugins. By “commonly” I mean the plugins that need to be configured frequently. In fact, many other plugins are required when we use Maven. Examples include the default compiled plug-in maven-Compiler-plugin and the default packaged plug-in maven-jar-plugin, but because they rarely need to be configured, they are beyond the scope of this article. Knowing the common Maven plugins can help you get your project built with less effort, whereas you can often get frustrated with problems that are difficult to solve. The plugins described in this article cover most Maven users’ daily needs. If you really have a very specific need, it is not difficult to write your own Maven plugin, especially since there are so many open source plugins available. The list of plugins in this article is not a complete list, but you can also browse the list of Maven plugins for Apache and Codehaus Mojo to get a more complete view. Finally, online Maven repository search engines such as search.maven.org/ can help you quickly find Maven plugins that interest you.

This article was adapted from: The Ari Cloud developer Community