介绍
大家可能都会碰到一些代码比较敏感的项目场景,这个时候代码被反编译看到就不好了,这个时候就需要代码混淆插件来对代码进行混淆了。
基于Maven的项目一般会去考虑使用proguard-maven-plugin,但是这个插件仅支持打Jar包不支持打War包。
于是我用空闲时间在proguard-maven-plugin的基础上修改了里面的一部分逻辑,可以在项目构建过的时候把代码混淆,支持打成jar包和war包。
现在贴出来给大家看看。
项目地址
https://github.com/lovethegirl/code-hidding-plugin
修改部分
War包压缩解压的工具类
package com.github.wvengen.maven.proguard;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;
/**
* 处理WAR文件工具类。可压缩或解压缩WAR文件。
*
* @author Xiong Shuhong(shelltea@gmail.com)
*/
public class WarUtils {
public static void unzip(String warPath, String unzipPath) {
File warFile = new File(warPath);
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(
warFile));
ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(
ArchiveStreamFactory.JAR, bufferedInputStream);
JarArchiveEntry entry = null;
while ((entry = (JarArchiveEntry) in.getNextEntry()) != null) {
File file = new File(unzipPath, entry.getName());
if (file.exists()) {
file.delete();
}
if (entry.isDirectory()) {
file.mkdir();
} else {
OutputStream out = FileUtils.openOutputStream(file);
IOUtils.copy(in, out);
out.close();
}
}
in.close();
} catch (FileNotFoundException e) {
System.err.println("未找