Fix Maven Plugin development in the default folder of Maven Projects

Source: Internet
Author: User

Fix Maven Plugin development in the default folder of Maven Projects

In general, the development of Maven Plugin is relatively simple. After learning about Mojo, it is easier to implement one goal of Maven Plugin.

The directory under src is sometimes incomplete after a Maven project is created through eclipse or archetype, so we started to develop a Maven Plugin idea. This plug-in is named hello-maven-plugin. In addition, Apache Maven officially declares that the name of Maven Plugin requires attention. The official plug-in format of Maven is: maven-<plugin name>-plugin. other individuals or organizations can use <plugin name>-maven-plugin for naming. However, the advantage of using this naming format is that when using Plugin, you can use the mvn plugin: goal command to perform operations, saving you the need for a complete qualified name.

The steps for developing this plug-in are as follows:

Create a maven project for hello-Maven-plugin

Add dependency and maven-plugin plug-in pom. xml

<Dependencies>
<Dependency>
<GroupId> org. apache. maven </groupId>
<ArtifactId> maven-plugin-api </artifactId>
<Version> 2.0 </version>
</Dependency>
<Dependency>
<GroupId> org. apache. maven. plugin-tools </groupId>
<ArtifactId> maven-plugin-annotations </artifactId>
<Version> 3.4 </version>
<Scope> provided </scope>
</Dependency>
<Dependency>
<GroupId> org. apache. maven </groupId>
<ArtifactId> maven-core </artifactId>
<Version> 3.1.0 </version>
</Dependency>
</Dependencies>

<Plugin>
<GroupId> org. apache. maven. plugins </groupId>
<ArtifactId> maven-plugin </artifactId>
<Version> 3.4 </version>
<Executions>
<Execution>
<Id> default-descriptor </id>
<Phase> process-classes </phase>
</Execution>
</Executions>
</Plugin>

Note that the Mojo object can process the plugin. xml description file by using Java doc Tag or Java Annotation. Java Annotation is used here.

Details: http://maven.apache.org/plugin-tools/maven-plugin-annotations/index.html

3. Write Mojo
Generally, a Plugin can have multiple goal classes. Here we extract the common parts of multiple Mojo to the parent class, which is an abstract class and does not implement the execute method, instead, it is implemented by Mojo that implements specific goal.

Package secondriver. maven. plugin. hello;

Import org. apache. maven. plugin. AbstractMojo;

Public abstract class BaseMojo extends actmojo {

Public void printLine (){
GetLog (). info (
"------------------------------------------------------------------------");
}
}

 

 
Package secondriver. maven. plugin. hello;

Import java. io. File;

Import org. apache. maven. plugin. MojoExecutionException;
Import org. apache. maven. plugin. MojoFailureException;
Import org. apache. maven. plugins. annotations. LifecyclePhase;
Import org. apache. maven. plugins. annotations. Mojo;
Import org. apache. maven. plugins. annotations. Parameter;
Import org. apache. maven. project. MavenProject;

@ Mojo (name = "fixdir", requiresProject = true, threadSafe = true, defaultPhase = LifecyclePhase. NONE)
Public class FixDir extends BaseMojo {

@ Parameter (defaultValue = "$ {project }")
Private MavenProject project;

@ Parameter (defaultValue = "$ {project. basedir}", readonly = true)
Private File baseDir;

Private String srcDir = "/src ";

Private String mainJava = "/main/java ";

Private String mainResources = "/main/resources ";

Private String testJava = "/test/java ";
Private String testResources = "/test/resources ";

@ Parameter (name = "packageName", property = "fixdir. packageName", defaultValue = "$ {project. groupId}", required = true)
Private String packageName;

Public void execute () throws MojoExecutionException, MojoFailureException {
String packagePath = packageName. replace (".", File. separator );

// Create the main directory
Mkdirs (mainJava + File. separator + packagePath );
Mkdirs (mainResources );

// Create a directory for test
Mkdirs (testJava + File. separator + packagePath );
Mkdirs (testResources );
}

Private void mkdirs (String path) throws MojoFailureException {
File file = new File (baseDir, srcDir + File. separator + path );
If (! File. exists ()){
If (file. mkdirs ()){
GetLog (). info (path + "created OK .");
} Else {
GetLog (). error (path + "created Failed .");
Throw new MojoFailureException ("Fix" + mainJava + ","
+ MainResources + "," + testJava + ","
+ TestResources + "Failed .");
}
}
}
}

The above FixDirMojo implements fixdir goal. Its specific function is to complete the src directory (specifically, it is not complete here according to the actual use of the Maven project, for example, the assembly and profile directories are not created ).
For more information about how to use Annotation, see: http://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/index.html#Supported_Annotations.
Another best way is to use the Java Decompiler tool to decompile the existing Maven plug-in and see how to use these annotations.

4. Compile, generate the Plugin description file, and publish

Hello-maven-plugin> mvn compiler: compile
Hello-maven-plugin> mvn plugin: descriptor
Hello-maven-plugin> mvn install

You can also directly execute: mvn install

You can use plugin: descriptor to process Mojo and generate plugin. xml. The descriptions are as follows:
 
<Plugin>
<Name> hello-maven-plugin Maven Mojo </name>
<Description> </description>
<GroupId> secondriver. maven. plugin </groupId>
<ArtifactId> hello-maven-plugin </artifactId>
<Version> 1.0 </version>
<GoalPrefix> hello </goalPrefix>
<IsolatedRealm> false </isolatedRealm>
<InheritedByDefault> true </inheritedByDefault>
<Mojos>
<Mojo>
<Goal> fixdir </goal>
<RequiresDirectInvocation> false </requiresDirectInvocation>
<RequiresProject> true </requiresProject>
<RequiresReports> false </requiresReports>
<Aggregator> false </aggregator>
<RequiresOnline> false </requiresOnline>
<InheritedByDefault> true </inheritedByDefault>
<Implementation> secondriver. maven. plugin. hello. FixDir </implementation>
<Language> java </language>
<InstantiationStrategy> per-lookup </instantiationStrategy>
<ExecutionStrategy> once-per-session </executionStrategy>
<ThreadSafe> true </threadSafe>
<Parameters>
<Parameter>
<Name> baseDir </name>
<Type> java. io. File </type>
<Required> false </required>
<Editable> false </editable>
<Description> </description>
</Parameter>
<Parameter>
<Name> packageName </name>
<Type> java. lang. String </type>
<Required> true </required>
<Editable> true </editable>
<Description> </description>
</Parameter>
<Parameter>
<Name> project </name>
<Type> org. apache. maven. project. MavenProject </type>
<Required> false </required>
<Editable> true </editable>
<Description> </description>
</Parameter>
</Parameters>
<Configuration>
<BaseDir implementation = "java. io. File" default-value = "$ {project. basedir}"/>
<PackageName implementation = "java. lang. String" default-value = "$ {project. groupId}" >$ {fixdir. packageName} </packageName>
<Project implementation = "org. apache. maven. project. MavenProject" default-value = "$ {project}"/>
</Configuration>
</Mojo>
</Mojos>
</Plugin>

 
5. Add hello-Maven-plugin to the maven project and use the functions.

<Plugin>
<GroupId> secondriver. maven. plugin </groupId>
<ArtifactId> hello-maven-plugin </artifactId>
<Version> 1.0 </version>
</Plugin>

 
Run the mvn hello: fixdir or mvn secondriver. maven. plugin: hello: 1.0: fixdir command to fix the directory under src.

Before execution:

The execution result shows:

After execution:


Right, we can see that after fixdir is executed, the structure of the Directory test is complete.

6. Write it at the end
This article only solves a problem encountered by creating a simple Maven Plugin. It has the functionality, but it is far from the integrity of a qualified Maven Plugin. For example, Maven Plugin parameter description, help documentation, and parameter check.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.