5. 定制Maven插件
Maven 有十几个预构建插件供您使用,但是有时候您只想找到自己需要的插件,构建一个定制的 Maven 插件比较容易:
用 POM packaging 创建一个新项目,设置为 “maven-plugin”。
包括一个 maven-plugin-plugin 调用,可以定义您的公布插件目标。
创建一个 Maven 插件 “mojo” 类 (一个扩展 AbstractMojo 的类)。
为类的 Javadoc 做注释来定义目标,并为每个将被作为配置参数公布的变量做注解。
实现一个 execute() 方法,该方法在调用您的插件是将被调用。
例如,清单 8 显示了一个定制插件(为了部署 Tomcat)的相关部分:
清单 8. TomcatDeployerMojo.java
package net.fpic.maven.plugins;
import java.io.File;
import java.util.StringTokenizer;
import net.fpic.tomcatservice64.TomcatDeploymentServerClient;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import com.javasrc.server.embedded.CommandRequest;
import com.javasrc.server.embedded.CommandResponse;
import com.javasrc.server.embedded.credentials.Credentials;
import com.javasrc.server.embedded.credentials.UsernamePasswordCredentials;
import com.javasrc.util.FileUtils;
/**
* Goal that deploys a web application to Tomcat
*
* @goal deploy
* @phase install
*/
public class TomcatDeployerMojo extends AbstractMojo
{
/**
* The host name or IP address of the deployment server
*
* @parameter alias="host" expression="${deploy.host}" @required
*/
private String serverHost;
/**
* The port of the deployment server
*
* @parameter alias="port" expression="${deploy.port}" default-value="58020"
*/
private String serverPort;
/**
* The username to connect to the deployment manager (if omitted then the plugin
* attempts to deploy the application to the server without credentials)
*
* @parameter alias="username" expression="${deploy.username}"
*/
private String username;
/**
* The password for the specified username
*
* @parameter alias="password" expression="${deploy.password}"
*/
private String password;
/**
* The name of the source artifact to deploy, such as target/pos.war
*
* @parameter alias="artifactSource" expression=${deploy.artifactSource}"
* @required
*/
private String artifactSource;
/**
* The destination name of the artifact to deploy, such as ROOT.war.
* If not present then the
* artifact source name is used (without pathing information)
*
* @parameter alias="artifactDestination"
* expression=${deploy.artifactDestination}"
*/
private String artifactDestination;
public void execute() throws MojoExecutionException
{
getLog().info( "Server Host: " + serverHost +
", Server Port: " + serverPort +
", Artifact Source: " + artifactSource +
", Artifact Destination: " + artifactDestination );
// Validate our fields
if( serverHost == null )
{
throw new MojoExecutionException(
"No deployment host specified, deployment is not possible" );
}
if( artifactSource == null )
{
throw new MojoExecutionException(
"No source artifact is specified, deployment is not possible" );
}
...
}
}在这个类的头部,@goal 注释指定 MOJO 执行的目标,而 @phase 指出目标执行的阶段。除了一个映射到一个有真实值的系统属性的表达式之外,每个公布的属性有一个 @phase 注释,通过将被执行的参数指定别名。如果属性有一个 @required 注释,那么它是必须的。如果它有一个 default-value,那么如果没有指定的话,将使用这个值。在 execute() 方法中,您可以调用 getLog() 来访问 Maven 记录器,根据记录级别,它将输出具体消息到标准输出设备。如果插件发生故障,抛出一个 MojoExecutionException 将导致构建失败。
结束语
您可以使用 Maven 只进行构建,但是最好的 Maven 是一个项目生命周期管理工具。本文介绍了 5 个大家很少了解的特性,可以帮助您更高效地使用 Maven。