田源
2025-01-16 cfd8d4c470cc6db6f6689ebf01eae07e47a46990
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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;
 
/**
 * 
 * <p>Title: 自动加载指定目录(包含子目录)下全部的.jar到项目的Classpath</p>
 * <p>Description: 自动加载指定目录(包含子目录)下全部的.jar到项目的Classpath</p>
 * <p></p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>Company: VCI</p>
 * @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<Element> classpathEntrys = new ArrayList<Element>();
            getJarFiles(f, workspaceFullPath, classpathEntrys);
            SAXReader reader = new SAXReader(DocumentFactory.getInstance());
            Document doc = reader.read(new File(classpathFileName));
            Element root = doc.getRootElement();
            @SuppressWarnings("unchecked")
            Iterator<Element> elements = (Iterator<Element>)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<Element> 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);
            }
        }
    }
}