[英]How to force ant javac task to overwrite the class file in the destination directory?
Here is a very simplified version of what I am trying to achieve. 这是我要达到的目标的非常简化的版本。 I have two directories, Directory1
and Directory2
. 我有两个目录, Directory1
和Directory2
。 Both directories contain Java source files. 这两个目录都包含Java源文件。 Some of the files in Directory2
can have the same fully qualified class name as the files in Directory1
. 一些文件Directory2
可以有相同的完全合格的类名称,如文件Directory1
。
Using ant, the files are compiled to a directory called CompileDirectory
, first from Directory1
and then from Directory2
. 使用ant,首先从Directory1
然后从Directory2
将文件编译到名为CompileDirectory
Directory2
。 I want the files in Directory2
to be compiled and overwrite the compiled class files from Directory1
. 我想在文件Directory2
进行编译和覆盖编译的类文件Directory1
。 However, ant seems to ignore the classes that have the same fully qualified class name. 但是,ant似乎忽略了具有相同完全限定类名的类。
Here's a simple example - 这是一个简单的例子-
Directory structure 目录结构
$ ls -R
.:
build.xml CompileDirectory Directory1 Directory2
./CompileDirectory:
./Directory1:
A.java
./Directory2:
A.java
build.xml build.xml
<project name="TestProject" default="build" basedir=".">
<target name="build" depends="javac1, javac2" />
<target name="javac1">
<javac srcdir="${basedir}/Directory1" destdir="CompileDirectory" includeantruntime="false"/>
</target>
<target name="javac2">
<javac srcdir="${basedir}/Directory2" destdir="CompileDirectory" includeantruntime="false"/>
</target>
</project>
Ant run 蚂蚁跑
$ ant -buildfile build.xml
Buildfile: ...(path).../build.xml
javac1:
[javac] Compiling 1 source file to ...(path).../CompileDirectory
javac2:
build:
BUILD SUCCESSFUL
Total time: 0 seconds
As can be seen, the javac2
target above does nothing. 可以看出,上面的javac2
目标什么都不做。
When I run the Java program, I see that the class file is the one from Directory1. 运行Java程序时,我看到该类文件是Directory1中的文件。
$ cd CompileDirectory/
$ java A
I am class A from directory 1
Is there a way to force the javac
task in the javac2
target to compile the source file in Directory2
and overwrite the class file in the CompileDirectory
? 有没有一种方法可以强制javac2
目标中的javac
任务编译Directory2
的源文件并覆盖CompileDirectory
的类文件?
It has to do with timestamp of files and whether the compiler thinks the source is newer than class file. 它与文件的时间戳以及编译器是否认为源比类文件更新有关。
<project name="TestProject" default="build" basedir=".">
<target name="build" depends="javac1, touch2, javac2" />
<target name="javac1">
<javac srcdir="${basedir}/Directory1" destdir="CompileDirectory" includeantruntime="false"/>
</target>
<target name="touch2">
<sleep seconds="2" />
<touch datetime="now">
<fileset dir="${basedir}/Directory2" />
</touch>
</target>
<target name="javac2">
<javac srcdir="${basedir}/Directory2" destdir="CompileDirectory" includeantruntime="false"/>
</target>
</project>
Other possible way to avoid this is create a stage directory and compile the classes there and copy back to original directory using overwrite option. 避免这种情况的其他可能方法是创建一个stage目录并在那里编译类,然后使用overwrite选项将其复制回到原始目录。
<project name="TestProject" default="build" basedir=".">
<target name="build" depends="javac1, javac2, copy1" />
<target name="javac1">
<javac srcdir="${basedir}/Directory1" destdir="CompileDirectory" includeantruntime="false"/>
</target>
<target name="javac2">
<javac srcdir="${basedir}/Directory2" destdir="CompileDirectory1" includeantruntime="false"/>
</target>
<target name="copy1">
<copy overwrite="on" todir="CompileDirectory">
<fileset dir="CompileDirectory1">
<include name ="**/*.*"/>
</fileset>
</copy>
</target>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.