Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the target directory path in Maven (Java)

Tags:

java

maven

In a Maven project I want write some test results into a file in target directory of the project. Whenever, I get the project directory, it is actually getting the IntelliJ project directory instead of actual Maven directory.

like image 789
vkrams Avatar asked Jul 25 '16 01:07

vkrams


People also ask

Where is target folder in Maven?

The maven targets are located in your project config/ folder.

How do you get the path of the directory of a file in Java?

The method java. io. File. getAbsolutePath() is used to obtain the absolute path name of a file or directory in the form of a string.

What is the target file Java?

target: The path of the file where data will be copied.

What is src main Java in Maven?

As the name indicates, src/main is the most important directory of a Maven project. Anything that is supposed to be part of an artifact, be it a jar or war, should be present here. Its subdirectories are: src/main/java – Java source code for the artifact.


2 Answers

You could also use java.nio.file.Paths:

URL url = Paths.get("target", "results.csv").toUri().toURL();
like image 139
twalthr Avatar answered Oct 03 '22 23:10

twalthr


I'm using the classloaders root resource path for retrieving projects build directory with following single line:

Path targetPath = Paths.get(getClass().getResource("/").toURI()).getParent();

The root resource path points to test-classes and it's parent is the desired path regardless of running the test via maven surefire or a JUnit runner of the IDE like Eclipse.

like image 33
Jens Vagts Avatar answered Oct 03 '22 23:10

Jens Vagts