I'd like to save some unpacked files into the temp folder of a machine.
Question: How can I get the temp folder using maven?
Question: Will it work on both linux and windows environments?
Click the Start button to open the Start menu. Click the Find option, then click Find Files or folders. In the named box, type *. tmp to search for any files with a "tmp" file extension.
In Java, we can use System. getProperty("java. io. tmpdir") to get the default temporary file location.
You can use property java. io. tmpdir to get the tmp location and save your files there.
public static class HasTempFolder { @Rule public TemporaryFolder folder= new TemporaryFolder(); @Test public void testUsingTempFolder() throws IOException { File createdFile= folder. newFile("myfile. txt"); File createdFolder= folder. newFolder("subfolder"); // ... } }
Maven supports, as part of the default properties, any Java System property, hence you can use the following property:
java.io.tmpdir
Default temp file path
As example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<!-- further conf here -->
<outputDirectory>${java.io.tmpdir}/libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Note the outputDirectory
element and its value.
As a further note, also note that the target
folder of the Maven build is also meant to host temporary files, so you should also consider to use it for such a purpose.
Will it work on both linux and windows environments?
Yes, since it is Java property, it is supposed to be OS independent.
use the java environment tmp dir - java.io.tmpdir
you can access it from maven via ${java.io.tmpdir}
without having to predefine it.
you can also customize it on a specific run by running:
mvn clean install -Djava.io.tmpdir=/tmp/where/ever
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