2

I am having trouble to use external configuration properties for my Quarkus application.

I want to override the database properties. The application.properties inside the project has properties for dev and test, not for prod. The values for prod are inside the external application.properties.

I read the official guide (https://quarkus.io/guides/config#overriding-properties-at-runtime) and tried to place an application.properties file in a folder next to the runner-jar called config. That didn't work. But just passing the properties on the command line didn't work either.

C:.
│   0.1.0-SNAPSHOT-runner.jar
│   0.1.0-SNAPSHOT.jar.original
│
├───config
│       application.properties

Internal:

%dev.quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/postgres
%dev.quarkus.datasource.username=postgres
%dev.quarkus.datasource.password=postgres
%dev.quarkus.datasource.db-kind=postgresql
%dev.quarkus.hibernate-orm.database.generation=none

%test.quarkus.datasource.jdbc.url=jdbc:h2:mem:test
%test.quarkus.datasource.username=sa
%test.quarkus.datasource.password=
%test.quarkus.datasource.db-kind=h2
%test.quarkus.hibernate-orm.database.generation=none

External:

quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/postgres
quarkus.datasource.username=postgres
quarkus.datasource.password=postgres
quarkus.datasource.db-kind=postgresql
quarkus.hibernate-orm.database.generation=none

With java -jar .\0.1.0-SNAPSHOT-runner.jar I get the following error:

2020-07-24 16:58:16,666 ERROR [io.qua.application] (main) Failed to start application: java.lang.IllegalArgumentException: Parameter 'dataSource' may not be null
        at org.wildfly.common.Assert.checkNotNullParamChecked(Assert.java:71)
        at org.wildfly.common.Assert.checkNotNullParam(Assert.java:49)
        at org.wildfly.security.auth.realm.jdbc.QueryConfiguration.<init>(QueryConfiguration.java:40)
        at org.wildfly.security.auth.realm.jdbc.QueryBuilder.buildQuery(QueryBuilder.java:76)
        at org.wildfly.security.auth.realm.jdbc.JdbcSecurityRealmBuilder.build(JdbcSecurityRealmBuilder.java:51)
        at io.quarkus.elytron.security.jdbc.JdbcRecorder.createRealm(JdbcRecorder.java:42)
        at io.quarkus.deployment.steps.ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.deploy_0(ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.zig:76)
        at io.quarkus.deployment.steps.ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.deploy(ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.zig:36)
        at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:524)
        at io.quarkus.runtime.Application.start(Application.java:90)
        at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:91)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:61)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:38)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:106)
        at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)

2020-07-24 16:58:16,730 ERROR [io.qua.run.Application] (main) Error running Quarkus application: java.lang.RuntimeException: Failed to start quarkus
        at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:649)
        at io.quarkus.runtime.Application.start(Application.java:90)
        at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:91)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:61)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:38)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:106)
        at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)
Caused by: java.lang.IllegalArgumentException: Parameter 'dataSource' may not be null
        at org.wildfly.common.Assert.checkNotNullParamChecked(Assert.java:71)
        at org.wildfly.common.Assert.checkNotNullParam(Assert.java:49)
        at org.wildfly.security.auth.realm.jdbc.QueryConfiguration.<init>(QueryConfiguration.java:40)
        at org.wildfly.security.auth.realm.jdbc.QueryBuilder.buildQuery(QueryBuilder.java:76)
        at org.wildfly.security.auth.realm.jdbc.JdbcSecurityRealmBuilder.build(JdbcSecurityRealmBuilder.java:51)
        at io.quarkus.elytron.security.jdbc.JdbcRecorder.createRealm(JdbcRecorder.java:42)
        at io.quarkus.deployment.steps.ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.deploy_0(ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.zig:76)
        at io.quarkus.deployment.steps.ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.deploy(ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.zig:36)
        at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:524)
        ... 6 more

With java -Dquarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/postgres -Dquarkus.datasource.username=postgres -Dquarkus.datasource.password=postgres -Dquarkus.datasource.db-kind=postgresql -jar .\0.1.0-SNAPSHOT-runner.jar I get this:

Error: Could not find or load main class .datasource.jdbc.url=jdbc:postgresql:..localhost:5432.postgres
Caused by: java.lang.ClassNotFoundException: /datasource/jdbc/url=jdbc:postgresql://localhost:5432/postgres

I could only find two similar threads (How can I override properties in Quarkus? and https://github.com/quarkusio/quarkus/issues/1218). Both mention the method with the application.properties in the config folder. Since this does not work for me and I cannot find any new information, I am somewhat lost ...

I would be grateful for any help!

M..
  • 75
  • 1
  • 4
  • 11

2 Answers2

1

Ok, so it turns out there are several properties which you can not override at runtime. Those are marked on the offical overview of all properties (https://quarkus.io/guides/all-config). There is a little hint about this at the top of the page.

quarkus.datasource.db-kind is one of those properties. So all I had to do is include this property in my main application.properties. After that the external file was found and I could use those values for url, password, and username.

M..
  • 75
  • 1
  • 4
  • 11
  • FYI that's what we do at production anyways – alexander Oct 08 '20 at 10:10
  • I'm surprised that `quarkus.datasource.db-kind` can not be defined at runtime. I usually create an application and allow users to choose between PostgreSQL, MySQL, or just H2. I can not do this anymore with Quarkus. I wonder if this can be enhanced in future releases of Quarkus? – Carlos E. Feria Vila Dec 31 '20 at 11:47
0

Max is right when he says that quarkus.datasource.db-kind is a runtime property.

The only way I got it to work so that I have

%dev 
quarkus.datasource.db-kind=postgres
%qa
quarkus.datasource.db-kind=h2
%prod
quarkus.datasource.db-kind=<otherDBKind>

is to, either use profiling:

gradle/maven quarkusDev -Dquarkus-profile=[dev,qa,prod]

or, if you want to deploy the application and run it through java -jar, you have to compile it with the profile context

gradle/maven quarkusBuild -Dquarkus-profile=[dev,qa,prod]

a build for each profile with a different db-kind

Andre Brito
  • 146
  • 8