[Cao Gong's Random Talk] Construction of Maven source debugging project

Maven Source Debugging Project Setup


Our previous article, " What does Maven have to do with Tomcat? Do they all wear patched clothes? The analysis of Maven's general implementation phase mainly includes three stages:

  1. Start the class phase, responsible for loading the framework;

  2. The maven core phase is responsible for finding the jar package of the corresponding plug-in, generating the plug-in object, parsing the parameters and calling the plug-in code according to the goal in the parameter, such as clean.

  3. In the plug-in execution phase, when execution is complete, control is returned to the framework core, and maven core completes the aftermath.

These three stages load the corresponding jar package in different places.

With an overview of the process, we began to think about what part of the code we were going to debug.

  • If we are debugging Phase 1 code, then there are two cases, whether Phase 1 code needs to be changed or not. If so, we need to find the source code of Phase 1 jar, load it into the idea, and assign the classpath to our source project when debugging starts.If no changes are needed, you can refer to the following: [Cao Gong Random Talk] Details the debugging method of Maven plug-in Directly introduce the jar package into idea's project classpath.
  • If we need to debug Phase 2 code, there are also two cases where we need to change the code, go to the source code, and introduce the idea.At the same time, when Phase 1 goes to Phase 2, it defaults to%MAVEN_Look under HOME%/lib/*.jar, and we'll have to correct its misbehavior and let it go to our source project to look for Phase 2 code.
  • If debugging phase 3 code is required, and if it needs to be changed, the default is to introduce the source project for the plug-in.However, Phase 2, when looking for plug-in code, it is more difficult to let it go to our source code project (by default, to go to the local warehouse, it is more difficult to correct this behavior), so even that.We'll still bring in the source code for the plug-in, but each time the plug-in code is changed, deploy it manually to the local repository.

Next, we begin to achieve the three goals above.

Debugging Project in Stage 3 of Construction

Most people might be interested in plug-in code. If you want to play with it yourself, you can refer to the following: [Cao Gong Random Talk] Details the debugging method of Maven plug-in>

But here, I still need to explain it based on a new warehouse I built on gitee.Subsequent source analysis articles, if any, will also be based on this project.

The code is here: https://gitee.com/ckl111/maven-3.8.1-source-learn

The following directory structure is described:

  • test-maven-core, which executes commands such as mvn clean on this module's pom
  • plugin-sources, which stores the code for the plugin, where subsequent plugin sources will be placed
  • plexus-classworlds-source, which stores the source code for the startup class of stage one
  • In addition, they are the source code for the maven framework itself, which is collectively referred to as the maven core

After downloading the source code, make idea import. When importing, select the POM under this home directory. This POM is the core of the framework. By default, only the Maven project of the core of the framework will be imported.We also need to manually import test-maven-core, plugin-sources, plexus-classworlds-source (right-click on pom, Mark As Maven Project s).

When the import is complete, we default to four root maven projects, of which the top Apache Maven is the Maven core framework.

Next, it's easy, and if you don't need to modify the code, you can break points anywhere.After the breakpoint, how do you trigger it? That's all.

I've got ok ay on my side. How about you?

If you want to modify the plug-in code, remember to add the mvn install plug-in to the local repository when you are done.

Debugging Project in Stage 2 of Construction

If you just want to follow the code debug, you can just break points on the classes corresponding to the maven framework, which is maven core.

If you want to change your maven source code and assisted learning, you have to think about it.

Before that, we need to compile the entire core of the maven framework once, so under G:\gitee-projectsmaven-3.8.1-source-learn (just the directory on my side), execute:

mvn -DskipTests=true install

If you make a mistake, what rat s are relevant, you can come here:

Put this plug-in element ( Element) deleted.

I also encountered a checkstyle error here, and also deleted the checkstyle check in this pom.

Ultimately, OKs are executed.

After this ok step above, in yesterday's article, " What does Maven have to do with Tomcat? Do they all wear patched clothes? "We said that maven's startup class goes down here to find the maven core code:


The contents of the file are as follows:

main is org.apache.maven.cli.MavenCli from plexus.core

set maven.conf default ${maven.home}/conf

load       ${maven.conf}/logging
optionally ${maven.home}/lib/ext/*.jar
load       ${maven.home}/lib/*.jar

Among them, load ${maven.home}/lib/*.jar loads maven's core framework jar.This is where we have to change it so that it has to be loaded under the source project in our idea.

main is org.apache.maven.cli.MavenCli from plexus.core

set maven.conf default ${maven.home}/conf

load       ${maven.conf}/logging
optionally ${maven.home}/lib/ext/*.jar
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-artifact\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-builder-support\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-compat\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-core\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-embedder\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-model\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-model-builder\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-plugin-api\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-repository-metadata\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-resolver-provider\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-settings\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-settings-builder\target\classes
load       G:\gitee-projects\maven-3.8.1-source-learn\maven-slf4j-provider\target\classes
load       ${maven.home}/lib/*.jar

That is to say we have classes loaded under the target.

That way, we can change the code of the maven core framework imported in our idea, and compile it, and after compile, class goes into the corresponding target directory, which is exactly in the path in m2.conf above.

To verify the validity of this, we casually made a breakpoint in the maven core framework and looked at the path of the class loader for the current class and found that it was okay.

Debugging Project in Stage 1 of Build

Stage one, which is the startup jar package, corresponds to the source code in:

Instead of plexus-classworlds-2.6.0.jar under the maven installation directory, we just need to point the classpath to the target directory here in the startup test project.

We'll copy this and replace it with one that points to the jar package:

-classpath "G:\gitee-projects\maven-3.8.1-source-learn\target;F:\tools\apache-maven-3.8.1-bin\apache-maven-3.8.1\boot\plexus-classworlds.license;C:\Program Files\JetBrains\IntelliJ IDEA 2021.1.1\lib\idea_rt.jar"

Paste this into the parameters that maven executes.


To summarize:

  1. Stage one, specify classpath to debug our source project:

    -classpath "G:\gitee-projects\maven-3.8.1-source-learn\target;F:\tools\apache-maven-3.8.1-bin\apache-maven-3.8.1\boot\plexus-classworlds.license;C:\Program Files\JetBrains\IntelliJ IDEA 2021.1.1\lib\idea_rt.jar"
  2. Stage two, the core stage of the framework, is achieved by modifying the content of m2.conf in the maven installation directory

  3. Phase 3, Plugin Source Phase, after modifying the source code, by compiling and deploying to the local repository

I am a daily worker in Tencent, Shenzhen, and a java program ape in the back end that jumps across and over again in Chengdu and Shenzhen. Before 3 years in Shenzhen, I went to Chengdu for 4 years. Now I come to Shenzhen again, and I have started writing the front end. If there is a need to push in, I can find it.Interest in first-line coding, network, database, high concurrency, etc.

Welcome to join me and join the technical group to communicate with each other.Tencent Next can also find me.

This article is published by blog OpenWrite Release!

Posted on Tue, 07 Sep 2021 13:51:02 -0400 by Simplicity