0

I want my Spring Boot application to run on other computers with the example data that I enter. Currently, I can exit the IDE and restart the application and it works fine, but as soon as I upload my project for my colleagues to download, they do not have any data they can access. How can I implement the function that I can upload the project and everyone using the application can access the test data that I have entered before?

My application.properties in the main folder:

spring.h2.console.enabled=true
spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:~/spring-boot-h2-db;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

spring.jpa.hibernate.ddl-auto=update

My build.gradle:

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'de.hsba.bi.traveldiary'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'com.h2database:h2'
    implementation 'org.springframework.boot:spring-boot-devtools'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:2.3.0'
    implementation 'org.springframework.boot:spring-boot-starter-web-services'
    implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
    runtime 'com.h2database:h2'
}

Thanks a lot in advance!

JaLo
  • 3
  • 1
  • Do you you share your h2 databade file located in the user home as well (~/spring-boot-h2-db). This where the data is. Share this file as well and let your colleagues put it into their user home directory. – Thomas Herzog Apr 04 '19 at 20:31

2 Answers2

1

You have multiple options

1, If more than 1 developer is working on an application, you should create a shared DB for example MySql on a server or computer that anybody can reach

2, If you want to go with h2 you can populate it with application startup: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

3, Im guessing this an h2 db stored in a file in your home directory, so you can copy it also <- im not sure this works, but in theory it should be okay

scrach
  • 21
  • 4
0

Your data is being saved to the database configured by the spring.datasource properties. In your case this is ~/spring-boot-h2-db, which is an H2 database on your local machine.

If you want to share the data between multiple machines then you'll need to set up a database that they can all access. H2 probably isn't the right choice for a database that is used by multiple applications, Postgres or MySql are better options.

Another option would be changing the location of your H2 database file and committing/uploading it with your application. If you're just trying to give some initial data then that might be a good enough solution, but if you want changes to be visible across all applications when they are made then this won't solve your problem.

You can also use a tool like Flyway (supported by Spring Boot) to create reference data on startup. You can generate scripts to create all the existing data you've set up using the H2 command SCRIPT TO 'fileName' (as described in this SO answer). You can access an H2 console where you can run the SCRIPT command by adding the properties

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.web-allow-others=true #ONLY if you want to be able to connect using something other than "localhost" in your URL

If you navigate to <application-path>/h2-console you'll see a login screen asking for a JDBC connection string, a username and a password. Enter the same details for all of them as you have in your properties file and you'll be able to run SQL against your H2 DB.

Player One
  • 607
  • 6
  • 12
  • Thanks a lot! The option with changing the location is enough in the first place. Is it possible to clarify in the application.properties that the db file is in the project folder and to access it? Or does every one who downloads the project need to put the db file manually in his Home Directory? (Project folder path \Traveldiary) – JaLo Apr 05 '19 at 06:23
  • You can make it a relative directory, but if your goal is to set up initial data then the link in Scrach's answer is probably the best way to go. Flyway is easy to set up, and then you can be confident that every user will have the same data no matter where they configure their database to be. I just forgot about it when I was writing my answer and don't want to steal credit by editing it in. – Player One Apr 05 '19 at 06:55
  • But I already set up lots of data in the H2 Database, so it would be a lot of rework to do. In case of making it a relative directory, how does it work? The following code doesn't work... h2.implicitRelativePath=true spring.datasource.url=jdbc:h2:~/Traveldiary/h2/spring-boot-h2-db;DB_CLOSE_ON_EXIT=FALSE – JaLo Apr 05 '19 at 08:38
  • @JaLo you'll need to start your path with a `.` instead of a `~` for a relative path. E.g. `jdbc:h2:file:./db/spring-boot-h2-db` would put it in a directory called "db" in the application's working directory. Generating insert scripts for Flyway from an existing DB is really easy though (I've done it for a project before), I've added some details on how into my answer. I'd strongly encourage you to explore that option before committing an H2 database into your source control. – Player One Apr 05 '19 at 09:19