Recently received a business student appeal: users will table in their big data platform a lot of new model, but to write business code, need to manually to create and model table corresponding to the DO, the situation with small scale was acceptable, but in the case of more than a dozen or even dozens of tables, will be a headache.

We know that Mybatis actually provides a similar plug-in tool (Mybatis -Generator), and there are many similar code generation tools in the community, but due to some limitations, there is no way to use them, so the simplest thing is to write a plug-in to complete such a workload and high repeatability.

This article does not explain the concepts and plug-in life cycle of Maven. There is a lot of information on how to write a Maven plug-in.

The Maven plugin is not much different from a regular Maven project, as it also has its own GAV. The difference is that each goal maven plugin will contain some of the columns, each goal corresponds to a Mojo class, the Mojo class needs to implement org.. Apache maven. Plugin. Mojo the interface, The Mojo interface provides a execute method that requires subclasses to implement. In the actual development of plug-in work, our task is to implement the logic in the execute method, execute method to do the plug-in needs to deal with things. Here’s a brief introduction to the basics of Maven: lifecycle, phases, goal, and finally writing a Maven plugin.

Maven Life Cycle

Maven builds follow a specific life cycle to deploy and distribute target projects, and there are three built-in life cycles in Maven:

  • Default: is responsible for the major life cycle of project deployment
  • Clean: Clean all files from the last build (target/)
  • Site: Creates a site document for the project

Each life cycle consists of a series of phases; The default build life cycle consists of 23 phases, which are the main build life cycle. On the other hand, the Clean life cycle consists of three phases, while the Site life cycle consists of four phases. Let’s look at these stages in detail.

Maven phase

Maven phases represent phases in the Maven build life cycle, each of which is responsible for a specific task, including the following by default:

  • Validate: Checks that all the information required for the build is available
  • Compile: Compiles the source code
  • Test-compile: compiles the test source code
  • Test: Runs a unit test
  • Package: jar, war…
  • Integration-test: indicates an integration test
  • Install: Installs the package into the local repository
  • Deploy: Deploy the package to a remote repository

Phases in Maven are sequenced. When running later phases, previous ones are also executed, but you can skip some operations with parameters, the most common being skipTests.

For example, when we execute

mvn install
Copy the code

As you can see, the earlier stages such as compile and test are also executed.

maven goal

There are phases in the lifecycle, in which there are a series of goals, each responsible for a specific task; When we run a phase, by default all the targets bound to the phase are executed in sequence. Take compile as an example:

Here are some of the phases and default goals associated with them:

  • Compile :compile -> The compile target of the compiler plug-in is bound to the compile phase
  • Compile :testCompile -> is bound to the testCompile phase

How to look at the goal of a particular phase and its plugin information? MVN help:describe -Dcmd=PHASENAME

mvn help:describe -Dcmd=compile
Copy the code
[INFO] 'compile' is a phase corresponding to this plugin: Org, apache maven. Plugins: maven - compiler - plugin: 3.1: the compile It is a part of the lifecycle for POM packaging 'jar'. This lifecycle includes the following phases: * validate: Not defined * initialize: Not defined * generate-sources: Not defined * process-sources: Not defined * generate-resources: Not defined * process-resources: Org, apache maven. Plugins: maven - resources - the plugin: 2.6: resources * compile: Org, apache maven. Plugins: maven - compiler - plugin: 3.1: the compile * process - classes: Not defined * generate - test - sources: Not defined * process-test-sources: Not defined * generate-test-resources: Not defined * process-test-resources: Org, apache maven. Plugins: maven - resources - the plugin: 2.6: testResources * test - the compile: Org, apache maven. Plugins: maven - compiler - plugin: 3.1: testCompile * process - test - classes: Not defined * test: Org, apache maven. Plugins: maven - surefire - plugin: 2.12.4: prepare the test * - package: Not defined * package: Org, apache maven. Plugins: maven - jar - plugin: 2.4: jar * pre - integration - test: Not defined * integration - the test: Not defined * post-integration-test: Not defined * verify: Not defined * install: Org, apache maven. Plugins: maven - install - plugin: 2.4: install * deploy: Org, apache maven. Plugins: maven deploy - plugin: 2.7: deployCopy the code

Write a plug-in

Writing a plug-in can be broken down into the following steps:

Step1: create a new maven-archetype-mojo

Step2: Add a Mojo class

@Mojo(name = "build-glmapper", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME, requiresDependencyCollection = ResolutionScope.RUNTIME)
public class RepackageMojo extends AbstractMojo {
    public void execute(a) throws MojoExecutionException, MojoFailureException {
        getLog().info("this is a test case for mojo"); }}Copy the code

Step3: Pom.xml processing

Packaging is not a POM or a JAR

<packaging>maven-plugin</packaging>
Copy the code

Introduce some necessary dependencies:

<dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-archiver</artifactId>
  </dependency>
  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-artifact</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-core</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.maven.plugin-tools</groupId>
    <artifactId>maven-plugin-annotations</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.maven.shared</groupId>
    <artifactId>maven-common-artifact-filters</artifactId>
</dependency>
Copy the code

Introduce a plugin that packages plug-ins

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>3.5</version>
            <configuration>
                <mojoDependencies>
                	<! -- Plugin name -->
                    <dependency>com.bridge.glmapper.boot:glmapper-plugin</dependency>
                </mojoDependencies>
            </configuration>

        </plugin>
    </plugins>
</build>
Copy the code

Step4: install your plugin & test plugin allows

The imported project can also be seen from the Maven Plugin menu:

For example, our plugin name is glmapper-plugin, but it is actually executed using glmapper. This is when the plugin name satisfies: You can use XXX when naming rules for xxx-maven-plugin or maven-xxx-plugin.

conclusion

This article briefly introduces some of the basics of Maven as a reminder, and also introduces how to write a Maven plugin through a small case. If you think it is good, welcome to like it!!