Windows下YUICompress实现js、css混淆压缩

首先,我们针对Linux下的部分命令进行Windows系统的对应实现

ls————cmd /c dir/b
rm————cmd /c del
mv————cmd /c move
pwd————cmd /c chdir
注:cmd /c是执行完命令后关闭命令行窗口、cmd /k是执行完命令后不关闭命令行窗口、cmd /c start是会打开新窗口,关闭旧窗口、cmd /c start是会打开新窗口,不关闭旧窗口

也可以将上述命令做成bat文件,放到Windows/System32目录下,例如,如下文件命名为ls.bat,放到对应目录下,cmd执行ls命令

@echo off
dir

在这里插入图片描述
接下来,全局安装JAVASCRIPT_OBFUSCATOR,需要提前准备好Node环境
在这里插入图片描述

#安装
npm install javascript-obfuscator -g
#检查是否安装成功
javascript-obfuscator -v

然后在系统环境变量指定path,我这里是npm安装,因此,找到npm路径,在系统用户变量的path里,例如C:\Users\zy_ys\AppData\Roaming\npm
在这里插入图片描述
加到系统变量的path里
在这里插入图片描述
还需要准备YUI压缩使用的jar包,这里使用2.4.8版本,下载地址,提取码qc1j,接下来,对应本地项目,改造如下代码,具体改造内容为项目路径、jar包路径、压缩文件路径等,完整代码如下

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通过压缩JS文件、更改日期工具类
 * 需要系统 MacOS
 * 需要安装的软件
 * yui compressor:安装方式 下载jar包 使用时指定包所在路径使用命令即可
 * uglifyJs: npm 或者 brew 安装
 * javascript-obfuscator npm安装
 *
 * @author zwf
 * @date 20190703
 */
public class CompressCommand {
	private static final String encoding = "utf-8";
	private static final String[] SUFFIXARRAY = {".js", ".css"};
	private static final String[] JS_SUFFIXARRAY = {".js"};
	private static final String TEMP = ".temp";
	/**
	 * YUI_PATH 压缩使用的jar包路径
	 */
	private static final String YUI_PATH = "C:\\Users\\zy_ys\\Desktop\\yuicompressor-2.4.8.jar";

	/**
	 * JAVASCRIPT_OBFUSCATOR 命令所在路径
	 */
	private static final String JAVASCRIPT_OBFUSCATOR_CMD = "javascript-obfuscator";
	/**
	 * 项目根路径开始的js路径
	 */
	private static final String RESOURCES_PATH = "project\\src\\main\\resources\\static\\";
	/**
	 * 项目根路径开始的HTML路径
	 */
	private static final String RESOURCES_HTML_PATH = "project\\src\\main\\resources\\templates\\";
	/**
	 * JS的文件夹名称
	 */
	private static final String[] CSS_JS_DIRECTION = {"js\\aaa", "js\\bbb", "js\\ccc","js\\login","js\\ddd", "js\\eee", "js\\fff", "js\\ggg", "css"};
	/**
	 * HTML的文件夹名称
	 */
	private static final String[] DIRECTION_HTML = {"aaa", "download", "error", "ccc", "ddd", "bbb", "eee", "fff"};

	/**
	 *
	 */
	private static final String DATE_STAMP = System.currentTimeMillis() + "";
	/**
	 * 所有未压缩JS的集合
	 */
	private static List<String> RESOURCE_LIST = new ArrayList<>();
	/**
	 * 所有的中间文件HTML
	 */
	private static List<String> TEMP_HTML_LIST = new ArrayList<>();
	/**
	 * 项目跟路径
	 */
	private static String PROJECT_PATH = "";

	private static String CSS_COMPRESS_ONLY = "NO";

	static {
		Runtime runtime = Runtime.getRuntime();
		BufferedReader bufferedReader = null;
		try {
			Process process = runtime.exec("cmd /c chdir");
			if (process.waitFor() == 0) {
				bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
				PROJECT_PATH = bufferedReader.readLine();
				System.out.println(PROJECT_PATH);
			}
			if (StringUtils.isBlank(PROJECT_PATH)) {
				System.err.println("获取当前项目路径操作失败, 停止操作!");
				System.exit(-1);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (null != bufferedReader) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	enum STEP {
		/**
		 * 第一步:删除 min.js 文件
		 */
		STEP_ONE_RM_OLD_MIN_JS,
		/**
		 * 第二步:生成新的 min.js 文件
		 */
		STEP_TWO_CREATE_NEW_MIN_JS,
		/**
		 * 第三步: 生成htm中间文件
		 */
		STEP_THREE_CREATE_COPY_TEMPLATE,
		/**
		 * 第四步: 替换中间文件的min.js
		 */
		STEP_FOUR_REPLACE_MIN_JS,
		/**
		 * 第五步: 还原html文件
		 */
		STEP_FIVE_DELETE_TEMPLATE,

	}

	enum CompressObfuscator {
		YUI_COMPRESSOR,
		UGLIFY_JS,
		JAVASCRIPT_OBFUSCATOR
	}

	enum EVN {
		DEBUG,
		PUBLISH,
	}

	public static void main(String[] args) throws Exception {
		EVN evn = EVN.DEBUG;
		//DEBUG是初始化,PUBLISH是混淆压缩
		System.out.println(" 请输入你需要操作的模式? DEBUG 或者 PUBLISH ");
		Scanner scanner = new Scanner(System.in);
		String command = scanner.nextLine();
		if ("debug".equalsIgnoreCase(command)) {
			evn = EVN.DEBUG;
		} else if ("publish".equalsIgnoreCase(command)) {
			evn = EVN.PUBLISH;
		} else {
			System.exit(0);
		}

		switch (evn) {
			case DEBUG:
				// 还原html文件中的js文件
				devOps();
				break;
			case PUBLISH:
				masterOps();
				break;
		}

	}

	enum DevStep {
		RM_MIN_JS,
		RP_TEMPLATE_COPY,
		RM_TEMPLATE,

	}

	private static void devOps() throws Exception {
		// 第一步: 删除min.js
		List<String> commandList = initRmJsCommandList();
		execDevCommand(commandList, DevStep.RM_MIN_JS);
		// 第二步: 生成中间文件
		List<String> commandList1 = initTempLateCommandList();
		execDevCommand(commandList1, DevStep.RP_TEMPLATE_COPY);
		// 第三步: 替换js
		reverseReplaceJsInHtml();
		// 第四步: 删除html中间文件
		List<String> backCommand = initDelTemplateList();
		execDevCommand(backCommand, DevStep.RM_TEMPLATE);
	}

	private static void execDevCommand(List<String> commandList, DevStep devStep) {
		switch (devStep) {
			case RM_MIN_JS:
				System.out.println("开始删除min.js文件-----");
				execute(commandList);
				System.out.println("结束删除min.js文件-----");
				break;
			case RP_TEMPLATE_COPY:
				System.out.println("开始生成HTML中间文件-----");
				execute(commandList);
				System.out.println("结束生成HTML中间文件-----");
			case RM_TEMPLATE:
				System.out.println("开始删除html中间文件");
				execute(commandList);
				System.out.println("结束删除html中间文件");
				break;

		}
	}

	private static void masterOps() throws Exception {
		// step 1 :初始化min.js删除命令集合
		List<String> rmCmdList = initRmJsCommandList();
		execCommand(rmCmdList, STEP.STEP_ONE_RM_OLD_MIN_JS);
		//step 2: 压缩文件,生成新的min.js文件, 如使用yui压缩, 注释掉第一行compressJSCommandList
		List<String> compressJSCommandList = getCompressCommandList(CompressObfuscator.JAVASCRIPT_OBFUSCATOR);
		List<String> compressCssCommandList = getCompressCommandList(CompressObfuscator.YUI_COMPRESSOR);
		execCommand(compressJSCommandList, STEP.STEP_TWO_CREATE_NEW_MIN_JS);
		execCommand(compressCssCommandList, STEP.STEP_TWO_CREATE_NEW_MIN_JS);
		//step 3: 生成html中间文件
		List<String> tempTempLateCommandList = initTempLateCommandList();
		execCommand(tempTempLateCommandList, STEP.STEP_THREE_CREATE_COPY_TEMPLATE);
		//step 4: 替换中间文件的min.js
		replaceJsInHtml();
		//step 5: 恢复HTML文件
		List<String> backCommand = initDelTemplateList();
		execCommand(backCommand, STEP.STEP_FIVE_DELETE_TEMPLATE);
	}

	private static List<String> getCompressCommandList(CompressObfuscator javascriptObfuscator) throws Exception {
		switch (javascriptObfuscator) {
			case YUI_COMPRESSOR:
				return initCommandList(YUI_PATH);
			case UGLIFY_JS:
				CSS_COMPRESS_ONLY = "YES";
				return initUglifyJsCommandList();
			case JAVASCRIPT_OBFUSCATOR:
				CSS_COMPRESS_ONLY = "YES";
				return initJavascriptObfuscatorCommandList();
			default:
				throw new RuntimeException("出错了,停止吧!");
		}
	}


	private static void execCommand(List<String> commandList, STEP step) throws Exception {
		switch (step) {
			case STEP_ONE_RM_OLD_MIN_JS:
				System.out.println("开始清理旧的min.js文件---");
				execute(commandList);
				System.out.println("清理旧的min.js文件结束");
				break;
			case STEP_TWO_CREATE_NEW_MIN_JS:
				System.out.println("开始生成新的min.js文件--");
				execute(commandList);
				System.out.println("结束生成新的min.js文件--");
				break;
			case STEP_THREE_CREATE_COPY_TEMPLATE:
				System.out.println("开始生成新的html中间文件--");
				execute(commandList);
				System.out.println("结束生成新的html中间文件--");
				break;
			case STEP_FIVE_DELETE_TEMPLATE:
				System.out.println("开始还原html文件--");
				execute(commandList);
				System.out.println("结束还原html文件--");
				break;
			default:
		}
	}

	private static List<String> initUglifyJsCommandList() throws Exception {
		List<String> cmdList = new ArrayList<>();
		for (String directionJ : CSS_JS_DIRECTION) {
			String lsCmd = "ls " + PROJECT_PATH + "/" + RESOURCES_PATH + directionJ;
			Process process = Runtime.getRuntime().exec(lsCmd);
			InputStream inputStream = null;
			BufferedReader bufferedReader = null;
			if (process.waitFor() == 0) {
				inputStream = process.getInputStream();
				bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
				String line = null;
				while ((line = bufferedReader.readLine()) != null) {
					String suffix = line.substring(line.lastIndexOf("."));

					List<String> suffixList = Arrays.asList(JS_SUFFIXARRAY);
					if (suffixList.contains(suffix) && !line.endsWith("-min" + suffix)) {
						StringBuffer _path = new StringBuffer();
						_path.append(PROJECT_PATH).append("/");
						_path.append(RESOURCES_PATH).append(directionJ).append("/").append(line).append(" ");
						RESOURCE_LIST.add(_path.toString());

						StringBuffer sb = new StringBuffer();
						sb.append("uglifyjs ");
						sb.append(_path.toString()).append(" ");
						sb.append("-c ");
						sb.append("--ie8 ");
						sb.append("-m ");
						sb.append("-o").append(" ");
						sb.append(PROJECT_PATH).append("/");
						sb.append(RESOURCES_PATH).append(directionJ).append("/").append(line.replace(suffix, "-" + DATE_STAMP + "-min" + suffix));
						cmdList.add(sb.toString());
					}
				}
			}
		}
		System.out.println("初始化压缩命令成功");
		return cmdList;
	}

	private static List<String> initJavascriptObfuscatorCommandList() throws Exception {

		List<String> cmdList = new ArrayList<>();
		for (String directionJ : CSS_JS_DIRECTION) {
			String lsCmd = "cmd /c dir/b " + PROJECT_PATH + "\\" + RESOURCES_PATH + directionJ;
			Process process = Runtime.getRuntime().exec(lsCmd);
			InputStream inputStream = null;
			BufferedReader bufferedReader = null;
			if (process.waitFor() == 0) {
				inputStream = process.getInputStream();
				bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
				String line = null;
				while ((line = bufferedReader.readLine()) != null) {
					String suffix = line.substring(line.lastIndexOf("."));

					List<String> suffixList = Arrays.asList(JS_SUFFIXARRAY);
					if (suffixList.contains(suffix) && !line.endsWith("-min" + suffix)) {
						StringBuffer _path = new StringBuffer();
						_path.append(PROJECT_PATH).append("\\");
						_path.append(RESOURCES_PATH).append(directionJ).append("\\").append(line).append(" ");
						RESOURCE_LIST.add(_path.toString());

						StringBuffer sb = new StringBuffer();
						sb.append(" cmd /c ");
						sb.append(JAVASCRIPT_OBFUSCATOR_CMD).append(" ");
						sb.append(_path.toString()).append(" ");
						sb.append("--output").append(" ");
						sb.append(PROJECT_PATH).append("\\");
						sb.append(RESOURCES_PATH).append(directionJ).append("\\").append(line.replace(suffix, "-" + DATE_STAMP + "-min" + suffix));
						cmdList.add(sb.toString());
					}
				}
			}
		}
		System.out.println("初始化压缩命令成功");
		return cmdList;
	}

	private static List<String> initDelTemplateList() throws Exception {
		List<String> cmdList = new ArrayList<>();
		for (String htmlFilePath : TEMP_HTML_LIST) {
			StringBuffer sb = new StringBuffer();
			sb.append("cmd /c del ").append(" ");
			sb.append(htmlFilePath).append(" ");
			cmdList.add(sb.toString());
		}
		return cmdList;
	}

	private static List<String> initTempLateCommandList() throws Exception {
		List<String> cmdList = new ArrayList<>();
		for (String directionH : DIRECTION_HTML) {
			String lsCmd = "cmd /c dir/b " + PROJECT_PATH + "\\" + RESOURCES_HTML_PATH + directionH;
			Runtime runtime = Runtime.getRuntime();
			Process process = runtime.exec(lsCmd);
			if (process.waitFor() == 0) {
				InputStream inputStream = null;
				BufferedReader bufferedReader = null;
				try {
					inputStream = process.getInputStream();
					bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
					String line = null;
					while ((line = bufferedReader.readLine()) != null) {
						if (line.endsWith(".html")) {
							StringBuilder stringBuilder = new StringBuilder();
							stringBuilder.append("cmd /c move").append(" ");
							stringBuilder.append(PROJECT_PATH).append("\\");
							stringBuilder.append(RESOURCES_HTML_PATH).append(directionH).append("\\").append(line).append(" ");
							StringBuffer stringBuffer = new StringBuffer();
							stringBuffer.append(PROJECT_PATH).append("\\");
							stringBuffer.append(RESOURCES_HTML_PATH).append(directionH).append("\\").append(line).append(TEMP).append(" ");
							TEMP_HTML_LIST.add(stringBuffer.toString());
							cmdList.add(stringBuilder.append(stringBuffer.toString()).toString());
						}
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				} finally {
					if (inputStream != null) {
						inputStream.close();
					}
					if (bufferedReader != null) {
						bufferedReader.close();
					}
				}
			}
		}
		return cmdList;
	}


	private static synchronized void execute(List<String> commandList) {
		long beginTime = System.currentTimeMillis();
		int count = 0;
		//这里实际定义的是环境变量的地址,我们配置了path,可以忽略
		String[] envp = new String[] {"PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"};
		for (String command : commandList) {
			try {
				Runtime runtime = Runtime.getRuntime();
				// Process process = runtime.exec(command, envp);
				Process process = runtime.exec(command);
				System.out.println(command);
				// 如js无法进行压缩打印命令报错信息
				BufferedInputStream bis = new BufferedInputStream(process.getErrorStream());
				BufferedReader br = new BufferedReader(new InputStreamReader(bis,"GB2312"));
				String line;
				while ((line = br.readLine()) != null) {
					System.out.println(line);
				}
				bis.close();
				br.close();
				if (process.waitFor() == 0) {
					count++;
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		long endTime = System.currentTimeMillis();
		System.out.println("操作执行完成:共花费 " + (endTime - beginTime) + " ms, 共执行了 " + count + " 个文件");
	}

	private static List<String> initRmJsCommandList() throws Exception {
		List<String> cmdList = new ArrayList<>();
		for (String directionJ : CSS_JS_DIRECTION) {
			String lsCmd = "cmd /c dir/b " + PROJECT_PATH + "\\" +  RESOURCES_PATH + directionJ;
			Runtime runtime = Runtime.getRuntime();
			Process process = runtime.exec(lsCmd);
			if (process.waitFor() == 0) {
				InputStream inputStream = null;
				BufferedReader bufferedReader = null;
				try {
					inputStream = process.getInputStream();
					bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
					String line = null;
					while ((line = bufferedReader.readLine()) != null) {
						if (line.endsWith("-min.js") || line.endsWith("-min.css")) {
							StringBuilder stringBuilder = new StringBuilder();
							stringBuilder.append("cmd /c del").append(" ").append(" ").append(PROJECT_PATH).append("\\").append(RESOURCES_PATH).append(directionJ).append("\\").append(line);
							System.out.println("cmd:-->"+stringBuilder.toString());
							cmdList.add(stringBuilder.toString());
						}
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				} finally {
					if (inputStream != null) {
						inputStream.close();
					}
					if (bufferedReader != null) {
						bufferedReader.close();
					}
				}
			}
		}

		return cmdList;
	}

	private static void replaceJsInHtml() throws Exception {
		for (String directionHtml : DIRECTION_HTML) {
			String absoluteDir = PROJECT_PATH + "\\" + RESOURCES_HTML_PATH + directionHtml;
			System.out.println("absoluteDir:--->"+absoluteDir);
			File htmlFile = new File(absoluteDir);
			File[] htmlFiles = htmlFile.listFiles();
			for (File hFile : htmlFiles) {
				BufferedReader bufferedReader = null;
				BufferedWriter bufferedWriter = null;
				try {
					String newFilePath = hFile.getAbsolutePath().substring(0, hFile.getAbsolutePath().indexOf(TEMP));
					bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(hFile)));
					bufferedWriter = new BufferedWriter((new OutputStreamWriter(new FileOutputStream(new File(newFilePath)))));
					String line = null;
					while ((line = bufferedReader.readLine()) != null) {
						if (line.trim().contains("script") && line.trim().contains("src")) {
							Attribute srcAttribute = null;
							Document document = Jsoup.parse(line.trim());
							Iterator<Element> iterator = document.select("script").iterator();
							while (iterator.hasNext()) {
								Element next = iterator.next();
								Attributes attributes = next.attributes();
								for (Attribute attribute : attributes) {
									if ("src".equals(attribute.getKey())) {
										srcAttribute = attribute;
										break;
									}
								}
								break;
							}
							boolean w = false;
							if (srcAttribute != null) {
								for (String absolutePathName : RESOURCE_LIST) {
									if (!absolutePathName.trim().endsWith(".js")) {
										continue;
									}
									// 获取js路径上的上一级和js名称
									String[] split = absolutePathName.split("\\\\");
									String jsDirName = split[split.length - 2];
									String jsName = split[split.length - 1];
									String jsPreName = jsName.substring(0, jsName.indexOf(".js"));
									String[] srcSpilt = srcAttribute.getValue().split("\\/");
									String srcJsName = srcSpilt[srcSpilt.length - 1];
									String srcJsDirName = srcSpilt[srcSpilt.length - 2];
									if (!absolutePathName.contains("min.js") && jsDirName.equals(srcJsDirName) && srcJsName.contains(jsPreName)) {
										String newSrc = srcAttribute.getValue().substring(0, srcAttribute.getValue().indexOf(srcJsDirName + "/" + srcJsName));
										int startIndex = line.indexOf(jsDirName + "/" + jsPreName);
										int endIndex = line.indexOf(".js\"");
										String oldJsName = line.substring(startIndex, endIndex);
										String newLine = line.replace(oldJsName, jsDirName + "/" + jsPreName + "-" + DATE_STAMP + "-min");
										// 包含当前js文件,替换
										bufferedWriter.write(newLine + "\n");
										w = true;
									}
								}
							}
							if (!w) {
								bufferedWriter.write(line + "\n");
							}
						} else if (line.trim().contains("link") && line.trim().contains("href") && line.trim().contains("css")) {
							Attribute srcAttribute = null;
							Document document = Jsoup.parse(line.trim());
							Iterator<Element> iterator = document.select("link").iterator();
							while (iterator.hasNext()) {
								Element next = iterator.next();
								Attributes attributes = next.attributes();
								for (Attribute attribute : attributes) {
									if ("href".equals(attribute.getKey())) {
										srcAttribute = attribute;
										break;
									}
								}
								break;
							}
							boolean w = false;
							if (srcAttribute != null) {
								for (String absolutePathName : RESOURCE_LIST) {
									if (!absolutePathName.trim().endsWith(".css")) {
										continue;
									}
									// 获取css路径上的上一级和css名称
									String[] split = absolutePathName.split("\\\\");
									String cssDirName = split[split.length - 2];
									String cssName = split[split.length - 1];
									//System.out.println(cssName);
									String cssPreName = cssName.substring(0, cssName.indexOf(".css"));
									String[] srcSpilt = srcAttribute.getValue().split("\\/");
									String srcJsName = srcSpilt[srcSpilt.length - 1];
									String srcJsDirName = srcSpilt[srcSpilt.length - 2];
									if (!absolutePathName.contains("min.css") && cssDirName.equals(srcJsDirName) && srcJsName.contains(cssPreName)) {
										int startIndex = line.indexOf(cssDirName + "/" + cssPreName);
										int endIndex = line.indexOf(".css\"");
										String oldJsName = line.substring(startIndex, endIndex);
										String newLine = line.replace(oldJsName, cssDirName + "/" + cssPreName + "-" + DATE_STAMP + "-min");
										// 包含当前css文件,替换
										bufferedWriter.write(newLine + "\n");
										w = true;
									}
								}
							}
							if (!w) {
								bufferedWriter.write(line + "\n");
							}
						} else {
							bufferedWriter.write(line + "\n");
						}
						bufferedWriter.flush();
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				} finally {
					if (null != bufferedReader) {
						bufferedReader.close();
					}
					if (null != bufferedWriter) {
						bufferedWriter.close();
					}
				}
			}
		}
	}

	private static void reverseReplaceJsInHtml() throws Exception {
		for (String directionHtml : DIRECTION_HTML) {
			String absoluteDir = PROJECT_PATH + "\\" + RESOURCES_HTML_PATH + directionHtml;
			File htmlFile = new File(absoluteDir);
			File[] htmlFiles = htmlFile.listFiles();
			for (File hFile : htmlFiles) {
				BufferedReader bufferedReader = null;
				BufferedWriter bufferedWriter = null;
				try {
					String newFilePath = hFile.getAbsolutePath().substring(0, hFile.getAbsolutePath().indexOf(TEMP));
					bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(hFile)));
					bufferedWriter = new BufferedWriter((new OutputStreamWriter(new FileOutputStream(new File(newFilePath)))));
					String line = null;
					while ((line = bufferedReader.readLine()) != null) {
						String regex = "-[0-9]{13}-min";
						Pattern pattern = Pattern.compile(regex);
						Matcher matcher = pattern.matcher(line.trim());
						String datestamp_min_js = null;
						while (matcher.find()) {
							datestamp_min_js = matcher.group();
						}
						if(StringUtils.isNotBlank(datestamp_min_js)){
							String newLine = line.replace(datestamp_min_js, "");
							bufferedWriter.write(newLine + "\n");
						}else {
							bufferedWriter.write(line + "\n");
						}
						bufferedWriter.flush();
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				} finally {
					if (null != bufferedReader) {
						bufferedReader.close();
					}
					if (null != bufferedWriter) {
						bufferedWriter.close();
					}
				}
			}
		}
	}

	/**
	 * 初始化压缩命令
	 *
	 * @param yuiPath
	 */
	private static List<String> initCommandList(String yuiPath) throws Exception {
		List<String> cmdList = new ArrayList<>();
		String[] cssOnly = {".css"};
		String[] thisSuffixArray = SUFFIXARRAY;
		if ("YES".equals(CSS_COMPRESS_ONLY)) {
			thisSuffixArray = cssOnly;
		}
		for (String directionJ : CSS_JS_DIRECTION) {
			String lsCmd = "cmd /c dir/b " + PROJECT_PATH + "\\" + RESOURCES_PATH + directionJ;
			Process process = Runtime.getRuntime().exec(lsCmd);
			InputStream inputStream = null;
			BufferedReader bufferedReader = null;
			if (process.waitFor() == 0) {
				inputStream = process.getInputStream();
				bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
				String line = null;
				while ((line = bufferedReader.readLine()) != null) {
					String suffix = line.substring(line.lastIndexOf("."));

					List<String> suffixList = Arrays.asList(thisSuffixArray);
					if (suffixList.contains(suffix) && !line.endsWith("-min" + suffix)) {
						StringBuffer sb = new StringBuffer();
						sb.append(" cmd /c");
						sb.append(" ");
						sb.append("java -jar ");
						sb.append(yuiPath);
						sb.append(" --type ");
						sb.append(suffix.substring(suffix.indexOf(".") + 1));
						sb.append(" --charset ");
						sb.append(encoding).append(" ");
						sb.append(" --preserve-semi ");
						StringBuffer _path = new StringBuffer();
						_path.append(PROJECT_PATH).append("\\");
						_path.append(RESOURCES_PATH).append(directionJ).append("\\").append(line).append(" ");
						RESOURCE_LIST.add(_path.toString());
						sb.append(_path);
						sb.append(">").append(" ");
						sb.append(PROJECT_PATH).append("\\");
						sb.append(RESOURCES_PATH).append(directionJ).append("\\").append(line.replace(suffix, "-" + DATE_STAMP + "-min" + suffix));
						cmdList.add(sb.toString());
					}
				}
			}
		}
		System.out.println("初始化压缩命令成功");
		return cmdList;
	}
}

如果遇到问题,可以debug看下是否配置了正确的执行路径、项目路径等,如果要压缩混淆的文件很少,也可以配置好上述内容后,执行如下命令

javascript-obfuscator D:\project\src\main\resources\static\js\login\login.js  --output D:\project\src\main\resources\static\js\login\login-1689667293081-min.js

这里login.js是我们要压缩混淆的文件,login-1689667293081-min.js是压缩混淆后的代码,挂上具体时间戳,然后,将其名称覆盖到对应的html页面即可

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/43314.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

关于计算机的各种编码

ASCII编码 ASCII (American Standard Code for Information Interchange)&#xff1a;美国信息交换标准代码是基于的一套电脑编码系统&#xff0c;主要用于显示现代英语和其他语言。它是最通用的标准&#xff0c;并等同于国际标准 ISO/IEC 646。ASCII第一次以规范标准的类型发表…

如何在 SwiftUI 中使用 Touch ID 和 Face ID?

1. 需要通过指纹&#xff0c;面容认证后才能打开 App 2. 添加配置 需要向 Info.plist 文件中添加一个配置&#xff0c;向用户说明为什么要访问 添加 Privacy - Face ID Usage Description 并为其赋予值 $(PRODUCT_NAME) need Touch Id or Face ID permission for app lock 3. …

sql中group by 的使用

1、概述 Group By 从字面意义上理解就是根据By指定的规则对数据进行分组&#xff0c;所谓的分组就是将一个数据集划分为若干个小区域&#xff0c;然后针对若干个小区域进行数据处理 2、原始表 3、简单的Group By 示例1 select 类别&#xff0c;数量 as 数量之和 from A gro…

​MySQL高阶语句(三)

目录 1、内连接 2、左连接 3、右连接&#xff1a; 二、存储过程⭐⭐⭐ 4. 调用存储过程 5.查看存储过程 5.1 查看存储过程 5.2查看指定存储过程信息 三. 存储过程的参数 3.1存储过程的参数 3.2修改存储过程 四.删除存储过程 MySQL 的连接查询&#xff0c;通常都是将来…

(css)原生html实现遮罩层弹窗

(css)原生html实现遮罩层弹窗 效果&#xff1a; html <div class"overlay"><div class"content"><!-- 需要遮罩的内容 --> <el-table :data"tableData" size"mini" class"table-class" border stripe…

解决阿里云服务器不能访问端口

服务器已经下载了redis&#xff0c;kafka&#xff0c;但就是访问不了端口号&#xff0c; 开通云服务器以后&#xff0c;请一定在安全组设置规则&#xff0c;放行端口 防火墙要关闭

网络安全基础知识解析:了解常见的网络攻击类型、术语及其防范方法

目录 1、网络安全常识和术语 1.1资产 1.2网络安全 1.3漏洞 1.4 0day 1.5 1day 1.6后门 1.7exploit 1.8攻击 1.9安全策略 1.10安全机制 1.11社会工程学 2、为什么会出现网络安全问题&#xff1f; 2.1网络的脆弱性 2.4.1缓冲区溢出攻击原理&#xff1a; 2.4.2缓冲…

【简单认识MySQL函数和高级语句】

文章目录 一.常用查询1.按关键字排序&#xff08;ORDER BY 语句&#xff09;1、语法格式2、 ASC和DESC的排序概念3、举例1、按分数排序&#xff0c;默认不指定是升序排列2、分数按降序排列3、order by 还可以结合where进行条件过滤&#xff0c;筛选地址是南京的学生按分数降序排…

数据可视化——绘制带有时间线的柱状图

文章目录 前言如何绘制柱状图添加时间线根据提供的数据绘制动态柱状图读取并删除无用数据将数据转换为字典创建柱状图并添加到时间线中配置选项并生成带有数据的折线图 前言 我们已经学习了使用 pyecharts 包中的模块和相应的方法绘制了折线图和地图&#xff0c;那么今天我将为…

【MySQL】索引

索引 概念作用优势和劣势具体操作方式创建索引自动手动创建 查看索引删除索引 索引的数据结构哈希表二叉搜索树平衡二叉树B树B 树 概念 索引是一种特殊的文件&#xff0c;包含着对数据表里所有记录的引用指针。可以对表中的一列或多列创建索引&#xff0c;并指定索引的类型&am…

中移链与BSN分布式云管平台集成,共同构建专属协同体系

01 中移链与BSN分布式云管平台集成&#xff0c; 融入BSN生态体系 中移链OPB&#xff08;OPB即开放联盟链&#xff0c;Open Permissioned Blockchain&#xff09;与BSN基于BSN分布式云管平台&#xff0c;打造了中移链专属门户、中移链专属运营、中移链专属运维功能模块&#x…

WebRTC带宽评估 -- Transport-wide Congestion Control

简述&#xff1a;在RTP包中增加transport-wide-cc扩展头&#xff0c;放置传输层面的包序号。视频接收端记录RTP包的接收时间&#xff0c;并通过RTCP Feedback消息反馈到视频发送端&#xff0c;发送端结合缓存的RTP包发送时间&#xff0c;基于丢包和延迟估算当前带宽&#xff0c…

【数据架构】Data Fabric 架构是实现数据管理和集成现代化的关键

D&A 领导者应该了解数据编织架构的关键支柱&#xff0c;以实现机器支持的数据集成。 在日益多样化、分布式和复杂的环境中&#xff0c;数据管理敏捷性已成为组织的任务关键优先事项。为了减少人为错误和总体成本&#xff0c;数据和分析 (D&A) 领导者需要超越传统的数据…

(三)springboot实战——web新特性之函数式实现

前言 本节内容我们主要介绍一下web访问的另一种形式&#xff0c;通过函数式web实现一个restful风格的http请求案例。函数式web是spring5.2之后的一个新特性&#xff0c;可以通过函数去定义web请求的处理流程&#xff0c;使得代码更为简洁&#xff0c;耦合性也降低了。 正文 …

数据结构day7(2023.7.23)

一、Xmind整理&#xff1a; 二、课上练习&#xff1a; 练习1&#xff1a;结点之间的关系 练习2&#xff1a;二叉树的特殊形态 练习3&#xff1a;满二叉树的形态 练习4&#xff1a;完全二叉树的形态 满二叉树一定是完全二叉树&#xff0c;完全二叉树不一定是满二叉树 练习5&am…

Window下编译ffmpeg

Window下编译ffmpeg 下载MSYS2编译ffmpeg运行 下载MSYS2 MSYS2是一个是工具和库的集合&#xff0c;它能够方便的在windows上编译、安装和运行程序。ffmpeg可以通过这个软件来编译。 从MSYS2官网下载MSYS2并安装。 运行MSYS2终端&#xff0c;在终端中输入命令&#xff0c;安装…

07统计模型练习

使用SPSS进行分析求解 第一题 下表1.1是中国1994-2016年国内旅游总花费Y、国内生产总值X1、铁路里程X2和公路里程X3的数据,请据此分析如下问题: (1)就建立简单线性回归模型,分别分析中国国内旅游总花费与国内生产总值、铁路里程和公路里程数据的数量关系。 (2)对建立的回归模型…

JVS开源基础框架:用户管理介绍(支持同步钉钉、企微、微信等)

在企业内部系统中&#xff0c;用户管理是指对系统内的用户进行管理、授权和权限管理的过程&#xff0c;这里主要介绍用户的创建与基本信息的管理&#xff0c;权限、登录等详细介绍请参考相关章节。 用户管理界面 点击平台管理-用户管理&#xff0c;界面上展示了组织管理与组织…

potplayer放大画面,画面拖拽。备份

放大画面&#xff1a; 按住alt和鼠标左键&#xff0c;就可以拖动放大后的画面了 窗口化示图 倍数调整 默认只能0.1往上加&#xff0c;但是有的视频0.08倍数才正好&#xff0c;需要精确到2位小数。

基于Vue+Element Plus实现表格组件

目录 前言分析实现例子效果图前言 表格对于管理类项目是很重要的,可以只管的展示和比比较数据。使用Element Plus能解决一部分问题,但是还存在一些缺点和不足。 分析 浏览器上表格数据展示空间不足。列显示太多不够直观。完全依赖官方表格组件代码过于臃肿不利于管理和优化…