【问题标题】:Ant If/Unless Attribute IssueAnt If/Unless 属性问题
【发布时间】:2015-05-05 05:45:07
【问题描述】:

预警,这是我第一次尝试实际创建 ANT 构建脚本。我正在尝试使我的 test 目标仅在 compiled 属性设置为 true 时运行,另外我希望我的 dist 目标运行,除非 @ 987654323@ 属性设置为 true。但由于某种原因,下面的代码似乎并没有按照我的意图去做;这意味着当我尝试运行ant 时它不会执行测试,因为compiled 没有设置为true(我相信),但是如果我查看bin 文件夹,我可以看到确实生成了.class 文件。因此这意味着 dist 也不会运行,因为它取决于编译和测试目标。关于我可能做错的任何想法或想法?

<project name="project" default="dist" basedir="." xmlns:if="ant:if" xmlns:unless="ant:unless">
    <description>
        Build file for the project
    </description>
    <!-- set global properties for this build -->
    <property name="src" location="src"/>
    <property name="lib" location="lib"/>
    <property name="bin" location="bin"/>
    <property name="dist"  location="dist"/>
    <property name="distDirName" value="dist"/>

    <!-- including the external jar files -->
    <path id="proj.classpath">
        <fileset dir="${lib}" includes="*.jar"/>
        <pathelement location="${bin}"/>
    </path>

    <target name="compile" description="compile the source">
        <!-- Create the distribution directory -->
        <mkdir dir="${bin}"/>
        <!-- Compile the java code from ${src} into ${bin} -->
        <javac srcdir="${src}" classpathref="proj.classpath" destdir="${bin}"/>
        <!-- Set condition for dependent target -->
        <condition property="compiled">
            <and>
                <available file="**/SampleTest.class"/>
                <available file="**/DBConnection.class"/>
            </and>
        </condition>
    </target>

    <target name="test" depends="compile" if="compiled">
        <junit failureproperty="junit.failure" errorproperty="junit.failure" printsummary="withOutAndErr" fork="yes" haltonfailure="true" haltonerror="true">
            <formatter type="xml"/>
            <test name="this.one.dir.SampleTest" todir="${bin}"/>
            <classpath refid="proj.classpath"/>
        </junit>
    </target>

    <target name="dist" depends="compile,test" unless="junit.failure" description="generate the distribution">
        <!-- Create the distribution directory -->
        <mkdir dir="${dist}"/>

        <!-- Put everything in ${bin} into the distribution.jar file -->
        <jar jarfile="${dist}/distribution.jar" basedir="${basedir}" excludes="**/test/,${distDirName}/**"/>
    </target>
</project>

【问题讨论】:

  • 您能否添加更多详细信息,说明您希望脚本做什么以及它正在做什么?
  • 你不需要这一切。如果编译失败,构建将停止,因此测试将不会运行。如果 junit 测试失败,构建将停止,因此 dist 目标不会执行。
  • 见 JB Nizet 和 btw 的评论。 ant -v test 确认如果未创建 class 文件之一Skipped because property 'compiled' not set.,则跳过目标测试
  • @bkail 就我目前所看到的而言,我刚刚用更多细节更新了 OP。 @JB Nizet 这就是我自己的想法,但这实际上是我班级的作业,教授告诉我这还不够,他暗示他希望我们也使用 if/unless。 @SubOptimal 没想到要做ant -v。今天晚些时候我在家的时候必须尝试一下,但是就使用 **/SampleTest.class" 尝试查找文件而言,我做错了什么吗?

标签: java ant build compilation target


【解决方案1】:

你需要评估你的变量,例如使用${junit.failure}

<target name="dist" depends="compile,test" unless="${junit.failure}" description="generate the distribution"> 
</target>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2011-03-22
    • 2016-09-08
    • 2020-06-12
    相关资源
    最近更新 更多