只用 ant 编译源代码树的一部分 [英] compiling only part of the source tree with ant

查看:25
本文介绍了只用 ant 编译源代码树的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Say I have my sources in my src/ tree (and possibly in my test/ tree). Say I would like to compile only part of 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!

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

This can be easily achieved from the command line with:

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

现在,你怎么能用蚂蚁做到这一点?javac ant 任务编译一切:

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

源目录和目标目录将递归扫描 Java要编译的源文件.

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

可以使用 excludesincludes 参数,但它们在此方面存在问题.事实上,似乎必须显式设置所有 includes(不是自动依赖查找),甚至最糟糕excludes 优先于includes:

One can use the excludes and includes parameters, 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 worst that excludes has priority on includes:

当包含和排除都是使用,只有文件/目录匹配至少一个包含模式并且不匹配任何使用排除模式.

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

因此,您不能使用

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

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

Because it will not compile anything :-(

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

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

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

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):

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

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

2) 当你只是 includes ant 记录类似

2) When you just includes ant logs something like

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

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

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

推荐答案

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

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" />

或者更灵活:

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

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

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

<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 documentation and you're sure to find the answer you need.

这篇关于只用 ant 编译源代码树的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆