我们在项目中使用了大量遗留package.html
文件,我们希望将它们转换为package-info.java
个文件。手动执行此操作不是一种选择(文件太多)。是否有一种自动化的好方法?
我们想要转换它们有几个原因:
来自javadoc规范:此文件是JDK 5.0中的新文件,比package.html更受欢迎。
不要在同一代码库中混合使用这两种类型的文件
为了避免Intellij / Eclipse构建将这些* .html文件放在我们的类目录中(并且可能在发布二进制文件中),所以它们的行为就像我们的其他普通html资源一样。
答案 0 :(得分:7)
如果您没有运行Windows,则可能需要更改目录分隔符。此外,转换是一个黑客攻击,但它应该工作。出于好奇,您手册中有多少包不是一种选择?
public class Converter {
public static void main(String[] args) {
File rootDir = new File(".");
renamePackageToPackageInfo(rootDir);
}
private static void renamePackageToPackageInfo(File dir) {
File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return "package.html".equals(name);
}
});
for (File file : files) {
convertFile(file);
}
// now recursively rename all the child directories.
File[] dirs = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
for (File subdir : dirs) {
renamePackageToPackageInfo(subdir);
}
}
private static void convertFile(File html) {
// determine the FQN package name
String fqpn = getPackageName(html);
// check if package-info.java already exists
File packageInfo = new File(html.getParent(), "package-info.java");
if (packageInfo.exists()) {
System.out.println("package-info.java already exists for package: "+fqpn);
return;
}
// create the i/o streams, and start pumping the data
try {
PrintWriter out = new PrintWriter(packageInfo);
BufferedReader in = new BufferedReader(new FileReader(html));
out.println("/**");
// skip over the headers
while (true) {
String line = in.readLine();
if (line.equalsIgnoreCase("<BODY>"))
break;
}
// now pump the file into the package-info.java file
while (true) {
String line = in.readLine();
if (line.equalsIgnoreCase("</BODY>"))
break;
out.println(" * " + line);
}
out.println("*/");
out.println("package "+fqpn+";");
out.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// queue the package.html file for deletion
//html.deleteOnExit();
}
private static String getPackageName(File file) {
StringBuilder path = new StringBuilder(file.getParent());
// trim the first two characters (./ or .\)
path.delete(0, 2);
// then convert all separators into . (HACK: should use directory separator property)
return path.toString().replaceAll("\\\\", ".");
}
}
答案 1 :(得分:0)
IntelliJ的家伙已an intention to do this for all files。它已经解决,可能会在下一个IntelliJ版本中发布。
答案 2 :(得分:0)
要在IDEA中以批处理模式执行此操作:
package.html
文件sed -i "/Put @see and @since/d" `find . -name "package-info.java"`
)