Grade项目转为MAVEN项目(粗略)
暂时支持粗略版的jar和pom
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Gradle项目转为MAVEN项目
* @author lry
*/
public class Gradle2MAVEN {
public static void main(String[] args) {
new Gradle2MAVEN().convert("com.test.web", "/Users/admin/git/works/test-webapp",Packaging.war);
}
public String BUILD_PATH;
public static final String POM_FILE = "pom.xml";
public static final String CLASSPATH_FILE = ".classpath";
public static final String BUILD_NAME = "build.gradle";
public static final String DEPENDENCIES = "dependencies";
public static final String DEPENDENCIES_XML = "<dependencies>%s</dependencies>";
public static final String VERSION = "1.0.0-SNAPSHOT";
public static final String PROJECT="project";
public static final String[] TYPE={"compile","provided","providedCompile"};
public static final String WAR_CLASSPATH="<attribute name=\"org.eclipse.jst.component.dependency\" value=\"/WEB-INF/lib\"/>";
public enum Packaging{
jar,war,pom
}
/**
* 项目路径(包括项目名称)
* @param groupId
* @param path
*/
public void convert(String groupId, String path,Packaging packaging) {
String pomData = "";
if (!path.endsWith(File.separator)) {
path += File.separator;
}
for (String finder:finders) {
chek(path+finder);
}
// 获取artifactId
String artifactId = path.substring(0, path.lastIndexOf(File.separator));
artifactId = artifactId.substring(artifactId.lastIndexOf(File.separator) + 1);
try {
LinkedHashMap<String, Boolean> map = readDir(path);
for (Map.Entry<String, Boolean> entry : map.entrySet()) {
if (!entry.getValue()) {// 文件
if (entry.getKey().equals(BUILD_NAME)) {
BUILD_PATH = path + entry.getKey();
}
}
}
String dependencys = "";
List<String> BUILD_DATA = readStringContent(BUILD_PATH, "$$$");
boolean startFlag = false;
for (String line : BUILD_DATA) {
if (line.contains(DEPENDENCIES)) {
startFlag = true;
}
if (startFlag) {
if (line.contains("}")) {
startFlag = false;
}
//排除
line = line.replace(" ", "");
boolean flag=false;
for (String type:TYPE) {
if(line.contains(type)){
flag=true;
break;
}
}
if(!flag){
continue;
}
int begin = line.indexOf("'");
int end = line.lastIndexOf("'");
if (begin != -1 && end != -1 && begin != end) {
String scope = line.substring(0, line.indexOf(" "));
String[] array =null;
if(line.substring(0,begin).contains(PROJECT)){//模块组件
line = line.substring(begin + 1, end).replace(":", "").replace(" ", "");
array=new String[]{groupId,line,VERSION};
}else{
line = line.substring(begin + 1, end);
array = line.split(":");
}
dependencys += String.format(DEPENDENCY_TEMP, array[0], array[1], array[2], scope);
}
}
}
dependencys = String.format(DEPENDENCIES_XML, dependencys);
pomData = String.format(POM, groupId, artifactId,VERSION,packaging.toString(), dependencys);
saveFile(path+POM_FILE, pomData);
String classpath=CLASSPATH_DATA;
if(packaging==Packaging.jar){
classpath=String.format(classpath, "");
}else if(packaging==Packaging.war){
classpath=String.format(classpath, WAR_CLASSPATH);
}
//创建.classpath文件
saveFile(path+CLASSPATH_FILE, classpath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建和保存文件
* @param fileName
* @param content
* @throws IOException
*/
private void saveFile(String fileName, String content) throws IOException {
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file, true);
OutputStreamWriter out = new OutputStreamWriter(fos, "utf-8");
@SuppressWarnings("resource")
BufferedWriter bw = new BufferedWriter(out);
bw.write(content);
bw.write("\r\n");
bw.flush();
}
/**
* 检查文件夹是否存在,不存在则创建
* @param finder
*/
private void chek(String finder) {
File file = new File(finder);
if (!file.exists()) {
file.mkdir();
}
}
/**
* 获取目录下的文件和文件夹列表
*
* @param dir 源目录
* @return LinkedHashMap<String,Boolean> true表示目录,false表示文件
* @throws Exception
*/
public static LinkedHashMap<String, Boolean> readDir(String dir) throws Exception {
File d = new File(dir);
if (!d.isDirectory()){
throw new Exception("\"" + dir + "\"" + "不是一个目录");
}
String[] array = d.list();
if (array == null){
return null;
}
LinkedHashMap<String, Boolean> map = new LinkedHashMap<String, Boolean>();
for (int i = 0; i < array.length; i++) {
map.put(array[i], new File(dir + File.separatorChar + array[i]).isDirectory() == true ? true : false);
}
return map;
}
/**
* 获取文件内容
*
* @param src 源文件
* @return String[] 文件内容数组,每行占一个数组空间
* @throws IOException
*/
public static List<String> readStringContent(String src,String separator) throws IOException {
List<String> list=new ArrayList<String>();
FileReader reader = new FileReader(src);
BufferedReader br = new BufferedReader(reader);
String line;
while ((line = br.readLine()) != null) {
list.add(line+separator);
}
if(reader!=null){
reader.close();
}
if(br!=null){
br.close();
}
return list;
}
public static final String DEPENDENCY_TEMP = "<dependency>"
+ "<groupId>%s</groupId>"
+ "<artifactId>%s</artifactId>"
+ "<version>%s</version>"
+ "<scope>%s</scope>"
+ "</dependency>";
public String POM = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" "
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ "xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 "
+ "http://maven.apache.org/xsd/maven-4.0.0.xsd\">"
+ "<modelVersion>4.0.0</modelVersion>"
+ "<groupId>%s</groupId>"
+ "<artifactId>%s</artifactId>"
+ "<version>%s</version>"
+ "<packaging>%s</packaging>"
+ "%s"
+ "</project>";
public static final String[] finders={"src","src/main","src/test","src/main/java","src/main/resources","src/test/java","src/test/resources"};
public static final String CLASSPATH_DATA="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<classpath>"
+ "<classpathentry kind=\"src\" output=\"target/classes\" path=\"src/main/java\">"
+ "<attributes>"
+ "<attribute name=\"optional\" value=\"true\"/>"
+ "<attribute name=\"maven.pomderived\" value=\"true\"/>"
+ "</attributes>"
+ "</classpathentry>"
+ "<classpathentry excluding=\"**\" kind=\"src\" output=\"target/classes\" path=\"src/main/resources\">"
+ "<attributes>"
+ "<attribute name=\"maven.pomderived\" value=\"true\"/>"
+ "</attributes>"
+ "</classpathentry>"
+ "<classpathentry kind=\"src\" output=\"target/test-classes\" path=\"src/test/java\">"
+ "<attributes><attribute name=\"optional\" value=\"true\"/>"
+ "<attribute name=\"maven.pomderived\" value=\"true\"/></attributes>"
+ "</classpathentry>"
+ "<classpathentry excluding=\"**\" kind=\"src\" output=\"target/test-classes\" path=\"src/test/resources\">"
+ "<attributes>"
+ "<attribute name=\"maven.pomderived\" value=\"true\"/>"
+ "</attributes>"
+ "</classpathentry>"
+ "<classpathentry kind=\"con\" path=\"org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5\">"
+ "<attributes>"
+ "<attribute name=\"maven.pomderived\" value=\"true\"/>"
+ "</attributes>"
+ "</classpathentry>"
+ "<classpathentry kind=\"con\" path=\"org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER\">"
+ "<attributes>"
+ "<attribute name=\"maven.pomderived\" value=\"true\"/>"
+ "%s"
+ "</attributes>"
+ "</classpathentry>"
+ "<classpathentry kind=\"output\" path=\"target/classes\"/>"
+ "</classpath>";
}
完毕