Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the temp folder of a machine running maven?

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?

like image 897
AlikElzin-kilaka Avatar asked Mar 23 '16 14:03

AlikElzin-kilaka


People also ask

How do I find tmp folder?

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.

How to get path of temp folder in Java?

In Java, we can use System. getProperty("java. io. tmpdir") to get the default temporary file location.

How to get tomcat temp directory in Java?

You can use property java. io. tmpdir to get the tmp location and save your files there.

How do I create a temp folder in Junit?

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"); // ... } }


Video Answer


2 Answers

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.

like image 183
A_Di-Matteo Avatar answered Sep 29 '22 02:09

A_Di-Matteo


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
like image 33
roiros Avatar answered Sep 29 '22 02:09

roiros