转载自 http://blog.csdn.net/hlf48641715/article/details/7188450
非常感谢博主的文章,博主用的python写的批量下载源码,现在提供一个java版本的批量下载源码
package com.pactera.android.download;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DownLoadSrc {
/**
* @param args
*/
// 源码存放路径
private static String downloadDir;
// xml路径
private static String defaultXMLFile;
private static List<Project> projects = new ArrayList<DownLoadSrc.Project>();
public static void main(String[] args) {
downloadDir = "E:/android_src";
defaultXMLFile = "F:/source/manifest/default.xml";
File downLoadFile = new File(downloadDir);
if (!downLoadFile.exists())
downLoadFile.mkdirs();
getProjects(defaultXMLFile);
downLoadSrc();
System.out.println("恭喜你,下载完成!!!!!");
}
private static void downLoadSrc() {
// git program path
String git = "E:/Program Files/Git/bin/git.exe";
String prefix = git + " clone https://android.googlesource.com/";
String suffix = ".git";
for (Project project : projects) {
String path = project.getPath();
String pathsp[] = path.split("/");
if (pathsp.length > 0) {
path = pathsp[0];
}
String childrenPath = downloadDir + "/" + path;
File childrenFile = new File(childrenPath);
if (!childrenFile.exists()) {
childrenFile.mkdirs();
}
String command = prefix + project.getName() + suffix + " "
+ childrenPath;
Runtime r = Runtime.getRuntime();
try {
r.exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
static void getProjects(String fileName) {
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(new File(fileName));
List<String> ps = doc.selectNodes("manifest/project");
Iterator it = ps.iterator();
while (it.hasNext()) {
Element elm = (Element) it.next();
String name = elm.attributeValue("name");
String path = elm.attributeValue("path");
// System.out.println("name:" + name + ",path:" + path);
projects.add(new Project(name, path));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
static private class Project {
String name;
String path;
public Project(String name, String path) {
super();
this.name = name;
this.path = path;
}
public String getName() {
return name;
}
public String getPath() {
return path;
}
}
}
附上源代码--->源码下载