Maven (D): Write a maven plugin

  • In the maven, all things are plug his own main function is dependent on plug-in implementation, but it also provides us with a wealth of plug-in library, the most important is https://maven.apache.org/plugins/ and http://www.mojohaus.org/plugins.html .
  • The use of plug-ins
    • In the pom files in the project, the build tag with a tag plugins, in which you can define multiple plug-ins; the figure below, you can see this maven project in function of all plug-ins, and plug-ins

  • A plug-in handwriting
    • First create a maven project
    • Pom in the value of the packaging label changed maven-plugin
      <packaging>maven-plugin</packaging>
    • The introduction of dependence
      <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>3.5.0</version>
      </dependency>
      <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>3.5</version>
      <scope>provided</scope>
      </dependency>
    • Create a class, inheritance
      AbstractMojo.class
    • Rewrite inside the execute method
      public void execute() throws MojoExecutionException, MojoFailureException {
      System.out.println("具体业务");

      }
    • On a class annotated
      @Mojo
    • And specify the name and defaultPhase, the value of name is the current goal, defaultPhase this goal is the phase in which the
      @Mojo(name="lhy",defaultPhase = LifecyclePhase.PACKAGE)
    • Then you plug it finished, it is not very simple.
  • Use plug-ins
    • The introduction of your plug-in project
      <build>
      <plugins>
      <plugin>
      <groupId>com.lhy</groupId>
      <artifactId>lhy-plugin</artifactId>
      <version>1.0-SNAPSHOT</version>
      </plugin>
      </plugins>
      </build>
    • Then you can see

    • Double-click he can run directly. Or mvn lhy: lhy can also run.
    • But sometimes we need to pass parameters, then we can define a property in the plug-in, and then add @Parameter comment, and then you can pass parameters when calling the
      @Parameter
      private String msg;
    • Run mvn lhy: lhy -Dmsg = ... to ok
    • So far, one of our basic plug-in is complete, from development to have a use, but all are run by command or double-click, how to let him run it automatically
    • In fact, can be configured in the pom
      <build>
      <plugins>
      <plugin>
      <groupId>com.lhy</groupId>
      <artifactId>lhy-plugin</artifactId>
      <version>1.0-SNAPSHOT</version>
      <configuration>
      <msg></msg>
      </configuration>
      <executions>
      <execution>
      <phase>install</phase>
      <goals>
      <goal>lhy</goal>
      </goals>
      </execution>
      </executions>
      </plugin>
      </plugins>
      </build>
    • configuration configuration parameters
    • configured to perform the execution phase and goal
    • Then when we execute phase plug where the goal will be executed automatically
  • Common plug-ins
    • findbugs static code checking
    • unified version upgrade versions
    • Packaging source code source
    • assembly packed zip, war
    • tomcat7

Guess you like

Origin www.cnblogs.com/lanhaiyue/p/10990313.html