如何自动将所有 javadoc package.html 文件转换为 package-info.java 文件?

发布于 2024-10-14 14:58:59 字数 394 浏览 5 评论 0原文

我们在项目中使用了很多旧的 package.html 文件,我们希望将它们转换为 package-info.java 文件。手动执行此操作不是一个选项(文件太多)。有没有一个好的方法可以实现自动化?

我们想要转换它们有几个原因:

  • 来自 javadoc 规范:此文件是 JDK 5.0 中的新文件,并且优先于 package.html。

  • 不要在同一代码库中混合两种类型的文件

  • 为了避免 Intellij/Eclipse 构建,请将这些 *.html 文件放在我们的类目录中(也可能放在发布的二进制 jar 中),这样它们的行为就像我们其他普通的 html 资源一样。

We use a lot of legacy package.html files in our project and we want to convert them to package-info.java files. Doing that manually isn't an option (way too many files). Is there a good way to automate that?

We want to convert them for a couple of reasons:

  • From the javadoc specs: This file is new in JDK 5.0, and is preferred over package.html.

  • To not mix both types of files in the same codebase

  • To avoid that Intellij/Eclipse builds put those *.html files in our classes dirs (and possibly in a release binary jars) so they behave like our other normal html resources.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-10-21 14:58:59

如果您没有运行 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("\\\\", ".");
    }

}

You may need to change the directory separator if you're not running windows. Also, the conversion is a bit of a hack, but it should work. Out of curiosity, how many packages do you have that manual isn't an option?

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("\\\\", ".");
    }

}
情栀口红 2024-10-21 14:58:59

IntelliJ 人员打算对所有文件执行此操作。该问题已得到解决,可能会在下一个 IntelliJ 版本中发布。

The IntelliJ guys have made an intention to do this for all files. It's been resolved and will probably be released in the next IntelliJ release.

诠释孤独 2024-10-21 14:58:59

要在 IDEA 中以批处理模式执行此操作:

  • 在设置中,激活检查小工具“'package.html' 可能会转换为 'package-info.java' 检查”
  • 打开一个 package.html 文件
  • 您会看到横幅将检查固定在文件顶部
  • 单击横幅右侧的设置图标
  • 选择“运行检查”>> “整个项目”
  • 点击“转换为package-info.java”>>确定
  • (可选)删除不适当的行(sed -i "/Put @see and @since/d" `find . -name "package-info.java"`

To do this in batch mode in IDEA:

  • In settings, activate the inspection gadget "'package.html' may be converted to 'package-info.java' inspection"
  • Open a package.html file
  • You see a banner fix the inspection on top the file
  • Click on the settings icon at the right on the banner
  • Select "Run inspection on" >> "Whole project"
  • Click on "Convert to package-info.java" >> OK
  • Optionally remove the inappropriate lines (sed -i "/Put @see and @since/d" `find . -name "package-info.java"`)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文