java 只用 ant 编译源代码树的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/203180/
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-10-29 11:24:46  来源:igfitidea点击:

compiling only part of the source tree with ant

javaantjarjavac

提问by Davide

Say I have my sources in my src/ tree (and possibly in my test/ tree). Say I would like to compile only partof that tree. The reasons why I might want to do that are various. Just as an example, I might want to create the smallest possible jar (without including certain classes), or I might want the fastest compile time for what I am compiling. I absolutely want to compile all the dependencies, though!

假设我的源代码在我的 src/ 树中(也可能在我的 test/ 树中)。假设我只想编译那棵树的一部分。我可能想要这样做的原因是多种多样的。举个例子,我可能想要创建尽可能小的 jar(不包括某些类),或者我可能想要我正在编译的内容最快的编译时间。不过,我绝对想编译所有依赖项!

This can be easily achieved from the command line with:

这可以通过命令行轻松实现:

javac -d build/ -cp whatever -sourcepath src src/path/to/MyClass.java

Now, how can you do that with ant? The javac ant task compiles everything:

现在,你怎么能用蚂蚁做到这一点?javac ant任务编译所有内容

The source and destination directory will be recursively scanned for Java source files to compile.

将递归扫描源目录和目标目录以查找要编译的 Java 源文件。

One can use the excludesand includesparameters, but they are problematic for this purpose. In fact, it seems that one has to explicitly setup all the includes(not automatic dependency lookup), and even worstthat excludes has priority on includes:

可以使用excludesincludes参数,但是为了这个目的它们是有问题的。事实上,它似乎是一个有明确设置所有的includes(不是自动的依赖查找),并且即使最差的是排除优先于包括

When both inclusion and exclusion are used, only files/directories that match at least one of the include patterns and don't match anyof the exclude patterns are used.

当同时使用包含和排除时,仅使用至少匹配一个包含模式但不匹配任何排除模式的文件/目录。

Thus, you cannot use

因此,您不能使用

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"
            excludes="**/*.java" includes="src/path/to/MyClass.java" />  

Because it will not compile anything :-(

因为它不会编译任何东西:-(

Is there any way of achieving that simple command line javacwith ant?

有没有办法javac用 ant实现那个简单的命令行?



EDITED: Thank you for your answer, Sadie, I'm accepting it, because it does work in the way I was wondering in this question. But I have a couple of comments (too long to be in the comment field of your answer):

编辑:谢谢你的回答,Sadie,我接受它,因为它确实以我在这个问题中想知道的方式工作。但是我有一些评论(太长而无法在您的答案的评论字段中):

1) I did read the documentation (see links above), but it's unclear that with just includesyou are actually also excluding everything else

1)我确实阅读了文档(请参阅上面的链接),但目前还不清楚只有includes您实际上还排除了其他所有内容

2) When you just includesant logs something like

2)当你只是includes蚂蚁记录类似

[javac] Compiling 1 source file to /my/path/to/build

even if the dependencies make it compiling (much) more than just one source file.

即使依赖关系使它编译(很多)不仅仅是一个源文件。

回答by Marcus Downing

Why are you excluding as well as including? If you have at least one include, then files are only compiled if they're explicitly included. So this should work:

你为什么既要排除又要包括?如果您至少有一个包含,则只有在明确包含文件时才会编译文件。所以这应该有效:

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"
        includes="src/path/to/MyClass.java" />

Or more flexibly:

或者更灵活:

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath">
    <include name="src/path/to/MyClass.java"/>
    <include name="src/path/to/AnotherClass.java"/>
</javac>

To include only certain packages or classes in a jar, use a fileset attribute

要在 jar 中仅包含某些包或类,请使用文件集属性

<jar jarfile="${outlib}/something.jar">
    <fileset dir="${build.dir}">
        <include name='src/path/to/classes' />
    </fileset>
</jar>

Again, you can use multiple includes to combine separate packages. Experiment with includes and read the documentationand you're sure to find the answer you need.

同样,您可以使用多个包含来组合单独的包。尝试包含并阅读文档,您一定会找到所需的答案。

回答by Tor P

Old question, but I was struggling with the same problem and found a a more elegant solution. So here it is, for future reference:

老问题,但我正在努力解决同样的问题,并找到了更优雅的解决方案。所以这里是,以供将来参考:

According to the ant docs the <javac>element is an implicit <fileset>and as such can take Selectorslike <filename name="**/MyClass.java"/>, so this would only compile MyClass.java:

根据 ant 文档,<javac>元素是隐式的<fileset>,因此可以采用Selectorslike <filename name="**/MyClass.java"/>,所以这只会编译 MyClass.java:

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath">
  <filename name="**/path/to/MyClass.java"/>
</javac>

回答by Draemon

Actually, ant only checkseverything, if you run a compile twice in a row you will notice the second is much quicker. Actually, it can be easily persuaded to miss things.

实际上,ant 只检查所有内容,如果您连续运行两次编译,您会发现第二次编译要快得多。实际上,很容易被说服错过一些事情。

If you don't even want it to consider everything, you're going to have to break it down into smaller modules/projects/source trees so that you're explicitly telling ant what to compile.

如果您甚至不希望它考虑所有内容,您将不得不将其分解为更小的模块/项目/源代码树,以便您明确地告诉 ant 要编译什么。

回答by Ankit

Just give only the comma seperated list of files that you want to build. Lets say example

只需给出要构建的文件的逗号分隔列表。说个例子

<property name="includeFileList" value="<name of java class>.java"/>

<javac srcdir="${src.dir}" destdir="${build.dir}"
       target="1.6" debug="true" includes="${includeFileList}"/>

It will work.

它会起作用。