Maven practice (8)-Introduction to common Maven plug-ins (II)

Source: Internet
Author: User

We all know that Maven is essentially a plug-in framework, and its core does not execute any specific build tasks. All these tasks are handed over to the plug-in for completion, for example, the source code is compiled by Maven-compiler-plugin. Further, each task corresponds to a plug-in target (GOAL), and each plug-in has one or more targets. For example, the compile target of Maven-compiler-plugin is used to compilesrc/main/java/The main source code in the directory. The testcompile object is used to compilesrc/test/java/Test source code under the directory.

You can call Maven plug-in targets in two ways. The first method is to bind the plug-in target to the lifecycle phase, so that the user only enters the lifecycle stage in the command line, for example, Maven binds the compile target of Maven-compiler-plugin to the compile lifecycle stage by default.MVN compileIn fact, we first locate the compiler lifecycle phase and then call the compile target of Maven-compiler-Plugin Based on the binding relationship. The second method is to directly specify the plug-in target to be executed on the command line, for exampleMVN archetype: generateIt indicates the generate target for calling Maven-Archetype-plugin. This method of calling with colons has nothing to do with the life cycle.

Understanding the basic concepts of Maven plug-ins can help you understand the working mechanism of Maven. However, to use Maven more efficiently, it is necessary to understand some common plug-ins, this helps you avoid a new wheel. Over the years, the maven community has accumulated a lot of experience and has formed a mature plug-in ecosystem. Maven has two official plug-ins. The groupid In the first list is org. Apache. Maven. plugins. The plug-ins are the most mature. The specific address is http://maven.apache.org/plugins/index.html. The groupid In the second list is org. codehaus. Mojo. The plug-in here is not so core, but it is also very useful. Its address is http://mojo.codehaus.org/plugins.html.

Next, I will introduce some of the most commonly used Maven plug-ins based on my own experience. In different environments, they each have their own outstanding performance. Using these plug-ins skillfully can help your daily build work get twice the result with half the effort. This article is the lower half. (For the first part, see Maven practice (7)-Introduction to common Maven plug-ins (I ))

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 main resource file directory issrc/main/resources, Many users need to add additional resource file directories, which can be implemented by configuring Maven-resources-plugin. In addition, resource file filtering is also a major feature of Maven. You can use it in resource files.$ {Propertyname}Configure Maven-resources-plugin to enable filtering of resource files. Then, you can pass in the attribute values 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/

It may be due to historical reasons. In Maven 2/3, the plug-in used for testing is not Maven-test-plugin, but Maven-surefire-plugin. In fact, most of the time, as long as your test class complies with the General Command conventions (ended with test, ended with testcase, or started with test), you hardly need to know the existence of the plug-in. However, when you want to skip the test, exclude some test classes, or use some testng features, it is useful to understand some configuration options of Maven-surefire-plugin. For exampleMVN test-Dtest = footestThe effect of such a command is only to run the footest test class, which is achieved by controlling the test parameter of Maven-surefire-plugin.

Build-helper-Maven-plugin

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

By default, Maven only allows you to specify one main java code directory and one test Java code directory. Although this is an agreement that should be followed as much as possible, but occasionally you still want to specify multiple source code directories (for example, to cope with legacy projects), the add-source goal of build-helper-Maven-plugin is to serve this purpose, it is usually bound to the generate-sources stage of the default life cycle to add additional source code directories. It should be emphasized that this practice is not recommended because it breaks the maven conventions and may encounter other plug-in tools that strictly abide by the conventions that cannot correctly identify additional source code directories.

Another useful goal of build-helper-Maven-plugin is attach-artifact. You can select some project files in the form of classifier to generate ancillary components, and install it to the local repository, or deploy it to the remote repository.

Exec-Maven-plugin

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

Exec-Maven-plugin is easy to understand. As its name suggests, it allows you to run any local system program. In certain situations, running a Maven external program may be the simplest problem solution, which isExec: ExecOf course, this plug-in also allows you to configure relevant program running parameters. In addition to the exec target, exec-Maven-plugin also provides a Java target, which requires you to provide a mainclass parameter, and then it can use the dependencies of the current project as classpath, run the mainclass in the same JVM. Sometimes, to simply demonstrate a command line Java program, you can configure relevant running parameters of exec-Maven-plugin in POM, and then directly runMVN Exec: JavaTo view the running effect.

Jetty-Maven-plugin

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

During web development, it is almost inevitable to open a browser to manually test the application. This test method is usually to package the project into a war file and deploy it into a Web container, then start the container for verification, which is obviously time consuming. To help developers save time, Jetty-Maven-plugin came into being. It is fully compatible with the directory structure of Maven projects and can periodically check source files, once a change is detected, it is automatically updated to the built-in jetty Web container. After some basic configuration (for example, the contextpath of the Web application and the interval between automatic scan changes), you only need to executeMVN jetty: RunAnd then modify the code in the IDE. The code is automatically compiled by the IDE and changed to the jetty container after being detected by Jetty-Maven-plugin, then you can directly test the web page. It should be noted that jetty-Maven-plugin is not an official plug-in hosting Apache or codehaus. Therefore, additional configuration is required during use.settings.xmlTo add the plugingroup org. mortbay. jetty.

Versions-Maven-plugin

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

Many Maven users have encountered such a problem. When a project contains a large number of modules, it is annoying to update versions for them collectively, is there any automated tool that can help accomplish this? (Of course, you can use text operation tools such as SED, but they are not covered in this article.) The answer is yes, versions-Maven-plugin provides many goals to help you manage various versions of Maven projects. For example, the most commonly used commandMVN versions: set-dnewversion = 1.1-SnapshotYou can update the versions of all modules to 1.1-snapshot. This plug-in also provides other useful targets. display-dependency-Updates can tell you which updates are available for project dependencies; similar display-plugin-Updates can tell you the available plug-in updates, 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 can useMVN versions: commitSubmit. You can also use it if you are not satisfied.MVN versions: Revert.

Summary

This article describes some of the most commonly used Maven plug-ins. The "commonly used" here refers to the plug-ins that often need to be configured. In fact, many other plug-ins are required when we use Maven, for example, the default compilation plug-in Maven-compiler-plugin and the default packaging plug-in Maven-jar-plugin are not discussed in this article because they are rarely configured. Understanding the commonly used Maven plug-ins can help you complete the project construction task half the time. On the contrary, you may feel frustrated because you often encounter some difficult problems. The plug-ins described in this article can basically cover the daily usage needs of most Maven users. If you have special requirements, it is not difficult to compile a Maven plug-in by yourself, what's more, there are so many open-source plug-ins for your reference.

This plug-in list in this article is not a complete list. If you are interested, you can also carefully browse the maven plug-in List of Apache and codehaus mojo to gain a more comprehensive understanding. Finally, the online Maven repository search engine such as http://search.maven.org/can also help you quickly find maven.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.