package com.vci.ant.ext.tasks; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.concurrent.TimeUnit; import javax.swing.JFileChooser; import org.apache.commons.io.FileUtils; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Location; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.MatchingTask; import org.apache.tools.ant.types.FileSet; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentFactory; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.DocumentSource; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.SAXWriter; import org.dom4j.io.XMLWriter; /** * *

Title: 自动加载指定目录(包含子目录)下全部的.jar到项目的Classpath

*

Description: 自动加载指定目录(包含子目录)下全部的.jar到项目的Classpath

*

*

Copyright: Copyright (c) 2017

*

Company: VCI

* @author xiongchao * @time 2017-9-8 * @version 1.0 */ public class AutoLoadJarToClasspath extends MatchingTask { private String showChooseDialog = "true"; private String platformJarProjectPath = ""; public String getShowChooseDialog() { return showChooseDialog; } public void setShowChooseDialog(String showChooseDialog) { this.showChooseDialog = showChooseDialog; } public String getPlatformJarProjectPath() { return platformJarProjectPath; } public void setPlatformJarProjectPath(String platformJarProjectPath) { this.platformJarProjectPath = platformJarProjectPath; } @Override public void execute() throws BuildException{ try{ Project project = super.getProject(); File workspaceFile = project.getBaseDir().getParentFile().getCanonicalFile(); String workspaceFullPath = workspaceFile.getCanonicalPath(); File projectFile = project.getBaseDir().getCanonicalFile(); String projectFullPath = projectFile.getCanonicalPath(); String projectDirName = projectFile.getName(); File f = null; String jarPath = getPlatformJarProjectPath(); if(getShowChooseDialog() != null && "true".equalsIgnoreCase(getShowChooseDialog())){ try { f = getChooseFile(); if(f == null){ System.out.println("选择无效"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return; } } else { f = new File(jarPath); if(f != null){ f = f.getCanonicalFile(); } } if(f == null){ return; } String classpathFileName = projectFullPath.replace("\\", "/") + "/.classpath"; List classpathEntrys = new ArrayList(); getJarFiles(f, workspaceFullPath, classpathEntrys); SAXReader reader = new SAXReader(DocumentFactory.getInstance()); Document doc = reader.read(new File(classpathFileName)); Element root = doc.getRootElement(); @SuppressWarnings("unchecked") Iterator elements = (Iterator)root.elementIterator(); while(elements.hasNext()){ Element e = elements.next(); if(isLibClasspathEntryElement(e)){ root.remove(e); } } for (Element e : classpathEntrys) { root.add(e); } OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(doc.getXMLEncoding()); XMLWriter write = new XMLWriter(new FileWriter(classpathFileName), format); write.write(doc); write.flush(); write.close(); System.out.println(classpathFileName + " 已更新,请刷新项目(" + projectDirName + ")!"); }catch(Throwable e){ e.printStackTrace(); throw new BuildException(e); } } private boolean isLibClasspathEntryElement(Element e){ boolean res = false; Attribute attrKind = e.attribute("kind"); Attribute attrPath = e.attribute("path"); if(attrKind != null && attrPath != null && "lib".equalsIgnoreCase(attrKind.getValue()) && (attrPath.getValue().endsWith(".jar"))){ res = true; } return res; } private File getChooseFile() throws IOException{ String path = super.getProject().getBaseDir().getName(); if(path.contains("_")){ String[] paths = path.split("_"); paths[0] = "VCI-JAR"; StringBuffer sb = new StringBuffer(); for (int i = 0; i < paths.length; i++) { sb.append(paths[i]); if(i != paths.length - 1){ sb.append("_"); } } File f = new File(super.getProject().getBaseDir().getParent(), sb.toString()); System.out.println("a"+super.getProject().getBaseDir().getParent() + "/" + sb.toString()); path = f.getCanonicalPath(); System.out.println("c"+path); } else { path = super.getProject().getBaseDir().getParentFile().getCanonicalPath(); } JFileChooser jfc = new JFileChooser(path); jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); jfc.showOpenDialog(null); File f = jfc.getSelectedFile(); return f; } private void getJarFiles(File file, String workspaceFullPath, List classpathEntrys) throws IOException{ File[] childFiles = file.listFiles(); if(childFiles == null){ return; } for (File childFile : childFiles) { if(childFile.getName().equals(".svn")){ continue; } if(childFile.isDirectory()){ getJarFiles(childFile, workspaceFullPath, classpathEntrys); } else if(childFile.getName().endsWith(".jar")){ Element e = DocumentHelper.createElement("classpathentry"); e.add(DocumentHelper.createAttribute(e, "kind", "lib")); e.add(DocumentHelper.createAttribute(e, "path", childFile.getCanonicalPath().replace(workspaceFullPath, "").replace("\\", "/"))); classpathEntrys.add(e); } } } }