28

In my unit tests I want to create a tmp directory inside the ${project.build.directory}. How can I access the value of ${project.build.directory} inside my unit test?

One way, which I could think of, is to provide a filtered properties file in the test resources, which holdes that value. (I haven't tried yet, but I think that should work.)

Is there a direct way to access/ pass this property value?

Puce
  • 37,247
  • 13
  • 80
  • 152
  • Probably we could configure to set a system property, but somehow I don't like the idea to pass things via system properties. On the other hand, you could say this is something environment specific (the absolute path at least). What do you think? – Puce Feb 09 '11 at 18:27

3 Answers3

21

I've used something like this with some success before. The unit test will still run even if not using Maven, the target directory will still get created two dirs up relative to the cwd of wherever the tests are run.

public File targetDir(){
  String relPath = getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
  File targetDir = new File(relPath+"../../target");
  if(!targetDir.exists()) {
    targetDir.mkdir();
  }
  return targetDir;
}
Stephan
  • 41,764
  • 65
  • 238
  • 329
Upgradingdave
  • 12,916
  • 10
  • 62
  • 72
  • Slightly late to the discussion but doesn't `relPath` equal `src/main/java` so surely you need to resolve to target with `../../../target`? – Adam Sep 19 '17 at 09:42
20

I think using system properties is quite straightforward if you configure the surefire-plugin as explained here http://maven.apache.org/plugins/maven-surefire-plugin/examples/system-properties.html . Even the example there is answering your question directly:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <version>2.9</version>
         <configuration>
           <systemPropertyVariables>
             <propertyName>propertyValue</propertyName>
             <buildDirectory>${project.build.directory}</buildDirectory>
             [...]
           </systemPropertyVariables>
         </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
AdrianRM
  • 2,622
  • 2
  • 25
  • 42
6

Remember, that your unit tests don't have to be executed from Maven surefire plugin, so ${project.build.directory} property might not be available. To make your tests more portable I would rather recommend using File.createTempFile().

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 3
    Well, actually, that's what we currently do and I want to change that. We had an issue that the tmp directory on Hudson got filled up until we ran out of disk space. We're investigating several options, one is to pass a tmp directory inside the target directory as an argument to File.createTempFile(). Like this, "mvn clean" would clean everything. – Puce Feb 09 '11 at 18:21
  • 1
    Besides, as this is a Maven project, I think the tests will only run using maven or an IDE which supports Maven (here: Eclipse). – Puce Feb 09 '11 at 18:24
  • I have another argument against it - we had issues on Mac and Windows with TestContainers - temp files could not be bound inside the docker container. – dmatej Apr 18 '20 at 09:11