创建 maven 插件:
删除所有资源文件夹 mac 目录下面所有 grf 文件(target/classes/mac/**/*.grf)里面的内容:
<Property fileURL="src/main/resources/workspace.prm" id="GraphParameter0"/>
<Property fileURL="src/main/resources/workspace.prm" id="GraphParameter1"/>
<Property fileURL="src/main/resources/workspace.prm" id="GraphParameter2"/>
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
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 < 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();
}
}
}
3) 安装插件
mvn -Dmaven.test.skip=true install
4) 使用,在工程 pom.xml 加入如下内容:
将 < > " 用转义字符替换掉。
<build>
<resources>
<resource>
<directory>${basedir}</directory>
<includes>
<include>mac/**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.xmlasia.plugins</groupId>
<artifactId>edit-resource-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<file>target/classes/mac/**/*.grf</file>
<commentStrs>
<param><Property fileURL="src/main/resources/workspace.prm" id="GraphParameter0"/></param>
<param><Property fileURL="src/main/resources/workspace.prm" id="GraphParameter1"/></param>
</commentStrs>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>editResource</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
5) 打包工程 mvn -Dmaven.test.skip=true package