Where to put data or config files loaded by my Java code when web app launches in a Vaadin 14 web app driven by Maven
Asked Answered
U

1

3

In a Vaadin 14 web app project created by the "Plain Java Servlet" flavor of the Vaadin Starter page, there are numerous folders automatically created by the Maven POM file process.

screenshot of IntelliJ "Project" pane showing folders nested within project

Where is the place to put a data file or configuration file that I will load and parse when my web app launches? Under what folder do I put my files? Should I create further nested folders? And by what file path name do I find and load such a text (XML, JSON, CSV, etc.) file?

Unnamed answered 31/1, 2020 at 0:52 Comment(2)
Similar: Where should I place my Vaadin 10+ static files?Unnamed
Similar: Where to put Flyway migrations in a Vaadin project (Java Servlet web app)?Unnamed
M
6

Location of file

You put them in src/main/resources as it is the convention also outside of a Vaadin application.

Vaadin adds several other resource roots for things that later end up in places in the jar, that are conventient for vaadin to find (e.g. under META-INF/resources/...).

resources still ends up in the root of the jar or get properly "classpathed" by the build-tools and is safe to load non-classes via the classloader in your application.

Opening file

You can open your text file from there by calling Class::getResourceAsStream, returning an InputStream. Note the leading slash character.

InputStream inputStream = this.getClass().getResourceAsStream( "/myfile.txt" ) ;
Musselman answered 31/1, 2020 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.