歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 修復Maven項目默認文件夾的Maven Plugin開發

修復Maven項目默認文件夾的Maven Plugin開發

日期:2017/2/28 14:03:03   编辑:Linux教程

總體說Maven Plugin開發算是比較簡單,了解了Mojo之後,實現起Maven Plugin的一個個goal就更加輕松了。

由於發現在通過eclipse或者archetype創建Maven工程之後,src下的目錄時而不完整,於是打起開發一個Maven Plugin的主意。該插件命名為:hello-maven-plugin,此外Apache Maven官方聲明過Maven Plugin命名需要注意的問題,Maven官方的Plugin命名格式為:maven-<plugin name>-plugin,其它個人或者組織可以使用<plugin name>-maven-plugin命名,當然不限於此。不過使用這種命名格式有個好處是在使用Plugin的時候可以通過 mvn plugin:goal 命令來進行操作,可以省去完整限定名。

下面將是開發該插件的具體步驟:

創建hello-maven-plugin的Maven工程

在pom.xml中添加依賴和maven-plugin-plugin插件

<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-plugin</artifactId>
<version>3.4</version>
<executions>
<execution>
<id>default-descriptor</id>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>

需要注意的事,Mojo對象的通過Java doc Tag或者Java Annotation的注釋的方式都可以通過處理生產plugin.xml的描述文件,這裡使用Java Annotation。

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

3.編寫Mojo
通常一個Plugin中可能有多個goal,這裡我們把多個Mojo的共同部分提取到父類,該父類是抽象類,並未實現execute方法,而是由每個實現特定goal的Mojo來實現。

package secondriver.maven.plugin.hello;

import org.apache.maven.plugin.AbstractMojo;

public abstract class BaseMojo extends AbstractMojo {

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);

// 創建關於main的目錄
mkdirs(mainJava + File.separator + packagePath);
mkdirs(mainResources);

// 創建關於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.");
}
}
}
}

上面的FixDirMojo實現了fixdir goal,其具體功能就是文中開始提到的將src的目錄補完整(特別說一個,按照Maven工程的實際使用,這裡並不完整,比如assembly,profile目錄等都未創建)。
限於篇幅有關Annotation的具體使用可以參見:http://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/index.html#Supported_Annotations
另外一個最好不過的方式就是通過Java Decompiler工具將現有個Maven Plugin反編譯之後看看這些注解的具體使用。

4.編譯,生成Plugin的描述文件,發布

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

也可以直接執行:mvn install

通過plugin:descriptor將對Mojo進行處理,生成plugin.xml。關於描述下面是其中一部分摘取:

<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.在Maven工程中添加hello-maven-plugin,並且使用其中功能

<plugin>
<groupId>secondriver.maven.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0</version>
</plugin>


執行 mvn hello:fixdir或者mvn secondriver.maven.plugin:hello:1.0:fixdir命令,修復src下的目錄。

執行前:

執行結果顯示:

執行後:


通過對吧前後兩幅目錄樹可以看出執行fixdir之後,test目錄下結構完整了。

6.寫在最後
本文只是通過創建一個簡單的Maven Plugin解決實際中遇到的一個問題,其具備了功能性,但離一個合格的Maven Plugin的完整性還差好遠。比如:Maven Plugin的參數說明,幫助文檔,參數檢查等。更多關於Maven Plugin開發還得去官網開開發文檔(有點小凌亂),還有就是反編譯已有的成熟Maven Plugin。

Maven權威指南_中文完整版清晰PDF http://www.linuxidc.com/Linux/2014-06/103690.htm

Maven 3.1.0 發布,項目構建工具 http://www.linuxidc.com/Linux/2013-07/87403.htm

Linux 安裝 Maven http://www.linuxidc.com/Linux/2013-05/84489.htm

Maven3.0 配置和簡單使用 http://www.linuxidc.com/Linux/2013-04/82939.htm

Ubuntu下搭建sun-jdk和Maven2 http://www.linuxidc.com/Linux/2012-12/76531.htm

Maven使用入門 http://www.linuxidc.com/Linux/2012-11/74354.htm

Copyright © Linux教程網 All Rights Reserved