简体   繁体   中英

ANT - How to append compiled classes to classpath dynamically

Setup:

Project A - Pure Java project with no dependencies.

Project B - Pure Java project depends on project A.

Process:

I have a build project script in each of the project root directory and a master script to run them both, in the correct order, first Project A and then Project B. The script output is relative to each project's path.

The script works just fine for Project A, but when it comes to Project B it misses the classes output of Project A.

Using ANT, is there a way to add "dynamically" to the compile classpath the output of a previously compile project?

Or, is there any action I can take except explicitly provide Project B with the classes output path of Project A?

OK, so this took a bunch of hacking.

First use add ant-contrib to your ant you can download it from here .

Then I declared a var instead of a property in my main ant script. In the compile macro I've passed it to the javac as the classpath. After the compile is done, I've appended the new classes output folder to the classpath var and called the next compile.

Good luck.

The compile script:

<?xml version="1.0"?>
<project name="PDF Test Client" default="main" basedir=".">
    <taskdef resource="net/sf/antcontrib/antlib.xml" />

    <macrodef name="Compile">
        <attribute name="ProjectName" default="   -- set aname to a 'ProjectName' property --" />
        <attribute name="ProjectRootDir" default="." />
        <attribute name="SourceDir" default="@{ProjectRootDir}/src" />
        <attribute name="OutputDir" default="@{ProjectRootDir}/output" />
        <attribute name="BuildDir" default="@{OutputDir}/bin" />
        <attribute name="ClassesDir" default="@{BuildDir}/classes" />

        <sequential>
            <echo message="Compiling... @{ProjectName}" />
            <mkdir dir="@{ClassesDir}" />
            <javac srcdir="@{SourceDir}" destdir="@{ClassesDir}" classpath="${ClassPathFolders}" includeantruntime="true" />
            <var name="ClassPathFolders" value="${ClassPathFolders}; @{ClassesDir}" />
            <echo message="-" />
        </sequential>
    </macrodef>

    <target name="main">
    </target>

</project>

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