简体   繁体   中英

How do I automatically convert all javadoc package.html files into package-info.java files?

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.

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

}

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.

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"` )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM