Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit + Maven: accessing ${project.build.directory} value

Tags:

java

junit

maven

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?

like image 901
Puce Avatar asked Feb 09 '11 17:02

Puce


People also ask

What is project build outputDirectory in Maven?

sourceDirectory , scriptSourceDirectory , and testSourceDirectory provide access to the source directories for the project. outputDirectory and testOutputDirectory provide access to the directories where Maven is going to put bytecode or other build output.

What is Maven target directory?

The target directory is used to house all output of the build. The src directory contains all of the source material for building the project, its site and so on. It contains a subdirectory for each type: main for the main build artifact, test for the unit test code and resources, site and so on.

What is ${ Basedir?

${project. basedir} is the root directory of your project.

What is JUnit in Maven?

JUnit is the testing framework that is extensively used for java projects built in the maven project format for unit testing purposes. Here we will see how we can mention the JUnit dependency in pom. xml file in a maven project and see various annotations and assert methods that can be used in java projects.


1 Answers

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; } 
like image 148
Upgradingdave Avatar answered Sep 21 '22 08:09

Upgradingdave