0

I have a database (.db-File) in my resources folder, which gets copied in every run of my Java code (I want to keep that file there because it is 'only' a university project and I need this for simplicity). That copying leads to the problem, that all changes made during runtime via code get lost every time, because the changes are only made to the copy in the build folder and not to the original.

How can I make Gradle to stop copying this file and force it to use the original file in the resources folder?

(Similar question, but without micronaut: Is it possible prevent copying of resources to build directory in Gradle project and directly use them from original location?)

fusion
  • 444
  • 4
  • 14

1 Answers1

1

You can exclude certain files in the build process.

build.gradle file:

sourceSets {
    main {
        resources {
            exclude '*.db-File'
        }
    }
}

Gradle 1.2: Exclude directory under resources sourceSets

  • That works. Thanks! But before I could access that File via DriverManager.getConnection("jdbc:sqlite::resources:" + dbUrl); (dbUrl was only 'UserData.db', because it was straight in the resources folder without subdirectories) Now it only works with: dbUrl = "src/main/resources/UserDb.db"; which I assume is kinda dirty.. Any better way for that issue? – fusion Jun 20 '20 at 20:37
  • @fusion, Ideally, you should pass the connection URL of the running database in the environment variable. To make things little simpler, you could put the connection URL in "application.properties" file and read it from there. – Dharmendra Vishwakarma Jun 20 '20 at 21:06
  • I decided to setup some test data in the db inside the resource folder and let my program copy this db to home directory (if it doesn't yet exist there) and access it from there. Probably a better approach at all. But thanks though, this did actually what I wanted first. – fusion Jun 22 '20 at 09:21