master
commit
cb72cd1974
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,26 @@
|
||||
```java
|
||||
<plugin>
|
||||
<groupId>xyz.wbsite</groupId>
|
||||
<artifactId>optimizer-maven-plugin</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>run</goal>
|
||||
</goals>
|
||||
<phase>compile</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<tasks>
|
||||
<task>
|
||||
<taskType>COMPRESS_CSS</taskType>
|
||||
<directory>src/main/resources/static/css/</directory>
|
||||
<targetPath>target/classes/static/css/</targetPath>
|
||||
<include>**/*.css</include>
|
||||
<exclude>**/*.min.css</exclude>
|
||||
</task>
|
||||
</tasks>
|
||||
</configuration>
|
||||
</plugin>
|
||||
```
|
@ -0,0 +1,10 @@
|
||||
package xyz.wbsite.optimizer;
|
||||
|
||||
/**
|
||||
* Created by LiuFa on 2017/10/14.
|
||||
* xyz.wbsite.optimizer
|
||||
* greatapp
|
||||
*/
|
||||
public interface Compressor<T> {
|
||||
void compress(String fileName, T config);
|
||||
}
|
@ -0,0 +1,174 @@
|
||||
package xyz.wbsite.optimizer;
|
||||
|
||||
import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
|
||||
import com.googlecode.htmlcompressor.compressor.XmlCompressor;
|
||||
import com.googlecode.htmlcompressor.compressor.YuiCssCompressor;
|
||||
import com.googlecode.htmlcompressor.compressor.YuiJavaScriptCompressor;
|
||||
import org.apache.maven.plugin.AbstractMojo;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.codehaus.plexus.util.FileUtils;
|
||||
import xyz.wbsite.optimizer.compressor.SimpleJavaScriptCompressor;
|
||||
import xyz.wbsite.optimizer.task.Task;
|
||||
import xyz.wbsite.optimizer.util.FileUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @goal run
|
||||
* @phase compile
|
||||
* @execute phase="compile"
|
||||
*/
|
||||
public class Optimizer extends AbstractMojo {
|
||||
|
||||
/**
|
||||
* @parameter
|
||||
*/
|
||||
private Task[] tasks;
|
||||
|
||||
public void execute() throws MojoExecutionException {
|
||||
getLog().info("------------------------------------------------------------------------");
|
||||
for (Task task : tasks) {
|
||||
try {
|
||||
File directory = task.getDirectory();
|
||||
File targetPath = task.getTargetPath();
|
||||
String include = task.getInclude();
|
||||
String exclude = task.getExclude();
|
||||
|
||||
List fileList = FileUtils.getFiles(directory, include, exclude);
|
||||
for (Object o : fileList) {
|
||||
if (o instanceof File) {
|
||||
File sourceFile = (File) o;
|
||||
File targetFile = new File(targetPath, sourceFile.getAbsolutePath().substring(directory.getAbsolutePath().length()));
|
||||
switch (task.getTaskType()) {
|
||||
case COMPRESS_PLAIN:
|
||||
compressPLAIN(sourceFile, targetFile);
|
||||
break;
|
||||
case COMPRESS_CSS:
|
||||
compressCSS(sourceFile, targetFile);
|
||||
break;
|
||||
case COMPRESS_JS:
|
||||
compressJS(sourceFile, targetFile);
|
||||
break;
|
||||
case COMPRESS_HTML:
|
||||
compressHTML(sourceFile, targetFile);
|
||||
break;
|
||||
case COMPRESS_XML:
|
||||
compressXML(sourceFile, targetFile);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
}
|
||||
}
|
||||
getLog().info("------------------------------------------------------------------------");
|
||||
getLog().info("The optimization task has been completed.");
|
||||
}
|
||||
|
||||
public void compressPLAIN(File sourceFile, File targetFile) {
|
||||
try {
|
||||
// 源文件大小
|
||||
long sourceLength = sourceFile.length();
|
||||
// 从源文件向目标复制文件
|
||||
FileUtils.copyFile(sourceFile, targetFile);
|
||||
// 对目标文件进行压缩
|
||||
String string = FileUtil.readFileToString(sourceFile);
|
||||
String targetString = FileUtil.trim(string);
|
||||
FileUtil.writeStringToFile(targetString, targetFile, false, false);
|
||||
// 目标文件压缩后的大小
|
||||
long targetLength = targetFile.length();
|
||||
// 打印压缩信息
|
||||
getLog().info(targetFile.getName() + FileUtil.compressorInfo(sourceLength, targetLength));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
getLog().info(sourceFile.getAbsolutePath() + " compress failed");
|
||||
}
|
||||
}
|
||||
|
||||
public void compressCSS(File sourceFile, File targetFile) {
|
||||
try {
|
||||
// 源文件大小
|
||||
long sourceLength = sourceFile.length();
|
||||
// 从源文件向目标复制文件
|
||||
FileUtils.copyFile(sourceFile, targetFile);
|
||||
// 对目标文件进行压缩
|
||||
YuiCssCompressor compressor = new YuiCssCompressor();
|
||||
String compress = compressor.compress(FileUtil.readFileToString(sourceFile));
|
||||
FileUtil.writeStringToFile(compress, targetFile, false, false);
|
||||
// 目标文件压缩后的大小
|
||||
long targetLength = targetFile.length();
|
||||
// 打印压缩信息
|
||||
getLog().info(targetFile.getName() + FileUtil.compressorInfo(sourceLength, targetLength));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
getLog().info(sourceFile.getAbsolutePath() + " compress failed");
|
||||
}
|
||||
}
|
||||
|
||||
public void compressJS(File sourceFile, File targetFile) {
|
||||
try {
|
||||
// 源文件大小
|
||||
long sourceLength = sourceFile.length();
|
||||
// 从源文件向目标复制文件
|
||||
FileUtils.copyFile(sourceFile, targetFile);
|
||||
// 对目标文件进行压缩
|
||||
YuiJavaScriptCompressor compressor = new YuiJavaScriptCompressor();
|
||||
String compress = compressor.compress(FileUtil.readFileToString(sourceFile));
|
||||
FileUtil.writeStringToFile(compress, targetFile, false, false);
|
||||
// 目标文件压缩后的大小
|
||||
long targetLength = targetFile.length();
|
||||
// 打印压缩信息
|
||||
getLog().info(targetFile.getName() + FileUtil.compressorInfo(sourceLength, targetLength));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
getLog().info(sourceFile.getAbsolutePath() + " compress failed");
|
||||
}
|
||||
}
|
||||
|
||||
public void compressHTML(File sourceFile, File targetFile) {
|
||||
try {
|
||||
// 源文件大小
|
||||
long sourceLength = sourceFile.length();
|
||||
// 从源文件向目标复制文件
|
||||
FileUtils.copyFile(sourceFile, targetFile);
|
||||
// 对目标文件进行压缩
|
||||
HtmlCompressor compressor = new HtmlCompressor();
|
||||
compressor.setRemoveIntertagSpaces(true);
|
||||
compressor.setCompressJavaScript(true);
|
||||
compressor.setJavaScriptCompressor(new SimpleJavaScriptCompressor());
|
||||
compressor.setCompressCss(true);
|
||||
String compress = compressor.compress(FileUtil.readFileToString(sourceFile));
|
||||
FileUtil.writeStringToFile(compress, targetFile, false, false);
|
||||
// 目标文件压缩后的大小
|
||||
long targetLength = targetFile.length();
|
||||
// 打印压缩信息
|
||||
getLog().info(targetFile.getName() + FileUtil.compressorInfo(sourceLength, targetLength));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
getLog().info(sourceFile.getAbsolutePath() + " compress failed");
|
||||
}
|
||||
}
|
||||
|
||||
public void compressXML(File sourceFile, File targetFile) {
|
||||
try {
|
||||
// 源文件大小
|
||||
long sourceLength = sourceFile.length();
|
||||
// 从源文件向目标复制文件
|
||||
FileUtils.copyFile(sourceFile, targetFile);
|
||||
// 对目标文件进行压缩
|
||||
XmlCompressor compressor = new XmlCompressor();
|
||||
String compress = compressor.compress(FileUtil.readFileToString(sourceFile));
|
||||
FileUtil.writeStringToFile(compress, targetFile, false, false);
|
||||
// 目标文件压缩后的大小
|
||||
long targetLength = targetFile.length();
|
||||
// 打印压缩信息
|
||||
getLog().info(targetFile.getName() + FileUtil.compressorInfo(sourceLength, targetLength));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
getLog().info(sourceFile.getAbsolutePath() + " compress failed");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package xyz.wbsite.optimizer;
|
||||
|
||||
|
||||
public enum TaskType {
|
||||
COMPRESS_PLAIN,//一般压缩(去除换行多余空格)
|
||||
COMPRESS_CSS,
|
||||
COMPRESS_JS,
|
||||
COMPRESS_HTML,
|
||||
COMPRESS_XML
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package xyz.wbsite.optimizer.compressor;
|
||||
|
||||
|
||||
import com.googlecode.htmlcompressor.compressor.Compressor;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SimpleJavaScriptCompressor implements Compressor {
|
||||
@Override
|
||||
public String compress(String source) {
|
||||
if (source == null || "".equals(source)) {
|
||||
return source;
|
||||
}
|
||||
String result = source;
|
||||
// 将空格、制表符、回车、换行符替换为空字符
|
||||
result = Pattern.compile("//.*|\\s+|\t+|\r+|\n+").matcher(result).replaceAll(" ");
|
||||
result = Pattern.compile("\\s?(=|:|;|,|:\\{|\\{|}|\\(|\\))\\s?").matcher(result).replaceAll("$1");
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
package xyz.wbsite.optimizer.task;
|
||||
|
||||
import xyz.wbsite.optimizer.TaskType;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class Task {
|
||||
/**
|
||||
* @parameter
|
||||
*/
|
||||
private TaskType taskType;
|
||||
/**
|
||||
* @parameter
|
||||
*/
|
||||
private File directory;
|
||||
/**
|
||||
* @parameter defaultValue="${project.basedir}/target"
|
||||
*/
|
||||
private File targetPath;
|
||||
/**
|
||||
* @parameter
|
||||
*/
|
||||
private String include;
|
||||
/**
|
||||
* @parameter
|
||||
*/
|
||||
private String exclude;
|
||||
|
||||
/**
|
||||
* @parameter :是否只压缩, 不对局部变量进行混淆
|
||||
*/
|
||||
private boolean JsConfigMunge = false;
|
||||
/**
|
||||
* @parameter :是否显示info和warn级别的信息
|
||||
*/
|
||||
private boolean JsConfigVerbose = false;
|
||||
/**
|
||||
* @parameter :是否保留所有的分号
|
||||
*/
|
||||
private boolean JsConfigPreserveAllSemiColons = false;
|
||||
/**
|
||||
* @parameter :是否禁用自带的所有优化措施
|
||||
*/
|
||||
private boolean JsConfigDisableOptimizations = false;
|
||||
|
||||
public TaskType getTaskType() {
|
||||
return taskType;
|
||||
}
|
||||
|
||||
public void setTaskType(TaskType taskType) {
|
||||
this.taskType = taskType;
|
||||
}
|
||||
|
||||
public File getDirectory() {
|
||||
return directory;
|
||||
}
|
||||
|
||||
public void setDirectory(File directory) {
|
||||
this.directory = directory;
|
||||
}
|
||||
|
||||
public File getTargetPath() {
|
||||
return targetPath;
|
||||
}
|
||||
|
||||
public void setTargetPath(File targetPath) {
|
||||
this.targetPath = targetPath;
|
||||
}
|
||||
|
||||
public String getInclude() {
|
||||
return include;
|
||||
}
|
||||
|
||||
public void setInclude(String include) {
|
||||
this.include = include;
|
||||
}
|
||||
|
||||
public String getExclude() {
|
||||
return exclude;
|
||||
}
|
||||
|
||||
public void setExclude(String exclude) {
|
||||
this.exclude = exclude;
|
||||
}
|
||||
|
||||
public boolean isJsConfigMunge() {
|
||||
return JsConfigMunge;
|
||||
}
|
||||
|
||||
public void setJsConfigMunge(boolean jsConfigMunge) {
|
||||
JsConfigMunge = jsConfigMunge;
|
||||
}
|
||||
|
||||
public boolean isJsConfigVerbose() {
|
||||
return JsConfigVerbose;
|
||||
}
|
||||
|
||||
public void setJsConfigVerbose(boolean jsConfigVerbose) {
|
||||
JsConfigVerbose = jsConfigVerbose;
|
||||
}
|
||||
|
||||
public boolean isJsConfigPreserveAllSemiColons() {
|
||||
return JsConfigPreserveAllSemiColons;
|
||||
}
|
||||
|
||||
public void setJsConfigPreserveAllSemiColons(boolean jsConfigPreserveAllSemiColons) {
|
||||
JsConfigPreserveAllSemiColons = jsConfigPreserveAllSemiColons;
|
||||
}
|
||||
|
||||
public boolean isJsConfigDisableOptimizations() {
|
||||
return JsConfigDisableOptimizations;
|
||||
}
|
||||
|
||||
public void setJsConfigDisableOptimizations(boolean jsConfigDisableOptimizations) {
|
||||
JsConfigDisableOptimizations = jsConfigDisableOptimizations;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package xyz.wbsite.optimizer.util;
|
||||
|
||||
/**
|
||||
* Created by LiuFa on 2017/10/14.
|
||||
* xyz.wbsite.optimizer.config
|
||||
* greatapp
|
||||
*/
|
||||
public class FileInfo {
|
||||
private String fileName;
|
||||
private long fileLength;
|
||||
private String fileSize;
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public long getFileLength() {
|
||||
return fileLength;
|
||||
}
|
||||
|
||||
public void setFileLength(long fileLength) {
|
||||
this.fileLength = fileLength;
|
||||
}
|
||||
|
||||
public String getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
public void setFileSize(String fileSize) {
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package xyz.wbsite;
|
||||
|
||||
import org.codehaus.plexus.util.FileUtils;
|
||||
import xyz.wbsite.optimizer.Optimizer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class TaskTest {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
{
|
||||
System.out.println("=================================================CSS");
|
||||
File directory = new File("E:\\workspace\\dbtool\\target\\project\\wadmin\\src\\main\\resources\\static");
|
||||
File targetPath = new File("E:\\workspace\\dbtool\\target\\project\\wadmin\\target\\classes\\static");
|
||||
String include = "**/*.css";
|
||||
String exclude = "";
|
||||
Optimizer optimizer = new Optimizer();
|
||||
|
||||
List fileList = FileUtils.getFiles(directory, include, exclude);
|
||||
for (Object o : fileList) {
|
||||
if (o instanceof File) {
|
||||
File sourceFile = (File) o;
|
||||
File targetFile = new File(targetPath, sourceFile.getAbsolutePath().substring(directory.getAbsolutePath().length()));
|
||||
optimizer.compressCSS(sourceFile, targetFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
System.out.println("=================================================JS");
|
||||
File directory = new File("E:\\workspace\\dbtool\\target\\project\\wadmin\\src\\main\\resources\\static");
|
||||
File targetPath = new File("E:\\workspace\\dbtool\\target\\project\\wadmin\\target\\classes\\static");
|
||||
String include = "**/*.js";
|
||||
String exclude = "**/*.min.js";
|
||||
Optimizer optimizer = new Optimizer();
|
||||
|
||||
List fileList = FileUtils.getFiles(directory, include, exclude);
|
||||
for (Object o : fileList) {
|
||||
if (o instanceof File) {
|
||||
File sourceFile = (File) o;
|
||||
File targetFile = new File(targetPath, sourceFile.getAbsolutePath().substring(directory.getAbsolutePath().length()));
|
||||
optimizer.compressJS(sourceFile, targetFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
System.out.println("=================================================COMPRESS_HTML");
|
||||
File directory = new File("E:\\workspace\\dbtool\\target\\project\\wadmin\\src\\main\\resources\\static");
|
||||
File targetPath = new File("E:\\workspace\\dbtool\\target\\project\\wadmin\\target\\classes\\static");
|
||||
String include = "**/*.ftl";
|
||||
String exclude = "";
|
||||
Optimizer optimizer = new Optimizer();
|
||||
|
||||
List fileList = FileUtils.getFiles(directory, include, exclude);
|
||||
for (Object o : fileList) {
|
||||
if (o instanceof File) {
|
||||
File sourceFile = (File) o;
|
||||
File targetFile = new File(targetPath, sourceFile.getAbsolutePath().substring(directory.getAbsolutePath().length()));
|
||||
optimizer.compressHTML(sourceFile, targetFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue