简体   繁体   中英

Ant: add a single compiled class file to javac classpath?

In Ant, if I have a lone compiled CLASS file located somewhere inside my project (say at gen/stragglers/SomePOJO.class , is there a way to specifically add that class file to my <javac> compile classpath, when I currently have the following:

<javac includeantruntime="false" srcdir="src/main/java"
        destdir="gen/bin/main">
    <classpath refid="main.compile.path"/>
</javac>

If not, why? If so, how? Thanks in advance!

Builld a path refering your class file :

<path id="javac.classpath">
     <pathelement location="./gen/stragglers/SomePOJO.class"/>
</path>

Then use it within your javac task :

<javac srcdir="./src/main/java" destdir="./gen/bin/main">
    <classpath refid="javac.classpath"/>
</javac>

You can't add a single class file as such, you add a directory to the classpath and the compiler will look for classes under that directory at relative paths derived from their package structure. This is not negotiable as far as I know - the directory structure must match the package structure.

So for example, if the class defined in SomePOJO.class is stragglers.SomePOJO then you'd add the gen directory to the classpath

<javac includeantruntime="false" srcdir="src/main/java"
        destdir="gen/bin/main">
    <classpath>
      <pathelement location="gen" />
      <path refid="main.compile.path"/>
    </classpath>
</javac>

If the path to the .class file doesn't match the package declaration of the class then you'll have to move it into a temporary directory that does match the package structure, and then add that directory to the classpath instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM