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

How to quickly fix the maven plug-in

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to quickly fix the maven plug-in", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to quickly fix the maven plug-in.

What is a Maven plug-in?

Maven is really just the core framework for the collection of Maven plug-ins. In other words, the plug-in is where most of the actual operations are performed.

Plug-ins are used to:

Create a jar file

Create a war file

Compiled code

Unit test code

Create project documents, etc.

Plug-ins are the core functionality of Maven and allow common build logic to be reused in multiple projects. They do this by performing "actions" (that is, creating WAR files or compiling unit tests) in the context of the project description (Project object Model (POM)). The behavior of the plug-in can be customized through a unique set of parameters, which are exposed by the description of each plug-in goal (or Mojo).

A plug-in usually provides a set of goals that can be executed using the following syntax:

Mvn [plugin-name]: [goal-name]

For example, a Java project can use the Maven compiler plug-in to compile the target by running the following command

What are the types of mvn compiler:compile plug-ins

Maven provides the following two types of plug-ins:

Here is a list of some common plug-ins:

For example

We use the maven-antrun-plugin plug-in in the example to print the data on the console. Now create a pom.xml file in the C:\ MVN\ project folder with the following contents:

4.0.0com.companyname.projectgroupproject1.0 org.apache.maven.plugins maven-antrun-plugin 1.1 id.clean clean run clean phase

Next, open the command terminal and jump to the directory where pom.xml is located, and execute the following mvn command.

Mvn clean

Maven will begin to process and display the clean phase of the clean life cycle.

[INFO] Scanning for projects... [INFO]-[INFO] Building Unnamed-com.companyname.projectgroup:project:jar:1.0 [INFO] task-segment: [post-clean] [INFO]-- -[INFO] [clean:clean {execution: default-clean}] [INFO] [antrun:run {execution: id.clean}] [INFO] Executing tasks [echo] clean phase [INFO] Executed tasks [INFO]- -[INFO] BUILD SUCCESSFUL [INFO]- -[INFO] Total time: < 1 second [INFO] Finished at: Sat Jul 07 13:38:59 IST 2020 [INFO] Final Memory: 4M/44M [INFO]-

The above example shows the following key concepts:

Plug-ins are defined using the plugins element in pom.xml.

Each plug-in can have multiple goals.

You can define the phase, and the plug-in will use its phase element to start processing. We have used the clean phase.

You can configure the tasks to be performed by binding to the target of the plug-in. We have bound the echo task to the run target of maven-antrun-plugin.

That's it. Maven will take care of the rest. It will download plug-ins that are not available in the local repository and start processing.

Plug-ins and goals

A plug-in can usually accomplish multiple tasks, and each task is called a goal of the plug-in. For example, when executing the mvn install command, the plug-ins to be invoked and the plug-ins to be executed are as follows:

Bind plug-ins to the lifecycle

The Maven lifecycle is abstract and actually requires plug-ins to complete the task, which is accomplished by binding the plug-in's goal to a specific phase of the lifecycle (phase). For example, bind the compile goal of the maven-compiler-plugin plug-in to the compile phase of the default life cycle, and complete the source code compilation of the project:

Built-in binding

Maven binds plug-in goals to some phases of the lifecycle (phase) by default, because different projects have different packaging methods, such as jar, war, pom, etc., so they have different binding relationships. The binding relationships of jar packages for default lifecycle are as follows:

In the second column, the colon is preceded by the prefix of the plug-in, which is a simplified way to configure and use the plug-in; the colon is followed by the binding plug-in target.

What maven plug-ins do you have in your warehouse?

Storage directory =% local warehouse%\ org\ apache\ maven\ plugins

A more detailed list of official plug-ins is available on the Maven website:

Custom plug-in

We mentioned earlier that a Mojo,Mojo is actually a Maven target, and the plug-in contains any number of targets (Mojos). Mojos can be defined as an annotated java class or a Beanshell script. Mojo specifies the metadata about the goal: the target name, which phase of the lifecycle it fits, and the parameters it expects.

The Mojo term, introduced in maven2, is a complete rewrite of how to write plug-ins. Mojo is an improvement on Pojo (plain-old-java-object), which replaces maven with plain.

A Mojo contains a simple Java class. Several Mojo-like commonalities in plug-ins can be encapsulated using abstract parent classes. The packaging method packaging for the Maven plug-in project must be maven-plugin.

Implement custom plug-ins

Create a maven project and add dependencies:

TianMojo inherits the abstract class AbstractMojo and implements the execute () method, which is used to define the specific operation of the Mojo. We just need to write our own implementation according to our own needs.

/ / Custom plug-in class / / name is the @ Mojo (name = "tian") public class TianMojo extends AbstractMojo {/ / in excuation when the plug-in is used later. The configuration of this maven plug-in {/ / is configured using the configration tag in pom. Property is the name, / / the tag name in the configuration. When you call the plug-in, you can also set the default value @ Parameter (property = "userName", defaultValue = "Brother Tian") private String userName; @ Parameter (property = "pwd", defaultValue = "000000") private String pwd; @ Override public void execute () throws MojoExecutionException, MojoFailureException {System.out.println ("userm=" + userName + "pwd=" + pwd); System.out.println ("my plugin is running");}}

Then execute the mvn clean install command.

Use custom plug-ins

Add our custom plug-ins to our maven project:

Com.tian.maven my-maven-plugin 1.0-SNAPSHOT install tian Tian Weichang 123456

And then you can see it in our IDEA:

Double-click my:tian:

You can also use the command: mvn my:tian

My is a prefix and an abbreviation for my-maven. The suffix tian is the target of the plug-in binding.

At this point, our custom Maven plug-in is done.

At this point, I believe you have a deeper understanding of "how to quickly fix the maven plug-in". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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

Development

Wechat

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

12
Report