As we all know, MAVEN is essentially a plug-in framework, and its core does not perform any specific build tasks, all of which are given to the plug-in to complete, such as compiling the source code is done by Maven-compiler-plugin. Further, each task corresponds to a plug-in target (goal), each plug-in will have one or more targets, such as Maven-compiler-plugin's compile target to compile src/main/java/ the main source code located in the directory, The testcompile target is used to compile src/test/java/ the test source located in the directory.
Users can invoke the MAVEN plug-in target in two ways. The first is to bind the plug-in target to the life cycle phase (lifecycle phase) so that the user simply enters the life cycle stage at the command line, for example, MAVEN defaults to Maven-compiler-plugin compile target with The compile lifecycle phase is bound, so the command MVN compile is actually anchored to the compile life cycle phase and then calls Maven-compiler-plugin's compile target based on the binding relationship. The second way is to specify the plug-in target to execute directly on the command line, such as mvn archetype:generate , which represents the generate target of calling Maven-archetype-plugin. This method of invocation with a colon is independent of the life cycle.
Knowing the basic concepts of the Maven plugin above will help you understand how MAVEN works, but for more efficient use of MAVEN, it may be necessary to know a few common plugins that will help you avoid accidentally reinventing the wheel. Over the years, the MAVEN community has accumulated a lot of experience and has evolved into a mature plugin ecosystem. MAVEN official has two plug-ins list, the first list of GroupID for Org.apache.maven.plugins, where the most mature plug-ins, the specific address is: http://maven.apache.org/plugins/index.html. The second list of GroupID is Org.codehaus.mojo, where the plug-in is not so core, but also a lot of very useful, its address is: http://mojo.codehaus.org/plugins.html.
Next, I will introduce some of the most commonly used maven plugins according to my own experience, they have their own excellent performance in different environments, and use them skillfully to make your daily construction work more effective.
Maven-antrun-plugin
http://maven.apache.org/plugins/maven-antrun-plugin/
Maven-antrun-plugin enables users to run ant tasks in a Maven project. The user can write the target directly in the configuration of the plug-in ant, and then hand it over to the run target of the plugin. This plug-in is especially useful in some projects migrated by Ant to Maven. Also, when you find that you need to write some highly customized tasks and feel that Maven is not flexible enough, you can do it in Ant's Way. The run target of the maven-antrun-plugin is typically run with the life cycle bindings.
Maven-archetype-plugin
http://maven.apache.org/archetype/maven-archetype-plugin/
Archtype refers to the skeleton of the project, the Maven novice who started the mvn is probably the archetype:generate, This actually allows Maven-archetype-plugin to build a very simple project skeleton that helps developers get started quickly. It may also be seen that some documents write MVN archetype:create, but in fact the create target has been deprecated and replaced by the generate target, which uses an interactive way to prompt the user to enter the necessary information to create the project and experience better. Maven-archetype-plugin also has other goals that help users define project prototypes themselves, such as when you have two development of a product that needs to be delivered to many customers, and you can provide them with a 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 bundle, which may contain the project's executable files, 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, and specifically packages which files are highly controllable, such as the granularity of the file-level, the granularity of the file-set level, the granularity at the module level, As well as the granularity of the dependency level packaging, in addition, the inclusion and exclusion configurations are also supported. Maven-assembly-plugin requires the user to use a assembly.xml metadata file named to describe the package, its single target can be invoked directly on the command line, or it can be bound to the life cycle.
Maven-dependency-plugin
http://maven.apache.org/plugins/maven-dependency-plugin/
Maven-dependency-plugin's biggest use is to help analyze project dependencies, anddependency:list to list the dependencies that the project ultimately resolves to,dependency:tree Can further depict the project dependency tree,dependency:analyze can tell you that the project relies on potential problems, and if you have a direct use of undeclared dependencies, the target warns. Maven-dependency-plugin also has many goals to help you manipulate dependent files, such as dependency:copy-dependencies to copy Project dependencies from a local MAVEN repository to a specific folder.
Maven-enforcer-plugin
http://maven.apache.org/plugins/maven-enforcer-plugin/
In a slightly larger organization or team, you cannot guarantee that all members are familiar with Maven, and that they will become normal when they do something more foolish, such as introducing an external snapshot dependency on the project and causing the build to be unstable. Use a version of Maven that is inconsistent with everyone and often complain about the weird build problem. Maven-enforcer-plugin can help you avoid such problems, it allows you to create a series of rules to enforce compliance, including setting Java version, setting Maven version, prohibiting certain dependencies, and prohibiting snapshot dependencies. As long as the rules are configured in a parent pom and then let everyone inherit, MAVEN will give an error when the rules are compromised. In addition to the Standard Rules, you can also extend the plugin 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 the available environment variables and Java System Properties. help:effective-pom and help:effective-settings are most useful for printing the project's effective pom and effective settings, respectively, A valid POM is a combination of all the parent pom (including Super POM) XML, you can view the valid POM when you are not sure where some of the information from the Pom comes from. Effective settings is the same, especially if you find that the settings.xml you have configured does not take effect, you can use help:effective-settings to verify it. In addition, Maven-help-plugin's describe goal can help you describe any MAVEN plugin's information, as well as all-profiles goals and active-profiles goals to help you view the project's profile.
Maven-release-plugin
http://maven.apache.org/plugins/maven-release-plugin/
The purpose of Maven-release-plugin is to help automate the project release, which relies on SCM information in the POM. Release:prepare is used to prepare version releases, including checking for uncommitted code, checking for snapshot dependencies, upgrading the snapshot version of the project to release, tagging the project, and so on. Release:perform is the release source code in the Checkout tab, built and published. Release is very trivial work, it involves a variety of checks, and since the work is only occasional need, so manual operation is easy to omit some details, Maven-release-plugin makes the work very fast and easy, not error-prone. The various goals of Maven-release-plugin are usually called directly at the command line, because release 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 treats Java code files and resource files differently, Maven-compiler-plugin is used to compile Java code, and Maven-resources-plugin is used to process resource files. The default master resource file directory is src/main/resources , many users will need to add additional resource files directory, this time can be configured by Maven-resources-plugin to achieve. In addition, resource file filtering is also a major feature of MAVEN, you can use the Maven attribute in the ${propertyname} form in the resource file, and then configure Maven-resources-plugin to enable filtering of the resource file. You can then pass in the values of the properties through the command line or profile for different environments for a more flexible build.
Maven-surefire-plugin
http://maven.apache.org/plugins/maven-surefire-plugin/
Perhaps for historical reasons, the plugin used to perform the test 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 (end with test, end with TestCase, or start with test), you'll almost never know the plug-in exists. However, when you want to skip tests, exclude certain test classes, or use some testng features, it is useful to understand some of the configuration options for Maven-surefire-plugin. For example, the effect of a command such as mvn test-dtest=footest is to run only the Footest test class, which is implemented by controlling the Maven-surefire-plugin test parameter.
Build-helper-maven-plugin
http://mojo.codehaus.org/build-helper-maven-plugin/
Maven only allows you to specify a main Java code directory and a test Java code directory by default, although this is a convention that should be adhered to as much as possible, but occasionally you will want to be able to specify multiple source directories (for example, to deal with legacy projects). Build-helper-maven-plugin's add-source goal is to serve this purpose, which is usually tied to the generate-sources phase of the default lifecycle to add additional source directories. It should be emphasized that this practice is not recommended because it destroys MAVEN's conventions and may encounter other strict compliance-compliant plug-in tools that do not correctly identify additional source directories.
Another very useful goal of Build-helper-maven-plugin is Attach-artifact, which allows you to select some of the project files in the form of classifier to generate the subordinate artifacts and install them to the local repository at the same time. You can also deploy to a remote repository.
Exec-maven-plugin
http://mojo.codehaus.org/exec-maven-plugin/
Exec-maven-plugin well understood, as the name implies, it allows you to run any local system programs, in some specific cases, running a MAVEN external program may be the simplest problem solution, this is the use of exec:exec , of course, The plugin also allows you to configure the relevant program run parameters. In addition to the exec target, Exec-maven-plugin also provides a Java target that requires you to provide a mainclass parameter, which can then be used to run the MainClass in the same JVM, taking advantage of the dependencies of the current project as Classpath. Sometimes, in order to simply demonstrate a command-line Java program, you can configure the Exec-maven-plugin relevant running parameters in the Pom, and then run mvn Exec:java directly in the command to see the effect of the run.
Jetty-maven-plugin
Http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin
When it comes to web development, it's almost impossible to open a browser to manually test an app, which is often a time-consuming way to package a project into a war file, deploy it to a Web container, and start a container for validation. To help developers save time, Jetty-maven-plugin was born with a fully compatible MAVEN project directory structure that periodically checks the source files and automatically updates to the built-in jetty Web container once changes are found. After doing some basic configuration (such as the ContextPath of the Web app and the time interval for automatic scan changes), you simply execute mvn jetty:run and then modify the code in the IDE, and the code is automatically compiled by the IDE, resulting in changes. Then the jetty-maven-plugin is detected and updated to the jetty container, so you can test the Web page directly. It is important to note that Jetty-maven-plugin is not an official plug-in that is hosted on Apache or Codehaus, so it requires additional configuration settings.xml of plugingroups elements when used. Join Org.mortbay.jetty this plugingroup.
Versions-maven-plugin
http://mojo.codehaus.org/versions-maven-plugin/
Many MAVEN users have experienced the problem that when a project contains a large number of modules, it becomes a chore to update the version collectively for them, and is there an automated tool that can help you do this? (Of course you can use text manipulation tools such as SED, but not in the scope of this article) the answer is yes, Versions-maven-plugin offers many goals to help you manage the various version information of your MAVEN project. For example, the most commonly used command, mvn versions:set-dnewversion=1.1-snapshot , will help you update the version of all modules to 1.1-snapshot. The plugin also provides some other useful goals that display-dependency-updates can tell you what updates are available for the project, and similar display-plugin-updates can tell you which plugin updates are available, and then use- Latest-versions can automatically help you to upgrade all dependencies to the latest version. Finally, if you are satisfied with your changes, you can use the mvn versions:commit submission, or you can use mvn versions:revert to revoke it if you are not satisfied.
Summary
This article describes some of the most commonly used MAVEN plug-ins, where "common" refers to plugins that often need to be configured, and in fact many other plugins are required when we use MAVEN, such as The default compilation plug-in Maven-compiler-plugin and the default packaged plug-in are maven-jar-plugin, but because they are rarely required to be configured, they are not covered in this article. Understanding commonly used Maven plugins can help you get the most out of your project-building tasks, and you may be frustrated by the often difficult problems you encounter. This article introduces the plug-in basic can cover most of the MAVEN user's daily use needs, if you really have very special needs, write a maven plugin yourself is not difficult, let alone there are so many open source plug-in for your reference.
The list of plugins in this article is not a complete list, and if the reader is interested, you can also go through the list of MAVEN plugins for Apache and Codehaus Mojo to get a more comprehensive understanding. Finally, the online MAVEN warehouse search engine, such as http://search.maven.org/, can also help you quickly find the Maven plugin you're interested in.
Common Maven Plugin Introduction