Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1063833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:50:36+00:00 2026-05-16T18:50:36+00:00

I’m using the Hello World with Ant tutorial from the Ant manual to learn

  • 0

I’m using the Hello World with Ant tutorial from the Ant manual to learn about Ant.

The last part of the tutorial involves adding JUnit tests to the project.

I’ve got everything working as described in the tutorial and am now going on to make some minor changes.

One of the changes I would like to make is to run the tests during a typical build but not have the *Test.class files end up in the final .jar file for the application. This is because the eventual project I will be working on will be for a device with limited hard drive space and support for only a subset of the Java SDK so I would prefer to just omit these test files entirely from the jar.

How do I do this?

It would be easy enough to create two separate jars, one for testing and one for deployment, but this seems less than ideal.

My current build.xml file is below.

<property name="src.dir"     value="src"/>

<property name="build.dir"   value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir"     value="${build.dir}/jar"/>
<property name="lib.dir"     value="lib"/>
<property name="report.dir"  value="${build.dir}/junitreport"/>


<property name="main-class"  value="oata.HelloWorld"/>

<path id="classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar"/>
    <path location="[LocalPath]/junit-4.8.2.jar"/>
</path>

<path id="application" location="${jar.dir}/${ant.project.name}.jar"/>

<target name="clean">
    <delete dir="${build.dir}"/>
</target>

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
    <copy todir="${classes.dir}">
        <fileset dir="${src.dir}" excludes="**/*.java"/>
    </copy>
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
        </manifest>
    </jar>
</target>

 <target name="junit" depends="jar">
     <mkdir dir="${report.dir}"/>
     <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
        <classpath>
            <path refid="classpath"/>
            <path refid="application"/>
        </classpath>

        <formatter type="xml"/>

        <batchtest fork="yes">
            <fileset dir="${src.dir}" includes="*Test.java"/>
        </batchtest>
    </junit>
</target>

<target name="junitreport" depends="junit">
    <junitreport todir="${report.dir}">
        <fileset dir="${report.dir}" includes="TEST-*.xml"/>
        <report todir="${report.dir}"/>
    </junitreport>
</target>

<target name="run" depends="junit">
    <java fork="true" classname="${main-class}">
        <classpath>
            <path refid="classpath"/>
            <path refid="application"/>
        </classpath>
    </java>
</target>

<target name="clean-build" depends="clean,junit"/>

<target name="main" depends="clean,run"/>

One thing I have tried is modifying the jar command to exclude the *Test.class files

...
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" excludes="**/*Test.class">
...

which successfully excludes the test classes but then when the tests are run via the junit target it fails with the following stack trace when run with -v:

[LocalPath]\build.xml:44: Test HelloWorldTest failed
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.actOnTestResult(JUnitTask.java:1863)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:814)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1808)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:760)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
        at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
        at org.apache.tools.ant.Task.perform(Task.java:348)
        at org.apache.tools.ant.Target.execute(Target.java:390)
        at org.apache.tools.ant.Target.performTasks(Target.java:411)
        at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1397)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1366)
        at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1249)
        at org.apache.tools.ant.Main.runBuild(Main.java:801)
        at org.apache.tools.ant.Main.startAnt(Main.java:218)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:50:37+00:00Added an answer on May 16, 2026 at 6:50 pm

    Based on @Jon’s advice I changed the junit target to run against the build/classes folder instead of the jar and updated the dependencies appropriately.

    My updated build.xml file is below:

    <project name="HelloWorld" basedir="." default="main">
    
        <property name="src.dir"     value="src"/>
    
        <property name="build.dir"   value="build"/>
        <property name="classes.dir" value="${build.dir}/classes"/>
        <property name="jar.dir"     value="${build.dir}/jar"/>
        <property name="lib.dir"     value="lib"/>
        <property name="report.dir"  value="${build.dir}/junitreport"/>
    
        <property name="main-class"  value="oata.HelloWorld"/>
    
        <path id="classpath">
            <fileset dir="${lib.dir}" includes="**/*.jar"/>
            <path location="[LocalPath]/junit-4.8.2.jar"/>
        </path>
    
        <path id="application" location="${jar.dir}/${ant.project.name}.jar"/>
    
        <target name="clean">
            <delete dir="${build.dir}"/>
        </target>
    
        <target name="compile">
            <mkdir dir="${classes.dir}"/>
            <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
            <copy todir="${classes.dir}">
                <fileset dir="${src.dir}" excludes="**/*.java"/>
            </copy>
        </target>
    
        <target name="jar" depends="junit">
            <mkdir dir="${jar.dir}"/>
            <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" excludes="**/*Test.class">
                <manifest>
                    <attribute name="Main-Class" value="${main-class}"/>
                </manifest>
            </jar>
        </target>
    
        <target name="junit" depends="compile">
            <mkdir dir="${report.dir}"/>
            <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
                <classpath>
                    <path refid="classpath"/>
                    <path location="${classes.dir}"/>
                </classpath>
    
                <formatter type="xml"/>
    
                <batchtest fork="yes">
                    <fileset dir="${src.dir}" includes="*Test.java"/>
                </batchtest>
            </junit>
        </target>
    
        <target name="junitreport" depends="junit">
            <junitreport todir="${report.dir}">
                <fileset dir="${report.dir}" includes="TEST-*.xml"/>
                <report todir="${report.dir}"/>
            </junitreport>
        </target>
    
        <target name="run" depends="jar">
            <java fork="true" classname="${main-class}">
                <classpath>
                    <path refid="classpath"/>
                    <path refid="application"/>
                </classpath>
            </java>
        </target>
    
        <target name="clean-build" depends="clean,jar"/>
    
        <target name="main" depends="clean,run"/>
    
    </project>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.