如何在 ant 的 javac 任务中排除源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3145968/
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
How can I exclude sources in a javac task in ant?
提问by Laurence Gonsalves
I have the following in my build.xml:
我的 build.xml 中有以下内容:
<target name="compile.baz" depends="init">
<javac destdir="${build.dir}/classes" debug="on">
<compilerarg value="-Xlint:deprecation"/>
<src>
<pathelement location="${src.dir}/com/foo/bar/baz/" />
<pathelement location="${src.dir}/com/foo/bar/quux/" />
<!-- Need to exclude ${src.dir}/com/foo/bar/quux/dontwant/ -->
</src>
<classpath refid="classpath.jars" />
</javac>
...
</target>
This mostly does what I want, except that (as the comment says) I do notwant the files in${src.dir}/com/foo/bar/quux/dontwant/
to be compiled by this task (but I do want everything elseunder ${src.dir}/com/foo/bar/quux/
to be compiled in this task).
这主要是我想要做什么,但(作为注释说)我不希望在文件中${src.dir}/com/foo/bar/quux/dontwant/
通过该任务编译(但我想其他的一切在${src.dir}/com/foo/bar/quux/
此任务中进行编译)。
I'm a complete ant n00b, and the documentation hasn't been much help to me. I see several places where it says there are various exclude/excludes elements/attributes, but every variation I can think of either has no effect or results in an error like "blahdoesn't support the 'exclude' attribute".
我是一个完整的蚂蚁 n00b,文档对我没有多大帮助。我看到几个地方说有各种 exclude/excludes 元素/属性,但我能想到的每一个变化要么没有效果,要么导致错误,比如“ blah不支持'exclude'属性”。
采纳答案by Laurence Gonsalves
A couple of people suggested using <exclude>
. This didn't work with the way my task was specified. trashgod's answer linked to the sixth example on this pagewhich gave me an idea of how to restructure my task specification.
有几个人建议使用<exclude>
. 这不符合我指定任务的方式。垃圾神的回答链接到本页上的第六个示例,它让我了解了如何重组我的任务规范。
It looks like my problem was related to the way I was specifying the source files. Rather than using <pathelement>
elements in a <src>
, like this:
看起来我的问题与我指定源文件的方式有关。而不是<pathelement>
在 a 中使用元素<src>
,如下所示:
<src>
<pathelement location="${src.dir}/com/foo/bar/baz/" />
<pathelement location="${src.dir}/com/foo/bar/quux/" />
</src>
I switched to using a single <src>
with a path and then a set of <include>
elements, like this:
我转而使用<src>
带有路径的单个<include>
元素,然后使用一组元素,如下所示:
<src path="${src.dir}" />
<include name="com/foo/bar/baz/**" />
<include name="com/foo/bar/quux/**" />
This appears to be functionally identical, but is compatible with the use of <exclude>
:
这似乎在功能上相同,但与使用兼容<exclude>
:
<exclude name="${src.dir}/com/foo/bar/quux/dontwant/**"/>
(Actually, I'm surprised that what was there in the first place worked at all.)
(实际上,我很惊讶最初存在的东西竟然有效。)
回答by matiasf
Try
尝试
<javac>
(...>
<exclude name="${src.dir}/com/foo/bar/quux/dontwant/*" />
(...)
</javac>
回答by trashgod
I'm not sure about the rest, but the <exclude/>
nested element should work in the Javac
task. See the sixth exampledown.
我不确定其余的,但<exclude/>
嵌套元素应该在Javac
任务中工作。向下看第六个例子。
Addendum: Patterns, including the **
notation, are discussed in Directory-based Tasks.
附录:模式,包括**
符号,在基于目录的任务中讨论。
<target name="compile.baz" depends="init">
<javac destdir="${build.dir}/classes" debug="on">
<compilerarg value="-Xlint:deprecation"/>
<src>
<pathelement location="${src.dir}/com/foo/bar/baz/" />
<pathelement location="${src.dir}/com/foo/bar/quux/" />
</src>
<exclude name="${src.dir}/com/foo/bar/quux/dontwant/**"/>
...
</javac>
...
</target>
回答by user2177577
try
folder "test" under {source.dir}
would not be complied
try 文件夹下的“test”{source.dir}
不会被编译
<javac destdir="${compile.dir}" ...>
<src path="${source.dir}" />
<exclude name="test/**"/>
</javac>
回答by May Bogus
From my experiments you should not include full path for file you want to exclude. This one doesn't work:
根据我的实验,您不应该包含要排除的文件的完整路径。这个不起作用:
<javac> (...> <exclude name="${src.dir}/com/foo/blah/blah1/FILENAME.java"/> (...) </javac>
<javac> (...> <exclude name="${src.dir}/com/foo/blah/blah1/FILENAME.java"/> (...) </javac>
but this one does:
但这个确实:
<javac> (...> <exclude name="com/foo/blah/blah1/FILENAME.java"/> (...) </javac>
<javac> (...> <exclude name="com/foo/blah/blah1/FILENAME.java"/> (...) </javac>
回答by Giles
If you are trying to exclude Java classes but Ant is still trying to compile them then this may be due to references to these classes in your code.
如果您尝试排除 Java 类但 Ant 仍在尝试编译它们,那么这可能是由于在您的代码中引用了这些类。
If A.java has a reference to B.java and you try to exclude B.java then compiling A.java will require that B.java is compiled too.
如果 A.java 引用了 B.java 并且您尝试排除 B.java,那么编译 A.java 将要求也编译 B.java。
This is one reason for interfaces in Java, so you can compile the interface without needing to compile the implementations.
这是 Java 中接口的原因之一,因此您可以编译接口而无需编译实现。