打印整个java项目的源代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1921178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:26:38  来源:igfitidea点击:

Print the sourcecode of a whole java project

javaeclipseprinting

提问by r3zn1k

I have to print the whole sourcecode of a java-project. The final version should look like: Eclipse: File -> Print. But with this function you can only print one file at once.

我必须打印一个 java 项目的整个源代码。最终版本应如下所示:Eclipse: File -> Print。但是使用此功能您一次只能打印一个文件。

Is there a way to print (or create a pdf/rtf of) the whole project (all *.java, *.xml, ... files) with one command?

有没有办法用一个命令打印(或创建一个 pdf/rtf)整个项目(所有 *.java、*.xml、...文件)?

Im using eclipse galileo on windows xp sp3

我在 windows xp sp3 上使用 eclipse galileo



EDIT:For each class/file the page should (more or less) look like this:

编辑:对于每个类/文件,页面应该(或多或少)如下所示:

C:\..\..\..\LibraryExtractor.java

C:\..\..\..\LibraryExtractor.java

1 package utils.libraries;
2
3 import java.io.File;
9
10 /**
11 * @
12 * @
13 * @
14 */
15 public class LibraryExtractor {
16
17 /**
18 * 
19 * 
20 *
21 * 
22 * 
23 *
24 *
25 */
26 public static void extranctLibrary(String library, File targetFile) throws
IOException, URISyntaxException {
27 targetFile.getParentFile().mkdirs();
28 if (!targetFile.exists())
29 targetFile.createNewFile();
30
31 ClassLoader classLoader = LibraryExtractor.class.getClassLoader();
32 InputStream in = classLoader.getResourceAsStream(library);
33 OutputStream out = new FileOutputStream(targetFile);
34
35 byte[] buf = new byte[1024];
36 int len;
37
38 while ((len = in.read(buf)) > 0)
39 out.write(buf, 0, len);
40
41 in.close();
42 out.close();
43 }
44 }
45


SOLUTION:

解决方案:

  1. enscript(with Cygwin)

  2. Java2HtmlEclipse-Plugin (only works with Europa)

  1. enscript(使用 Cygwin)

  2. Java2HtmlEclipse 插件(仅适用于 Europa)

采纳答案by kdgregory

If you don't mind installing Cygwin, or running on Linux, the following command will do what you want:

如果您不介意安装Cygwin或在 Linux 上运行,以下命令将执行您想要的操作:

enscript -r -2 --file-align=2 --highlight --line-numbers -o - `find . -name '*.java'` | ps2pdf - files.pdf

enscriptis a program for converting text files to a variety of output formats; PostScript is the default, but you can also produce HTML, RTF, and a few others. The -roption says to print in landscape, -2is two columns per page (save trees), --file-align=2says that each new file should start on its own physical page, --highlightturns on language-specific syntax highlighting (it will try to figure out the language, or you can specify "java"), --line-numbersshould be obvious, and -o -sends the output to standard-out (where it's piped to ps2pdf).

enscript是一个将文本文件转换为多种输出格式的程序;PostScript 是默认设置,但您也可以生成 HTML、RTF 和其他一些内容。该-r选项说横向打印,-2每页两列(保存树),--file-align=2说每个新文件应该从自己的物理页面开始,--highlight打开特定于语言的语法突出显示(它会尝试找出语言,或者你可以指定“java”),--line-numbers应该是显而易见的,-o -并将输出发送到标准输出(它通过管道传输到ps2pdf)。

findgenerates the list of files; here I'm telling it to find all Java files under in the current directory. The output is passed as arguments to enscript; for "50-100 files" you should be OK, but you might need to read about xargs. You could get rid of the -nameargument to generate a list of all files, or add multiple -namearguments to add more file types to the list; I wouldn't go with the "all files" approach, because then you'll get source-control files.

find生成文件列表;在这里,我告诉它在当前目录下查找所有 Java 文件。输出作为参数传递给enscript;对于“50-100 个文件”,您应该没问题,但您可能需要阅读有关xargs 的信息。您可以去掉-name参数以生成所有文件的列表,或者添加多个-name参数以将更多文件类型添加到列表中;我不会采用“所有文件”的方法,因为那样你会得到源代码控制文件。

ps2pdftakes the PostScript output from enscriptand converts it to PDF, which you can print.

ps2pdfenscript获取 PostScript 输出并将其转换为可以打印的 PDF。

回答by jitter

I don't think you can do this within Eclipse (of course you could write a plugin which does this).

我认为你不能在 Eclipse 中做到这一点(当然你可以编写一个插件来做到这一点)。

If you use Ant as your build tool you could use the concattask and then print the resulting file

如果您使用 Ant 作为构建工具,则可以使用该concat任务,然后打印生成的文件

<concat destfile="${concat.src.dir}/concat.txt" force="no">
    <filelist dir="${src.dir}" includes="**/*.java **/*.xml" />
</concat>

回答by JuanZe

If you can afford spending $50 buy Ultraedit, open all files and print it...

如果您能负担得起花费 50 美元购买 Ultraedit,请打开所有文件并打印...

Ultraedit featuresabout printing includes:

有关打印的Ultraedit 功能包括:

  • Print preview
  • Print line numbers
  • Print command doesn't print hidden lines
  • Headers/footers (with alignment commands), margins and page breaks
  • Printing of syntax highlighting in color
  • Print 2 pages on one sheet in landscape or portrait modes
  • Separate font selection for display and printer (supports all fonts installed including True Type fonts)
  • Print all open files
  • 打印预览
  • 打印行号
  • 打印命令不打印隐藏线
  • 页眉/页脚(带有对齐命令)、边距和分页符
  • 打印语法高亮彩色
  • 以横向或纵向模式在一张纸上打印 2 页
  • 显示器和打印机的单独字体选择(支持所有安装的字体,包括 True Type 字体)
  • 打印所有打开的文件

回答by Xavier Combelle

I would use pygments with linenos enabled as explained in http://pygments.org/docs/formatters/

我会使用启用了 linenos 的 pygments,如http://pygments.org/docs/formatters/ 中所述

回答by fortran

An option that looks fancy is to use vim in batch mode to generate a bunch of colorized HTML files and then print by dragging all of them to the printer (I know that can be done, some time ago a colleague printed the whole J2SE API, and I hope she didn't it page by page xD).

一个看起来很花哨的选项是在批处理模式下使用 vim 生成一堆彩色 HTML 文件,然后通过将它们全部拖到打印机进行打印(我知道可以做到,前段时间一位同事打印了整个 J2SE API,我希望她没有逐页 xD)。

find -name "*.java" -exec vim '+set nu' +TOhtml +wq +q '{}' \;

回答by Matthew Farwell

If you want the formatting exactly as it is in Eclipse, then you will probably have to print from Eclipse. You'll spend more time trying to duplicate the print formatting that you have in Eclipse with another method.

如果您希望格式与 Eclipse 中的格式完全相同,那么您可能必须从 Eclipse 打印。您将花费更多时间尝试使用另一种方法复制 Eclipse 中的打印格式。

Another important point: If you are using folding in the text editors in Eclipse, then the folded lines will not be displayed in the printed version.

另一个重点:如果您在 Eclipse 的文本编辑器中使用折叠,则折叠线将不会显示在打印版本中。

If you really really have to furnish the source code as trees, then I would suggest that you try and persuade your clients that colour and syntax highlighting are not important, and then format everything in Eclipse, and print from elsewhere. There are suggestions for the line numbers etc in other answers.

如果您真的必须将源代码提供为树,那么我建议您尝试说服您的客户颜色和语法突出显示并不重要,然后在 Eclipse 中格式化所有内容,并从其他地方打印。在其他答案中有关于行号等的建议。

回答by Gregory Pakosz

I've used Java2Htmlfrom Eclipsein the past. See whether it suits your needs.

我用Java2HtmlEclipse的过去。看看它是否适合您的需求。

回答by Stuart Clark

http://sourceforge.net/projects/javasrc2pdf/?source=typ_redirect

http://sourceforge.net/projects/javasrc2pdf/?source=typ_redirect

This creates all the PDFs and with reasonably good syntax highlighting however they are all in separate files. But there are plenty of ways to combine pdfs out there

这将创建所有 PDF 并具有相当好的语法突出显示,但是它们都在单独的文件中。但是有很多方法可以组合pdf