从同一个ANT构建文件构建两个jar文件

时间:2018-04-24 12:11:21

标签: java build ant

我正在尝试构建两个jar文件 - 一个只有与应用程序相关的类,另一个包括测试用例。然而,生成的两个罐子同时具有类和测试用例。

PFB构建XML的片段。

<target name="compile" description="generate the distribution" depends="init">
        <javac srcdir="${src}" debug="${debug}" destdir="${basedir}/dist/classes">
            <classpath refid="class.path" />
        </javac>
        <jar jarfile="${dist}/${jarname}.jar" basedir="${basedir}/dist/classes">
            <metainf  dir="${src}/META-INF"  includes="beans.xml" />
        </jar>
    </target>

    <target name="compilewithtest" description="generate the distribution with test" depends="init,compile">
        <javac srcdir="${test}" debug="${debug}" destdir="${basedir}/dist/classes">
            <classpath refid="class.path" />
        </javac>
        <jar jarfile="${dist}/${jarnamewithtest}.jar" basedir="${basedir}/dist/classes">
            <metainf  dir="${src}/META-INF"  includes="beans.xml" />
        </jar>
    </target>

    <target name="deploy">
        <antcall target="clean" />
        <antcall target="compile" />
        <antcall target="compilewithtest" />
        <antcall target="junitreport" />
    </target>

    <target name="clean">
        <delete dir="${basedir}/dist" />
        <delete dir="${basedir}/report" />
    </target>

是否无法从同一个类目录生成两个jar?我该如何解决这个问题?

编辑 -

如果类目录不同,它可以正常工作。是否无法使用同一目录。以下突出显示的部分是多余的。

<target name="compile" description="generate the distribution" depends="init">
        **<javac srcdir="${src}" debug="${debug}" destdir="${basedir}/dist/classes">**
            <classpath refid="class.path" />
        </javac>
        <jar jarfile="${dist}/${jarname}.jar" basedir="${basedir}/dist/classes">
            <metainf  dir="${src}/META-INF"  includes="beans.xml" />
        </jar>
    </target>

    <target name="compilewithtest" description="generate the distribution with test" depends="init,compile">
        <javac srcdir="${src}" debug="${debug}" destdir="${basedir}/dist/class">
            <classpath refid="class.path" />
        </javac>
        **<javac srcdir="${test}" debug="${debug}" destdir="${basedir}/dist/class">**
            <classpath refid="class.path" />
        </javac>
        <jar jarfile="${dist}/${jarnamewithtest}.jar" basedir="${basedir}/dist/class">
            <metainf  dir="${src}/META-INF"  includes="beans.xml" />
        </jar>
    </target>

    <target name="deploy">
        <antcall target="clean" />
        <antcall target="compile" />
        <antcall target="compilewithtest" />
        <antcall target="junitreport" />
    </target>

    <target name="clean">
        <delete dir="${basedir}/dist" />
        <delete dir="${basedir}/report" />
    </target>

2 个答案:

答案 0 :(得分:0)

以下模板应该有效:

<target name="compile">
    <antcall target="clean" />
    <!-- javac and jar without test-->
</target>
<target name="compilewithtest">
    <antcall target="clean" />
    <!-- javac and jar with test-->
</target>
<target name="deploy">
    <antcall target="compile" />
    <antcall target="compilewithtest" />
</target>
<target name="clean">
    <!-- Delete and create required directories -->
</target>

因此,对于jar的每个编译/构建,都会执行“clean”。此外,我建议通过参数化源/目标文件夹的目标,为多个jar使用相同的目标进行编译/ jar创建。

答案 1 :(得分:0)

根据以上输入的变化已经解决了这个问题。

<target name="compile" description="generate the distribution" depends="init">
        <javac srcdir="${src}" debug="${debug}" destdir="${basedir}/dist/classes">
            <classpath refid="class.path" />
        </javac>
        <jar jarfile="${dist}/${jarname}.jar" basedir="${basedir}/dist/classes">
            <metainf  dir="${src}/META-INF"  includes="beans.xml" />
        </jar>
    </target>

    <target name="compilewithtest" description="generate the distribution with test">
        <antcall target="clean" />
        <antcall target="init" />
        <javac srcdir="${src}" debug="${debug}" destdir="${basedir}/dist/classes">
            <classpath refid="class.path" />
        </javac>
        <javac srcdir="${test}" debug="${debug}" destdir="${basedir}/dist/classes">
            <classpath refid="class.path" />
        </javac>
        <jar jarfile="${dist}/${jarnamewithtest}.jar" basedir="${basedir}/dist/classes">
            <metainf  dir="${src}/META-INF"  includes="beans.xml" />
        </jar>
    </target>

    <target name="deploy">
        <antcall target="clean" />
        <antcall target="compile" />
        <antcall target="compilewithtest" />
        <antcall target="junitreport" />
    </target>

    <target name="clean">
        <delete dir="${basedir}/dist/classes" />
        <delete dir="${basedir}/report" />
    </target>