user1768830
user1768830

Reputation:

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!

Upvotes: 8

Views: 6326

Answers (2)

Ian Roberts
Ian Roberts

Reputation: 122414

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.

Upvotes: 7

Bax
Bax

Reputation: 4506

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>

Upvotes: 5

Related Questions