Maven plugin debugging method

[toc]

preface

The update frequency of this year has dropped to the freezing point. On the one hand, I work more overtime and just want to play with my mobile phone after work. On the other hand, it seems that after entering dachang, learning motivation is also very low, in short, very lazy, blog, this year only updated less than 5.

Now slowly have a little state, began to learn some technology; There is a Mybatis Generator in the company, which makes some customized development for the company. I don’t know the specific content, but there is a very painful problem. When the Po is generated according to the database table, it cannot annotate the field of the database table. Comment the field generated into the Po.

I’ve tried to solve this problem by myself, but I don’t know anything about maven plugins, so I’ve had to put up with it. Months before download maven source, but the source code in a pile of unit tests, but always don’t know how to like the way we usually use to debug, then three days fishing net to blind to see two days, the effect is very poor, see how much you forget how many, recently put the debug plugin way for ok, just share with everyone here.

Below the text.

The more complex the open source project, the more you will use the maven plugin, a pom, hundreds of line is common, often, everyone is only know how to configure, in other words, don’t know how to configure, need to change configuration, is a fierce search, often out of the search on the net, may also because version mismatch “to in his article, Why not on my end.”

Anyway, to sum up, the Maven plugin is often a black box to everyone.

We just don’t tolerate black boxes, of course, like JVMS written in c++.

In addition, to give you another reason to read this article, just ask you a question: How does Spring Boot package into a FAT JAR and boot from a FAT JAR?

Right? The answer is in the Maven plugin for Spring Boot. Today we will not be so complicated, make a clean plugin to learn, ok.

How to debug the source code of a plug-in in a single line

Creating a demo project

We need a Maven project, finally finished, my side looks like this:

Here are the steps: Generate one directly like this, and then finish next:

For the convenience of demonstration, we have deleted the content of elements in pom. XML (mainly for plug-in version management, we directly delete for demonstration, use the default version), and the deleted POM is as follows:


      

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>test-project</artifactId>
  <version>1.0 the SNAPSHOT</version>

  <name>test-project</name>
  <! -- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
  </build>
</project>
Copy the code

By default, we bind plugins to our Maven lifecycle, such as the Maven-clean-plugin plugin, during the clean phase. In order to make it simple and easy to understand, we pay attention to the Clean plug-in. My version is 2.5. There is no need to worry about the version number, because different idea versions may have different results.

Debugging the Clean plug-in

To debug, first trigger maven’s clean plugin, right? How do you trigger execution?

How do I trigger the execution of the Clean plug-in

For this command is very simple, do not need to pass parameters to the plug-in command, directly this can trigger the debugging run.

Let’s take a look at the implementation:

The java-main args class is the same as the java-main args class. The java-main args class is the same as the java-main args class.

The argument here is “clean:clean”.

Where to break it

Someone began to ask, you said debugging, this is a run ah, besides, I want to interrupt the point where to play?

Ok, to break the point, we need to know which method MVN clean is definitely going to execute, and then we can break the point ambush there in advance, just like we always know that program execution is going to enter the main method.

MVN Clean, on the other hand, must implement the CleanMojo class in the Clean plugin:

To debug this class, the class must be found in the current project (idea, a project contains multiple modules). In idea, I come up with double shift,

Look, can’t find this class ah, how to play? The easiest way to do this is to add the jar package directly to the project,

Add to libraries:

When adding, you will be prompted to join the current project. Select yes. When added, you can see:

I raise my hand again with the Doubel Shift, and you can see that cleanMojo already exists.

Now that it exists, here’s the point of interruption:

Continue to trigger debug execution

As you can see, we have successfully stopped at our breakpoint, so we can step through it. But ha, you have also noticed that we because of the decompilation of class, and decompilation of class, certainly not so comfortable source code, this problem, also simple, let me step by step to talk about.

Where to break — mode 2

We below this way, of course, is to find a way to break the source code, source code to obtain where?

Mvnrepository.com/artifact/or…

After downloading the source code, I tried the following approach (which is to attach the source code based on the previous solution) :

This way, make a break point and see:

This method can debug the plug-in itself several Java files, but the plug-in depends on those, or problems, this way, it is weak.

Where to break — the most recommended way

We’d better not do those have no, to the official plug-in development address pull code:

maven.apache.org/scm.html

After downloading, unzip it and find it is a Maven project, comfortable. Import idea directly. After the import, we set the breakpoint and run a debug.

As you can see, this is comfortable, indeed, debugging the source code. But, here is a reminder, do not think to change the code, if directly changed, will certainly change the class and Java source line number, do not correspond, as why, this is a story worth writing, I will leave it later.

extension

This is the way maven Clean execution is triggered.

In fact, this can only be used for simple scenarios, no need to pass parameters, complex scenarios still need to look like this:

For example, our mybatis generator:

It specifies a lot of parameters, like the location of the corresponding configuration file, right.

This article is published by OpenWrite!