项目中,需要强制更新业务jar包。以达到同一个jar包版本更新代码的目地。因此有了如下插件,原理也简单,读到仓库地址,直接删jar文件。主打一个简单粗爆有效。
package com.xxx.maven.plugin;
import org.apache.maven.plugin.AbstractMojo;
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 java.io.File;
import java.util.ArrayList;
import java.util.List;
@Mojo(name = "clearModel", defaultPhase = LifecyclePhase.PRE_CLEAN)
public class BusinessModelClear extends AbstractMojo {
private final static String PUBLIC_GROUP_ID = "/xxx/";
@Parameter(defaultValue = "${settings.localRepository}", readonly = true, required = true)
private String localMavenRepository;
// 清项目groupId下面的所有jar包
@Parameter(required = false)
private List<String> clearArtifactIds;
// 指定清哪些个
@Parameter(required = false, defaultValue = "false")
private String clearAll;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if ("true".equals(clearAll)) {
clearDependences(null);
return;
}else if(null!=clearArtifactIds){
clearByArtifactIds(clearArtifactIds);
}
}
private void clearByArtifactIds(List<String> artifactIds) {
if (null != artifactIds && artifactIds.size() > 0) {
for (String clearArtifactId : artifactIds) {
clearDependences(clearArtifactId);
}
}
}
private void clearDependences(String artifactId) {
if (null != artifactId) {
String jarFileForld = localMavenRepository + PUBLIC_GROUP_ID + artifactId;
this.getLog().info("zysoft current project clear dependency--> [" + jarFileForld + "]");
deleteFile(new File(jarFileForld));
} else {
String jarFileForld = localMavenRepository + PUBLIC_GROUP_ID;
this.getLog().info("zysoft current project clear dependency--> begin to clear com.zysoft.future all jars");
deleteFile(new File(jarFileForld));
}
}
private void deleteFile(File file) {
if (!file.exists()) {// 判断文件是否存在
return;
}
File[] files = file.listFiles();
if (null == files || files.length == 0) {
file.delete();
return;
}
for (File newfile : files) {// 遍历文件夹下的目录
if (newfile.isFile()) {// 如果是文件而不是文件夹==>可直接删除
newfile.delete();
} else {
deleteFile(newfile);// 是文件夹,递归调用方法
}
}
file.delete();
}
}
<build>
<plugins>
<plugin>
<groupId>com.zysoft</groupId>
<artifactId>xxx-business-model-update</artifactId>
<version>${xxx.clearmodel.plugin.version}</version>
<executions>
<execution>
<phase>clean</phase>
<goals>
<goal>clearModel</goal>
</goals>
</execution>
</executions>
<configuration>
<clearAll>true</clearAll>
</configuration>
</plugin>
</plugins>
</build>