Search code examples
javamavengradledesktop-applicationproject-structure

Directory for external files in the standard maven directory layout


In Java, we have two different approaches for loading data into the application.

If a file is packed into the jar (call it resource file) we get its InputStream by Class.getResourceAsStream() method.

If a file is not inside the jar (call it external file) we use new FileInputStream().

If I understand correctly, the distinction between resource and external files is due to the fact that the former are required for the application to launch and run properly (therefore packed into an executable jar - shouldn't be modified) and the latter are rather optional and may or may be not provided by the user.

In the standard maven directory layout we put our resource files in the src/main/resources directory so they are included in the jar when building a project. But I can't figure out what the proper directory for external files is.

In the distributed version of my application I want to provide a certain set of default external files, so I need to put them somewhere in the project structure. What is the best location?

So far, I've seen src/main/webapp (which doesn't seem appropriate for desktop applications) and src/main/conf (which seems to get out of use and besides the external files may be of different types - not only text).

A custom directory like src/main/assets or src/main/data? Or maybe there should be another source directory next to the main and test?

I'll be grateful for any suggestions!


Solution

  • As it is an external file, it can't be under src, you can put it under (for example) static_content (a folder next to the POM.xml) and add this to your POM

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                target/static_content/csvs
                            </outputDirectory>
                            <resources>
                                <resource>
                                   <directory>static_content/csvs</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>