首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

myeclipse转eclipse jee的工程文件补丁(主若是maven补丁)

2013-11-20 
myeclipse转eclipse jee的工程文件补丁(主要是maven补丁)以下是用来将myeclipse(或者其他没有加maven配置

myeclipse转eclipse jee的工程文件补丁(主要是maven补丁)
以下是用来将myeclipse(或者其他没有加maven配置的工程)的工程文件装换到最新的m2eclipse插件所能识别的格式,简单来说就是修改.project和.classpath使用方法:1. 在workspace任意层级下(当然啦,这个workspace的名字要包含"workspace"字样),运行: java?EclipseM2Patch 2. 在任意目录下指定workspace目录,运行:?java?EclipseM2Patch /java/workspace运行后将会对workspace下的所有需要patch工程文件进行patch,如果不满意还可以回滚,满意就删除所有备份文件。?Java代码

  1. import?java.io.BufferedReader;??
  2. import?java.io.File;??
  3. import?java.io.FileInputStream;??
  4. import?java.io.FileOutputStream;??
  5. import?java.io.InputStreamReader;??
  6. import?java.util.ArrayList;??
  7. ??
  8. public?class?EclipseM2Patch?{??
  9. ??
  10. ????private?static?String?fileToString(File?f)?throws?Exception?{??
  11. ????????byte[]?bytes?=?fileToBytes(f);??
  12. ????????if?(bytes.length?==?0)??
  13. ????????????return?null;??
  14. ????????return?new?String(bytes,?"iso-8859-1");??
  15. ????}??
  16. ??
  17. ????private?static?byte[]?fileToBytes(File?f)?throws?Exception?{??
  18. ????????if?(!f.exists())??
  19. ????????????return?null;??
  20. ????????FileInputStream?fis?=?null;??
  21. ????????byte[]?all?=?new?byte[0];??
  22. ????????try?{??
  23. ????????????byte[]?buf?=?new?byte[1024];??
  24. ????????????fis?=?new?FileInputStream(f);??
  25. ????????????int?read?=?-1;??
  26. ????????????while?((read?=?fis.read(buf))?!=?-1)?{??
  27. ????????????????byte[]?tmp?=?new?byte[all.length?+?read];??
  28. ????????????????if?(all.length?>?0)??
  29. ????????????????????System.arraycopy(all,?0,?tmp,?0,?all.length);??
  30. ????????????????System.arraycopy(buf,?0,?tmp,?all.length,?read);??
  31. ????????????????all?=?tmp;??
  32. ????????????}??
  33. ????????}?finally?{??
  34. ????????????if?(fis?!=?null)??
  35. ????????????????fis.close();??
  36. ????????}??
  37. ????????return?all;??
  38. ????}??
  39. ??
  40. ????private?static?void?write(File?f,?String?content)?throws?Exception?{??
  41. ????????FileOutputStream?fos?=?new?FileOutputStream(f);??
  42. ????????try?{??
  43. ????????????fos.write(content.getBytes("iso-8859-1"));??
  44. ????????}?finally?{??
  45. ????????????fos.close();??
  46. ????????}??
  47. ????}??
  48. ??
  49. ????private?static?File?backup(File?f)?throws?Exception?{??
  50. ????????byte[]?bytes?=?fileToBytes(f);??
  51. ????????if?(bytes.length?>?0)?{??
  52. ????????????File?backupfile?=?new?File(f.getAbsoluteFile()?+?".patchbackup");??
  53. ????????????FileOutputStream?fos?=?new?FileOutputStream(backupfile);??
  54. ????????????try?{??
  55. ????????????????fos.write(bytes);??
  56. ????????????}?finally?{??
  57. ????????????????fos.close();??
  58. ????????????}??
  59. ????????????return?backupfile;??
  60. ????????}??
  61. ????????return?null;??
  62. ????}??
  63. ??
  64. ????public?static?void?main(String[]?args)?throws?Exception?{??
  65. ????????String?workspacepath?=?args.length?==?0???null?:?args[0];??
  66. ????????if?(workspacepath?==?null)?{??
  67. ????????????File?file?=?new?File(".");??
  68. ????????????String?p?=?file.getAbsolutePath().replaceAll("(?i)(workspace[^\\\\/]+).*",?"$1");??
  69. ????????????if?(p.length()?!=?file.getAbsolutePath().length())?{??
  70. ????????????????workspacepath?=?p;??
  71. ????????????}??
  72. ????????}??
  73. ????????if?(workspacepath?==?null)?{??
  74. ????????????throw?new?IllegalAccessException();??
  75. ????????}??
  76. ????????ArrayList<File>?backups?=?new?ArrayList<File>();??
  77. ????????File?workspace?=?new?File(workspacepath);??
  78. ????????for?(File?projectBaseDir?:?workspace.listFiles())?{??
  79. ????????????if?(projectBaseDir.isDirectory()??
  80. ????????????????????&&?projectBaseDir.getName().matches("^\\p{Alpha}[\\p{Alpha}_-]+$"))?{??
  81. ????????????????File?_project?=?new?File(projectBaseDir,?".project");??
  82. ????????????????File?_classpath?=?new?File(projectBaseDir,?".classpath");??
  83. ????????????????if?(_project.exists())?{??
  84. ????????????????????String?content?=?fileToString(_project);??
  85. ????????????????????String?addToContent?=?"<nature>org.eclipse.m2e.core.maven2Nature</nature>";??
  86. ????????????????????if?(!content.contains(addToContent))?{??
  87. ????????????????????????File?backupfile?=?backup(_project);??
  88. ????????????????????????if?(backupfile?!=?null)??
  89. ????????????????????????????backups.add(backupfile);??
  90. ????????????????????????content?=?content.replaceAll("(?im)(\\s+)(?=</natures>)",?"$1\t"?+?addToContent??
  91. ????????????????????????????????+?"$1");??
  92. ????????????????????????write(_project,?content);??
  93. ????????????????????????System.out.println("file:"?+?_project?+?"?-?patched");??
  94. ????????????????????}?else?{??
  95. ????????????????????????File?backupfile?=?new?File(_project.getAbsoluteFile()?+?".patchbackup");??
  96. ????????????????????????if?(backupfile.exists())?{??
  97. ????????????????????????????backups.add(backupfile);??
  98. ????????????????????????}??
  99. ????????????????????}??
  100. ????????????????}??
  101. ????????????????if?(_classpath.exists())?{??
  102. ????????????????????String?content?=?fileToString(_classpath);??
  103. ????????????????????String?addToContent?=?"<classpathentry?kind="con"?path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>";??
  104. ????????????????????if?(!content.contains("org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"))?{??
  105. ????????????????????????File?backupfile?=?backup(_classpath);??
  106. ????????????????????????if?(backupfile?!=?null)??
  107. ????????????????????????????backups.add(backupfile);??
  108. ????????????????????????content?=?content.replaceAll("(?im)(\\s+)(?=</classpath>)",?"$1\t"?+?addToContent??
  109. ????????????????????????????????+?"$1");??
  110. ????????????????????????write(_classpath,?content);??
  111. ????????????????????????System.out.println("file:"?+?_classpath?+?"?-?patched");??
  112. ????????????????????}?else?{??
  113. ????????????????????????File?backupfile?=?new?File(_classpath.getAbsoluteFile()?+?".patchbackup");??
  114. ????????????????????????if?(backupfile.exists())?{??
  115. ????????????????????????????backups.add(backupfile);??
  116. ????????????????????????}??
  117. ????????????????????}??
  118. ????????????????}??
  119. ????????????}??
  120. ????????}??
  121. ????????BufferedReader?rd?=?new?BufferedReader(new?InputStreamReader(System.in));??
  122. ????????String?line?=?null;??
  123. ????????if?(backups.size()?>?0)?{??
  124. ????????????System.out.println(">Delete?all?backups?(Y-yes/N-no/R-rollback)");??
  125. ????????????line?=?rd.readLine();??
  126. ????????????for?(File?f?:?backups)?{??
  127. ????????????????if?("y".equalsIgnoreCase(line))?{??
  128. ????????????????????boolean?delete?=?f.delete();??
  129. ????????????????????System.out.println("Delete?file:"?+?f?+?"?-?"?+?(delete???"ok"?:?"fail"));??
  130. ????????????????}?else?if?("r".equalsIgnoreCase(line))?{??
  131. ????????????????????File?file?=?new?File(f.getAbsolutePath().replace(".patchbackup",?""));??
  132. ????????????????????boolean?result?=?file.delete();??
  133. ????????????????????if?(result)??
  134. ????????????????????????result?=?f.renameTo(file);??
  135. ????????????????????System.out.println("Rollback?file:"?+?f?+?"?-?"?+?(result???"ok"?:?"fail"));??
  136. ????????????????}??
  137. ????????????}??
  138. ????????}??
  139. ????????System.out.println(">Press?any?key?to?continue...");??
  140. ????????line?=?rd.readLine();??
  141. ????????rd.close();??
  142. ????}??
  143. }??

热点排行