一、前言
基于apache的ant包(ant-1.8.2.jar)org.apache.tools.ant.Project、org.apache.tools.ant.taskdefs.Jar及org.apache.tools.ant.types.FileSet等用于对项目指定的包路径下面节点进行分别打包部署发布等操作,参考示例代码如下
import java.io.File;@b@import java.util.Arrays;@b@import java.util.List;@b@import org.apache.tools.ant.Project;@b@import org.apache.tools.ant.taskdefs.Jar;@b@import org.apache.tools.ant.types.FileSet;@b@@b@ @b@public class RuleToJar2 {@b@ @b@ private static List<String> destRules = Arrays.asList();@b@ @b@ private static final String FROM_DIR = "D:\\NJ\\workspace\\xwood-woopa";@b@ private static final String DEST_DIR = "D:\\NJ\\workspace\\xwood-woopa\\jar\\";@b@ @b@ @b@ public static void main(String[] args) {@b@ Path[] ps = Path.values();@b@ for(Path p : ps){@b@ if(p.getType() == 1){@b@ JarRecursion(Path.CLASS_PATH.getPathURL() + p.getPathURL(), p.getPathURL());@b@ }@b@ }@b@ }@b@ @b@ private static void JarRecursion(String dirpath, String pathURL){@b@ File dir = new File(dirpath);@b@ if(!dir.exists() || !dir.isDirectory()) return;@b@ File[] files = dir.listFiles();@b@ for(File f : files){@b@ String filename = f.getName();@b@ if(f.isFile() && filename.endsWith(".class")){@b@ String name = filename.substring(0, filename.lastIndexOf("."));@b@ if(isDestRule(name)){@b@ Jar(name,@b@ f,@b@ new File(Path.CLASS_PATH.getPathURL()@b@ + Path.PROPERTIES_PATH.getPathURL()@b@ + pathURL + name@b@ + "_en.properties"));@b@ }@b@ } else if(f.isDirectory()){@b@ JarRecursion(dirpath + f.getName(), pathURL+f.getName());@b@ }@b@ }@b@ }@b@ @b@ private static boolean isDestRule(String ruleName){@b@ if(destRules == null || destRules.isEmpty()){@b@ return true;@b@ }@b@ return destRules.contains(ruleName);@b@ }@b@ @b@ private static void Jar(String name, File file, File pfile){@b@ Project prj = new Project();@b@ Jar jar = new Jar();@b@ jar.setProject(prj);@b@ jar.setDestFile(new File(Path.DEST_PATH.getPathURL() + name + ".jar"));@b@ FileSet fileSet = new FileSet();@b@ fileSet.setProject(prj);@b@ fileSet.setDir(new File(Path.PATH.getPathURL()));@b@ fileSet.setIncludes("**/"+name+".class,**/"+name+"_en.properties");@b@ jar.addFileset(fileSet);@b@ jar.execute();@b@ }@b@ @b@ enum Path {@b@ PATH(-1, FROM_DIR+"\\target\\classes\\"),@b@ CLASS_PATH(0, FROM_DIR+"\\target\\classes\\com\\xwood\\woopa\\common\\"),@b@ CONF_PATH(1, "conf\\"),@b@ WEB_PATH(1, "web\\"),@b@ ENG_PATH(1, "engine\\"),@b@ ENG_PUBLISH_PATH(2, "publisher\\"), @b@ ENG_INDEX_PATH(2,"index\\"),@b@ ENG_CRAW_PATH(2,"craw\\"),@b@ @b@ PROPERTIES_PATH(3, "properties\\"),@b@ DEST_PATH(4, DEST_DIR)@b@ ;@b@ @b@ //层级0 - classes的默认包路径; 1 - common下一级;2 - engine下一级@b@ private int type;@b@ private String pathURL;@b@ @b@ Path(int type, String pathURL){@b@ this.type = type;@b@ this.pathURL = pathURL;@b@ }@b@ public int getType() {@b@ return type;@b@ }@b@ public String getPathURL() {@b@ return pathURL;@b@ }@b@ }@b@@b@}