創建 maven 插件

創建 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章