Configuring your Red Hat build of Quarkus applications by using a properties file Red Hat build of Quarkus 3.15 Red Hat Customer Content Services

Commands:
mvn --version
export PATH=USDPATH:<path_to_JDK>
mvn com.redhat.quarkus.platform:quarkus-maven-plugin:3.15.3.SP1-redhat-00002:create -DprojectGroupId=org.acme -DprojectArtifactId=config-quickstart -DplatformGroupId=com.redhat.quarkus.platform -DplatformVersion=3.15.3.SP1-redhat-00002 -DclassName="org.acme.config.GreetingResource" -Dpath="/greeting" cd config-quickstart
<property_name>=<value>
greeting.message=hello greeting.name=quarkus
package org.acme.config; import java.util.Optional; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import org.eclipse.microprofile.config.inject.ConfigProperty; @Path("/greeting") public class GreetingResource { String message; Optional<String> name; String suffix; @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return message + " " + name.orElse("world") + suffix; } }
package org.acme.config; import java.util.Optional; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import org.eclipse.microprofile.config.inject.ConfigProperty; @Path("/greeting") public class GreetingResource { @ConfigProperty(name = "greeting.message") 1 String message; @ConfigProperty(name = "greeting.suffix", defaultValue="!") 2 String suffix; @ConfigProperty(name = "greeting.name") Optional<String> name; 3 @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return message + " " + name.orElse("world") + suffix; } }
./mvnw quarkus:dev
curl http://localhost:8080/greeting
hello quarkus!
package org.acme.config; import io.quarkus.test.junit.QuarkusTest; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.is; @QuarkusTest public class GreetingResourceTest { @Test public void testHelloEndpoint() { given() .when().get("/greeting") .then() .statusCode(200) .body(is("hello quarkus!")); // Modified line } }
./mvnw quarkus:dev
./mvnw clean package
java -D<property_name>=<value> -jar target/quarkus-app/quarkus-run.jar
java -Dgreeting.suffix=? -jar target/quarkus-app/quarkus-run.jar
export <property_name>=<value> ; java -jar target/quarkus-app/quarkus-run.jar
<PROPERTY_NAME>=<value>
package org.acme.config; import java.util.Optional; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import org.eclipse.microprofile.config.inject.ConfigProperty; @Path("/greeting") public class GreetingResource { @ConfigProperty(name = "greeting.message") String message; @ConfigProperty(name = "greeting.suffix", defaultValue="!") String suffix; @ConfigProperty(name = "greeting.name") Optional<String> name; @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return message + " " + name.orElse("world") + suffix; } }
package org.acme.config; import io.smallrye.config.ConfigMapping; import io.smallrye.config.WithDefault; import java.util.Optional; @ConfigMapping(prefix = "greeting") 1 public interface GreetingConfiguration { String message(); @WithDefault("!") 2 String suffix(); Optional<String> name(); }
package org.acme.config; import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; @Path("/greeting") public class GreetingResource { @Inject GreetingConfiguration config; @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return config.message() + " " + config.name().orElse("world") + config.suffix(); } }
./mvnw quarkus:dev
curl http://localhost:8080/greeting
hello quarkus!
package org.acme.config; import java.util.Optional; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import org.eclipse.microprofile.config.inject.ConfigProperty; @Path("/greeting") public class GreetingResource { @ConfigProperty(name = "greeting.message") String message; @ConfigProperty(name = "greeting.suffix", defaultValue="!") String suffix; @ConfigProperty(name = "greeting.name") Optional<String> name; @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return message + " " + name.orElse("world") + suffix; } }
package org.acme.config; import io.smallrye.config.ConfigMapping; import io.smallrye.config.WithDefault; import java.util.Optional; @ConfigMapping(prefix = "greeting") public interface GreetingConfiguration { String message(); @WithDefault("!") String suffix(); Optional<String> name(); }
package org.acme.config; import io.smallrye.config.ConfigMapping; import io.smallrye.config.WithDefault; import java.util.List; import java.util.Optional; @ConfigMapping(prefix = "greeting") public interface GreetingConfiguration { String message(); @WithDefault("!") String suffix(); Optional<String> name(); ContentConfig content(); interface ContentConfig { Integer prizeAmount(); List<String> recipients(); } }
greeting.message=hello greeting.name=quarkus greeting.content.prize-amount=10 greeting.content.recipients=Jane,John
package org.acme.config; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import jakarta.inject.Inject; @Path("/greeting") public class GreetingResource { @Inject GreetingConfiguration config; @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return config.message() + " " + config.name().orElse("world") + config.suffix() + "\n" + config.content().recipients() + " receive total of candies: " + config.content().prizeAmount(); } }
./mvnw quarkus:dev
curl http://localhost:8080/greeting
hello quarkus! [Jane, John] receive total of candies: 10
@ConfigMapping(prefix = "greeting") public class GreetingConfiguration { @Size(min = 20) public String message; public String suffix = "!"; }
String <variable-name> = ConfigProvider.getConfig().getValue(" <property-name> ", <data-type-class-name> .class);
String message = ConfigProvider.getConfig().getValue("greeting.message",String.class);
Optional<String> <variable-name> = ConfigProvider.getConfig().getOptionalValue(" <property-name> ", <data-type-class-name> .class);
Optional<String> name = ConfigProvider.getConfig().getOptionalValue("greeting.name", String.class);
package org.acme.config; import java.util.Optional; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; import org.eclipse.microprofile.config.inject.ConfigProperty; @Path("/greeting") public class GreetingResource { @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { Config config = ConfigProvider.getConfig(); String message = config.getValue("greeting.message", String.class); String suffix = config.getOptionalValue("greeting.suffix", String.class).orElse("!"); Optional<String> name = config.getOptionalValue("greeting.name", String.class); return message + " " + name.orElse("world") + suffix; } }
remote.host=quarkus.io callable.url=https://USD{remote.host}/
%dev.quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false quarkus.datasource.jdbc.url=jdbc:mysql://remotehost:3306/mydatabase?useSSL=false
%dev.application.server=localhost application.server=remotehost quarkus.datasource.jdbc.url=jdbc:mysql://USD{application.server}:3306/mydatabase?useSSL=false
USD{<property_name>:<default_value>}
quarkus.datasource.jdbc.url=jdbc:mysql://USD{application.server:mysql.db.server}:3306/mydatabase?useSSL=false
USD{<outer_property_name>USD{<inner_property_name>}}
USD{<first_property_name>}USD{<second_property_name>}
remote.host=quarkus.io application.host=USD{HOST:USD{remote.host}}
import io.quarkus.runtime.configuration.ConfigUtils;
LOGGER.infof("The application is starting with profiles `%s`", ConfigUtils.getProfiles());
%<profile>.<property_name>=<value>
quarkus.http.port=9090 %dev.quarkus.http.port=8181
mvn -Dquarkus.profile=<value> quarkus:dev
export QUARKUS_PROFILE=<profile>
./mvnw package -Dquarkus.profile=<profile> java -jar target/quarkus-app/quarkus-run.jar
./mvnw package -Dquarkus.profile=prod-aws java -jar target/quarkus-app/quarkus-run.jar
package org.acme.config; import org.eclipse.microprofile.config.spi.ConfigSource; import org.eclipse.microprofile.config.spi.ConfigSourceProvider; import java.util.List; public class InMemoryConfigSourceProvider implements ConfigSourceProvider { @Override public Iterable<ConfigSource> getConfigSources(ClassLoader classLoader) { return List.of(new InMemoryConfigSource()); } }
package org.acme.config; import org.eclipse.microprofile.config.spi.ConfigSource; import java.util.HashMap; import java.util.Map; import java.util.Set; public class InMemoryConfigSource implements ConfigSource { private static final Map<String, String> configuration = new HashMap<>(); static { configuration.put("my.prop", "1234"); } @Override public int getOrdinal() { 1 return 275; } @Override public Set<String> getPropertyNames() { return configuration.keySet(); } @Override public String getValue(final String propertyName) { return configuration.get(propertyName); } @Override public String getName() { return InMemoryConfigSource.class.getSimpleName(); } }
org.acme.config.InMemoryConfigSourceProvider
@ConfigProperty(name="my.prop") int value;
@GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return message + " " + name.orElse("world") + " " + value; }
./mvnw quarkus:dev
curl http://localhost:8080/greeting
hello world 1234
package org.acme.config; public class MyCustomValue { private final int value; public MyCustomValue(Integer value) { this.value = value; } public int value() { return value; } }
package org.acme.config; import org.eclipse.microprofile.config.spi.Converter; public class MyCustomValueConverter implements Converter<MyCustomValue> { @Override public MyCustomValue convert(String value) { return new MyCustomValue(Integer.valueOf(value)); } }
@ConfigProperty(name="custom") MyCustomValue value;
@GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return message + " " + name.orElse("world") + " - " + value.value(); }
custom=1234
./mvnw quarkus:dev
curl http://localhost:8080/greeting
hello world - 1234
package org.acme.config; import jakarta.annotation.Priority; import org.eclipse.microprofile.config.spi.Converter; @Priority(150) public class AnotherCustomValueConverter implements Converter<MyCustomValue> { @Override public MyCustomValue convert(String value) { return new MyCustomValue(Integer.valueOf(value)); } }
org.acme.config.AnotherCustomValueConverter