以下是用来将myeclipse(或者其他没有加maven配置的工程)的工程文件装换到最新的m2eclipse插件所能识别的格式,
简单来说就是修改.project和.classpath
使用方法:
1. 在workspace任意层级下(当然啦,这个workspace的名字要包含"
workspace"字样),运行: java
EclipseM2Patch
2. 在任意目录下指定workspace目录,运行: java
EclipseM2Patch /java/workspace
运行后将会对workspace下的所有需要patch工程文件进行patch,如果不满意还可以回滚,满意就删除所有备份文件。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class EclipseM2Patch {
private static String fileToString(File f) throws Exception {
byte[] bytes = fileToBytes(f);
if (bytes.length == 0)
return null;
return new String(bytes, "iso-8859-1");
}
private static byte[] fileToBytes(File f) throws Exception {
if (!f.exists())
return null;
FileInputStream fis = null;
byte[] all = new byte[0];
try {
byte[] buf = new byte[1024];
fis = new FileInputStream(f);
int read = -1;
while ((read = fis.read(buf)) != -1) {
byte[] tmp = new byte[all.length + read];
if (all.length > 0)
System.arraycopy(all, 0, tmp, 0, all.length);
System.arraycopy(buf, 0, tmp, all.length, read);
all = tmp;
}
} finally {
if (fis != null)
fis.close();
}
return all;
}
private static void write(File f, String content) throws Exception {
FileOutputStream fos = new FileOutputStream(f);
try {
fos.write(content.getBytes("iso-8859-1"));
} finally {
fos.close();
}
}
private static File backup(File f) throws Exception {
byte[] bytes = fileToBytes(f);
if (bytes.length > 0) {
File backupfile = new File(f.getAbsoluteFile() + ".patchbackup");
FileOutputStream fos = new FileOutputStream(backupfile);
try {
fos.write(bytes);
} finally {
fos.close();
}
return backupfile;
}
return null;
}
public static void main(String[] args) throws Exception {
String workspacepath = args.length == 0 ? null : args[0];
if (workspacepath == null) {
File file = new File(".");
String p = file.getAbsolutePath().replaceAll("(?i)(workspace[^\\\\/]+).*", "$1");
if (p.length() != file.getAbsolutePath().length()) {
workspacepath = p;
}
}
if (workspacepath == null) {
throw new IllegalAccessException();
}
ArrayList<File> backups = new ArrayList<File>();
File workspace = new File(workspacepath);
for (File projectBaseDir : workspace.listFiles()) {
if (projectBaseDir.isDirectory()
&& projectBaseDir.getName().matches("^\\p{Alpha}[\\p{Alpha}_-]+$")) {
File _project = new File(projectBaseDir, ".project");
File _classpath = new File(projectBaseDir, ".classpath");
if (_project.exists()) {
String content = fileToString(_project);
String addToContent = "<nature>org.eclipse.m2e.core.maven2Nature</nature>";
if (!content.contains(addToContent)) {
File backupfile = backup(_project);
if (backupfile != null)
backups.add(backupfile);
content = content.replaceAll("(?im)(\\s+)(?=</natures>)", "$1\t" + addToContent
+ "$1");
write(_project, content);
System.out.println("file:" + _project + " - patched");
} else {
File backupfile = new File(_project.getAbsoluteFile() + ".patchbackup");
if (backupfile.exists()) {
backups.add(backupfile);
}
}
}
if (_classpath.exists()) {
String content = fileToString(_classpath);
String addToContent = "<classpathentry kind=\"con\" path=\"org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER\"/>";
if (!content.contains("org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER")) {
File backupfile = backup(_classpath);
if (backupfile != null)
backups.add(backupfile);
content = content.replaceAll("(?im)(\\s+)(?=</classpath>)", "$1\t" + addToContent
+ "$1");
write(_classpath, content);
System.out.println("file:" + _classpath + " - patched");
} else {
File backupfile = new File(_classpath.getAbsoluteFile() + ".patchbackup");
if (backupfile.exists()) {
backups.add(backupfile);
}
}
}
}
}
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
String line = null;
if (backups.size() > 0) {
System.out.println(">Delete all backups?(Y-yes/N-no/R-rollback)");
line = rd.readLine();
for (File f : backups) {
if ("y".equalsIgnoreCase(line)) {
boolean delete = f.delete();
System.out.println("Delete file:" + f + " - " + (delete ? "ok" : "fail"));
} else if ("r".equalsIgnoreCase(line)) {
File file = new File(f.getAbsolutePath().replace(".patchbackup", ""));
boolean result = file.delete();
if (result)
result = f.renameTo(file);
System.out.println("Rollback file:" + f + " - " + (result ? "ok" : "fail"));
}
}
}
System.out.println(">Press any key to continue...");
line = rd.readLine();
rd.close();
}
}