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.
The maven targets are located in your project config/ folder.
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.
target: The path of the file where data will be copied.
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.
You could also use java.nio.file.Paths
:
URL url = Paths.get("target", "results.csv").toUri().toURL();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With