How to Read Properties From Pom.xml in Java
69. Backdrop & configuration
69.1 Automatically expand properties at build time
Rather than hardcoding some properties that are also specified in your project's build configuration, y'all can automatically aggrandize them using the existing build configuration instead. This is possible in both Maven and Gradle.
69.1.one Automatic belongings expansion using Maven
You tin can automatically aggrandize properties from the Maven project using resource filtering. If you utilize the leap-boot-starter-parent yous can and so refer to your Maven 'project properties' via @[e-mail protected] placeholders, eastward.g.
app.encoding[email protected]@ app.java.version[email protected]@
| Tip |
|---|---|
| The |
If you don't use the starter parent, in your pom.xml you need (inside the <build/> element):
<resources> <resource> <directory>src/chief/resources</directory> <filtering>true</filtering> </resource> </resources>
and (inside <plugins/>):
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <delimiters> <delimiter>@</delimiter> </delimiters> <useDefaultDelimiters>false</useDefaultDelimiters> </configuration> </plugin>
| Notation |
|---|---|
| The |
69.1.2 Automated property expansion using Gradle
You tin can automatically expand backdrop from the Gradle projection past configuring the Java plugin's processResources task to do and then:
processResources { expand(project.backdrop) } You lot can then refer to your Gradle project's backdrop via placeholders, due east.one thousand.
app.name=${name} app.clarification=${description}
| Note |
|---|---|
| Gradle's |
69.2 Externalize the configuration of SpringApplication
A SpringApplication has bean properties (mainly setters) so you tin employ its Coffee API as y'all create the application to modify its behavior. Or you lot can externalize the configuration using properties in spring.master.*. E.g. in application.backdrop you might have.
leap.main.web-environment=false spring.principal.imprint-mode=off
and then the Spring Boot banner will non exist printed on startup, and the application will not be a web application.
| Annotation |
|---|---|
| The instance above also demonstrates how flexible binding allows the use of underscores ( |
Properties divers in external configuration overrides the values specified via the Java API with the notable exception of the sources used to create the ApplicationContext. Let'due south consider this awarding
new SpringApplicationBuilder() .bannerMode(Banner.Style.OFF) .sources(demo.MyApp.class) .run(args);
used with the post-obit configuration:
spring.main.sources=com.summit.Config,com.peak.ExtraConfig spring.main.banner-mode=console
The actual awarding will now show the banner (every bit overridden by configuration) and employ three sources for the ApplicationContext (in that order): demo.MyApp, com.peak.Config, com.acme.ExtraConfig.
69.3 Change the location of external properties of an application
By default properties from dissimilar sources are added to the Spring Environment in a defined society (see Chapter 24, Externalized Configuration in the 'Spring Boot features' section for the exact club).
A nice way to augment and alter this is to add @PropertySource annotations to your application sources. Classes passed to the SpringApplication static convenience methods, and those added using setSources() are inspected to see if they have @PropertySources, and if they exercise, those backdrop are added to the Surround early enough to exist used in all phases of the ApplicationContext lifecycle. Properties added in this way have lower priority than any added using the default locations (due east.thou. application.properties), system properties, environment variables or the command line.
Y'all can also provide System backdrop (or environs variables) to change the behavior:
-
spring.config.name(SPRING_CONFIG_NAME), defaults toapplicationas the root of the file name. -
leap.config.location(SPRING_CONFIG_LOCATION) is the file to load (e.g. a classpath resource or a URL). A separateEnvironmentproperty source is gear up for this certificate and it can exist overridden by system backdrop, environment variables or the command line.
No matter what you set in the environment, Spring Boot will always load application.properties equally described above. If YAML is used then files with the '.yml' extension are also added to the list by default.
Bound Boot logs the configuration files that are loaded at DEBUG level and the candidates information technology has not found at TRACE level.
Run across ConfigFileApplicationListener for more detail.
69.four Utilise 'curt' command line arguments
Some people like to use (for case) --port=9000 instead of --server.port=9000 to set configuration properties on the command line. You tin easily enable this past using placeholders in application.properties, e.one thousand.
server.port=${port:8080} | Tip |
|---|---|
| If you are inheriting from the |
| Note |
|---|---|
| In this specific case the port binding will piece of work in a PaaS surroundings like Heroku and Cloud Foundry, since in those two platforms the |
69.5 Apply YAML for external backdrop
YAML is a superset of JSON and as such is a very convenient syntax for storing external properties in a hierarchical format. Eastward.k.
jump: application: name: cruncher datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost/test server: port: 9000
Create a file called application.yml and stick it in the root of your classpath, and likewise add together snakeyaml to your dependencies (Maven coordinates org.yaml:snakeyaml, already included if yous use the jump-boot-starter). A YAML file is parsed to a Java Map<String,Object> (like a JSON object), and Spring Boot flattens the map so that it is 1-level deep and has flow-separated keys, a lot like people are used to with Properties files in Coffee.
The example YAML above corresponds to an application.properties file
spring.application.name=cruncher bound.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost/examination server.port=9000
Run across Section 24.6, "Using YAML instead of Properties" in the 'Jump Boot features' section for more information about YAML.
69.6 Fix the active Bound profiles
The Spring Environment has an API for this, simply commonly yous would ready a System holding (spring.profiles.active) or an OS environment variable (SPRING_PROFILES_ACTIVE). E.thousand. launch your application with a -D argument (remember to put it earlier the main class or jar annal):
$ java -jar -Dspring.profiles.active=production demo-0.0.i-SNAPSHOT.jar
In Bound Boot you can likewise ready the active contour in awarding.backdrop, e.g.
jump.profiles.active=production A value set this mode is replaced by the Arrangement property or environment variable setting, simply not by the SpringApplicationBuilder.profiles() method. Thus the latter Java API can be used to broaden the profiles without changing the defaults.
Run into Chapter 25, Profiles in the 'Spring Boot features' section for more than information.
69.7 Alter configuration depending on the surround
A YAML file is actually a sequence of documents separated by --- lines, and each document is parsed separately to a flattened map.
If a YAML document contains a spring.profiles central, then the profiles value (comma-separated list of profiles) is fed into the Spring Environment.acceptsProfiles() and if any of those profiles is active that document is included in the final merge (otherwise non).
Example:
server: port: 9000 spring: profiles: development server: port: 9001 spring: profiles: production server: port: 0
In this example the default port is 9000, but if the Spring profile 'development' is active then the port is 9001, and if 'production' is active then information technology is 0.
The YAML documents are merged in the order they are encountered (and then later values override before ones).
To do the same thing with properties files you tin can utilize application-${profile}.properties to specify contour-specific values.
69.8 Notice built-in options for external properties
Spring Kicking binds external properties from application.backdrop (or .yml) (and other places) into an awarding at runtime. In that location is not (and technically cannot be) an exhaustive list of all supported properties in a unmarried location because contributions can come from additional jar files on your classpath.
A running awarding with the Actuator features has a configprops endpoint that shows all the bound and bindable properties available through @ConfigurationProperties.
The appendix includes an awarding.properties instance with a list of the nearly common properties supported past Spring Boot. The definitive list comes from searching the source code for @ConfigurationProperties and @Value annotations, as well as the occasional use of RelaxedPropertyResolver.
Source: https://docs.spring.io/spring-boot/docs/1.4.x/reference/html/howto-properties-and-configuration.html
0 Response to "How to Read Properties From Pom.xml in Java"
Post a Comment