---
title: "创建 maven 插件"
author: "louisling"
date: "2009-11-10T16:40:02Z"
tags: ["Apache", "Java", "XML", "maven"]
slug: "maven"
draft: false
lastmod: "2009-11-10T16:40:02Z"
id: 513754
url: "https://louisling.iteye.com/blog/513754"
---

创建 maven 插件： 删除所有资源文件夹 mac 目录下面所有 grf 文件（target/classes/mac/\*\*/\*.grf）里面的内容： \[code="xml"] \[/code] 1) cd/d C:\\Temp mvn archetype:create -DgroupId=org.xmlasia.plugins -DartifactId=edit-resource-maven-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo 2) EditResourceMojo.java \[code="java"]import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Arrays; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; /\** * Goal which edit resource, remove specified line. * * @goal editResource * @phase compile \*/ public class EditResourceMojo extends AbstractMojo { /\** * @parameter \*/ private File file; /\** * @parameter \*/ private String\[] commentStrs; public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String\[] getCommentStr() { return commentStrs; } public void setCommentStr(String\[] commentStrs) { this.commentStrs = commentStrs; } public void execute() throws MojoExecutionException { String absolutePath = file.getAbsolutePath(); getLog().info("Edit resource: " + absolutePath + ": " + Arrays.toString(commentStrs) + ": " + file.exists()); if (!file.exists()) { //Process folder if (absolutePath.contains("\*\*\\\\\*.")) { int index = absolutePath.lastIndexOf("."); String extension = absolutePath.substring(index + 1, absolutePath.length()); index = absolutePath.indexOf("\*\*\\\\\*."); String path = absolutePath.substring(0, index); getLog().info("path: " + path + " extension: " + extension); File mFile = new File(path); getLog().info("Path exist: " + mFile.exists()); for (String commentStr : commentStrs) { try { FileUtils.delContent(commentStr, path, extension); } catch (Exception e) { e.printStackTrace(); } } } } else //Process single file editResources(file); } private void editResources(File mFile) { try { BufferedInputStream bin = new BufferedInputStream(new FileInputStream(mFile)); byte\[] buff = new byte\[((int) mFile.length())]; bin.read(buff); String str = new String(buff, "utf-8"); String\[] all = str.split("\\r\\n"); int i = 0; OutputStream fout = new FileOutputStream(mFile); while (i &lt; all.length) { for (String commentStr : commentStrs) { all\[i] = all\[i].replaceAll(commentStr, ""); } fout.write((all\[i] + "\\r\\n").getBytes("utf-8")); i++; } fout.flush(); fout.close(); bin.close(); } catch (Exception er) { er.printStackTrace(); } } }\[/code] 3) 安装插件 mvn -Dmaven.test.skip=true install 4) 使用，在工程 pom.xml 加入如下内容： 将 &lt; &gt; " 用转义字符替换掉。 \[code="xml"] ${basedir} mac/\*\*/\*.* src/main/resources org.xmlasia.plugins edit-resource-maven-plugin 1.0-SNAPSHOT target/classes/mac/\*\*/\*.grf &lt;Property fileURL="src/main/resources/workspace.prm" id="GraphParameter0"/&gt; &lt;Property fileURL="src/main/resources/workspace.prm" id="GraphParameter1"/&gt; compile editResource \[/code] 5) 打包工程 mvn -Dmaven.test.skip=true package