
com.njzxw.compress_maven_plugin.util.WatchDir Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of compress-maven-plugin Show documentation
Show all versions of compress-maven-plugin Show documentation
配置监控信息,在web.xml中添加
<filter>
<description>拦截获取js与css过滤</description>
<filter-name>MergeFilter</filter-name>
<filter-class>com.njzxw.filter.MergeFilter</filter-class>
<init-param>
<!-- js合并输出路径 -->
<param-name>jsDir</param-name>
<param-value>${js.path}</param-value>
</init-param>
<init-param>
<!-- css合并输出路径 -->
<param-name>cssDir</param-name>
<param-value>${css.path}</param-value>
</init-param>
</filter>
同时添加一个合并js\css对应一个merge.xml,最好放置在同级,如:
<merge>
<group name="sss" >
<js path="js/common/common.js" />
<js path="js/common/index.js" />
<css path="css/common/animate.css" />
<css path="css/common/normalize.css" />
</group>
</merge>
访问项目:xxx/sss.js
xxx/sss.css
同时配合maven插件: compress-maven-plugin结合使用:
<plugin>
<groupId>com.github.eliayng</groupId>
<artifactId>compress-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>${skipCompress}</skip>
<poolNum>50</poolNum>
<jsOutDir>${jsOutDir}</jsOutDir>
<cssOutDir>${cssOutDir}</cssOutDir>
<!--是否监控编译class文件-->
<isCompressClass>true</isCompressClass>
<!-- 是否复制资源文件 -->
<isResourcesCopy>true</isResourcesCopy>
</configuration>
</plugin>
The newest version!
package com.njzxw.compress_maven_plugin.util;
import static java.nio.file.StandardWatchEventKinds.*;
import static java.nio.file.LinkOption.*;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
public class WatchDir {
private final WatchService watcher;
private final Map keys;
private final boolean recursive;
private boolean trace = false;
@SuppressWarnings("unchecked")
static WatchEvent cast(WatchEvent> event) {
return (WatchEvent)event;
}
/**
* Register the given directory with the WatchService
*/
private void register(Path dir) throws IOException {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
if (trace) {
Path prev = keys.get(key);
if (prev == null) {
System.out.format("register: %s\n", dir);
} else {
if (!dir.equals(prev)) {
System.out.format("update: %s -> %s\n", prev, dir);
}
}
}
keys.put(key, dir);
}
/**
* Register the given directory, and all its sub-directories, with the
* WatchService.
*/
private void registerAll(final Path start) throws IOException {
// register directory and sub-directories
Files.walkFileTree(start, new FileVisitor() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException
{
register(dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
return null;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc)
throws IOException {
return null;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
return null;
}
});
}
/**
* 创建监控文件
* @param dir
* @param recursive
* @throws IOException
*/
WatchDir(Path dir, boolean recursive) throws IOException {
this.watcher = FileSystems.getDefault().newWatchService();
this.keys = new HashMap();
this.recursive = recursive;
if (recursive) {
System.out.format("Scanning %s ...\n", dir);
registerAll(dir);
System.out.println("Done.");
} else {
register(dir);
}
// enable trace after initial registration
this.trace = true;
}
/**
* Process all events for keys queued to the watcher
*/
void processEvents() {
for (;;) {//这是个死循环
// wait for key to be signalled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
Path dir = keys.get(key);
if (dir == null) {
System.err.println("WatchKey not recognized!!");
continue;
}
for (WatchEvent> event: key.pollEvents()) {
WatchEvent.Kind kind = event.kind();
// TBD - provide example of how OVERFLOW event is handled
if (kind == OVERFLOW) {
continue;
}
// Context for directory entry event is the file name of entry
WatchEvent ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
// print out event
System.out.format("%s: %s\n", event.kind().name(), child);
// if directory is created, and watching recursively, then
// register it and its sub-directories
if (recursive && (kind == ENTRY_CREATE)) {
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
registerAll(child);
}
} catch (IOException x) {
// ignore to keep sample readbale
}
}
}
// reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
keys.remove(key);
// all directories are inaccessible
if (keys.isEmpty()) {
break;
}
}
}
}
static void usage() {
System.err.println("usage: java WatchDir [-r] dir");
System.exit(-1);
}
public static void main(String[] args) throws IOException {
// parse arguments
// register directory and process its events
Path dir = Paths.get("d:\\www");
new WatchDir(dir, true).processEvents();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy