简体   繁体   中英

Ant's runtime classpath can't find dependencies for javac

I am trying to get Ant to compile my project, which has the following directory structure:

MyProject/
    src/main/java/
        com/
            <The rest of my source>
    gen/
        bin/
            main/
                <Where I want all Java sources compiled to>
    lib/
        main/
            <All of my dependencies>
    build.xml
    build.properties

Here's my Ant task:

<path id="src.main.java.path">
    <fileset dir="src/main/java">
        <include name="**.*java"/>
    </fileset>
</path>

<path id="lib.main.path">
    <fileset dir="lib/main">
        <include name="**.*jar"/>
    </fileset>
</path>

<target name="compile">
    <javac includeantruntime="true" srcdir="src/main/java" destdir="gen/bin/main">
        <!--
        Define a new classpath composed of all main Java sources and main Java dependencies,
        and use them for this compilation.
        -->
        <classpath>
            <pathelement path="src.main.java.path"/>
            <pathelement path="lib.main.path"/>
        </classpath>
    </javac>
</target>

I should mention that build.xml is located at the project root directory, and that its basedir attribute is set to "." (the project root dir).

When I run the "compile" task I get 100+ compiler errors that it can't find any of my project dependencies (such as Guice, Joda-Time, XStream, etc.):

compile:
    [javac] Compiling 53 source files to /<path-to-my-project-root>/MyProject/gen/bin/main
    [javac] /<path-to-my-project-root>/MyProject/src/main/java/org/me/Widget.java:37: package com.google.inject does not exist
    [javac] import com.google.inject.AbstractModule;
    [javac]                         ^
    [javac] /<path-to-my-project-root>/MyProject/src/main/java/org/me/MyWidget.java:45: cannot find symbol
    [javac] symbol: class AbstractModule
    [javac] public class AppUziContext extends AbstractModule {
    [javac]            

BUILD FAILED
/<path-to-my-project>/MyProject/build.xml:51: Compile failed; see the compiler error output for details.

... the list is 100+ errors that all look like this, but are different for every dependency that it can't find.

I believe the problem lies somewhere in the paths I've configured and am referencing inside javac/classpath , but after reading the Ant docs I can't figure out where I'm going awrye. Thanks in advance!

Try this in your target:

<classpath>
    <path refid="src.main.java.path"/>
    <path refid="lib.main.path"/>
</classpath>

I usually make complete classpaths for targets, then use a one-liner classpath within the target:

<path id="target.class.path">
    <path refid="src.main.java.path"/>
    <path refid="lib.main.path"/>
</path>

Then in the target:

<classpath refid="target.class.path"/>

pathelement isn't very well documented, this is the best that you'll get from the ant site. Note that in these examples, <pathelement path= is always used with a property, otherwise it's <pathelement location taking an actual folder name (which could also be a property).

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