How to set the working directory for a <junit> task to something other than the basedir?

I have an Ant script with a junit target where I want it to start up the VM with a different working directory than the basedir. How would I do this?

Here's a pseudo version of my target.

<target name="buildWithClassFiles">
    <mkdir dir="${basedir}/UnitTest/junit-reports"/>
    <junit fork="true" printsummary="yes">
            <pathelement location="${basedir}/UnitTest/bin"/>
            <path refid="classpath.compile.tests.nojars"/>
        <jvmarg value="-javaagent:${lib}/jmockit/jmockit.jar=coverage=:html"/>
        <formatter type="xml" />
        <test name="GlobalTests" todir="${basedir}/UnitTest/junit-reports" />


Asked by: Michelle677 | Posted: 21-01-2022

Answer 1

Have you tried:

 <junit fork="true" printsummary="yes" dir="workingdir">

Answered by: Patrick229 | Posted: 22-02-2022

Answer 2

I think the other answers might be overlooking the fact that you want the working directory to be specified, not just that you want to run junit on a particular directory. In other words, you want to make sure that if a test creates a file with no path information, it is from the base directory you are specifying.

Try to pass in the directory you want as a JVM arg to junit, overriding user.dir:

 <junit fork="true" ...>
   <jvmarg value="-Duser.dir=${desired.current.dir}"/>

Answered by: Arnold489 | Posted: 22-02-2022

Similar questions

java - How to set the parent directory of the basedir in maven as the basedir of ant?

Background : I need to run an ant task in the build.xml file using maven. So I'm using maven-antrun-plugin to do this. Question : When I'm reading the basedir in ant (after running pom) it displays as fldr1.fldr2.prjctNm which was it's defualt value (in both echoes). But, I want to set the parent directory path to it. (eg : fldr1.fldr2). How can I do this. Ant file a...

java - How do I copy built artifact to a directory on remote Windows server in maven deploy phase?

could someone provide working example (full maven plugin configuration) how to copy built jar file to a specific server(s) at the time of deploy phase? I have tried to look at wagon plugin, but it is hugely undocumented and I was not able to set it up. The build produces standard jar that is being deployed to Nexus, but I need to put the jar also to the test server automatically over local network (\someserver\test...

Java - get the newest file in a directory?

Does anybody have a snippet of Java that can return the newest file in a directory (or knowledge of a library that simplifies this sort of thing)?

java - Use Tomcat to serve a directory?

I have a directory on a linux box that I want to make publicly readable using Tomcat (5.5). I think this is easy to set up but can't find the appropriate documentation. Is there a simple way to accomplish this?

java - How do I set the working directory for the Ant 'junit' task?

My Ant build includes a junit task that runs some tests. In order for the tests to work, the value of the property that specifies the current working directory (user.dir) must be changed, but I am unsure how to achieve this. The task in question currently looks like this: &lt;junit printsummary="withOutAndErr" fork="true" haltonfailure="yes" showoutput="true" filte...

java - Mixed language source directory layout

We are running a large project with several different languages: Java, Python, PHP, SQL and Perl. Until now people have been working in their own private repositories, but now we want to merge the entire project in a single repository. The question now is: how should the directory structure look? Should we have separate directories for each language, or should we separate it by component/project? How well does pyt...

java - change directory and run the batch file in the same command prompt

Is there any way to open the command prompt and change directory in the command prompt and run the batch file in the same command prompt using java. I know how to open the command prompt using java. Thanks,

java - Best practice to specify external directory for storing images in Tomcat?

Best practice to specify external directory for storing images in Tomcat?

java - Test data directory with jUnit

I'm writing some jUnit tests that depend on data files. Where should those data files go? And how would I (in the jUnit tests) get the location of that directory? In Python, I would use something similar to: datadir = os.dirname(__file__) + "/data/"

active directory - Detect user logged on a computer using Java

I want to develop an Java application that can detect the user logged on a Window Domain. These credentials are going to be used to logging on the Java application. How can I do this? Thanks!

java - How can I read files in my WEB-INF directory?

I'm running Java web application using Tomcat and I want to read a Properties file that's located in my WEB-INF directory. How can I get an InputStream to this Properties file so I can read it?

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)