Skip to content

Commit aba0131

Browse files
committed
add full project source-code
0 parents  commit aba0131

File tree

12 files changed

+775
-0
lines changed

12 files changed

+775
-0
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/

.mvn/wrapper/maven-wrapper.properties

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
wrapperVersion=3.3.2
18+
distributionType=only-script
19+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.7/apache-maven-3.9.7-bin.zip

Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Spring boot application Dockerfile with overridable environment variables
2+
3+
FROM maven:3.9.8-eclipse-temurin-17-focal AS builder
4+
COPY . /app
5+
WORKDIR /app
6+
RUN mvn clean verify
7+
8+
FROM eclipse-temurin:17.0.9_9-jre-focal
9+
VOLUME /tmp
10+
WORKDIR /app
11+
COPY --from=builder /app/target/*.jar /app/app.jar
12+
ENTRYPOINT [ "java", "-jar", "/app/app.jar" ]
13+
14+
15+
# Optional environment variables that can be overriden
16+
#ENV APP_PROPERTIES_NAME=overriden-application-name
17+
#ENV APP_ROLES_0=overriden-role-0
18+
#ENV APP_PATHS_0_ROLES_0=overriden-path-0-role-0

README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Spring Boot Overriding Environment Variables Guide
2+
This is an example of overriding the application.yml file with the OS environment variables. `Single` or `Array` environment variables can be overridden.
3+
4+
## Overview
5+
1. This guide will show you how to override environment variables in a Spring Boot application.
6+
2. Single and multiple environment variables will be overridden.
7+
3. When you run a Spring Boot application, the application properties are converted to environment variables.
8+
4. You can override environment variables using the `-D` flag when java -jar is used.
9+
5. You can override environment variables using the `-e` flag when running a Spring Boot application in a Docker container.
10+
6. You can override environment variables using ConfigMaps and Secrets when running a Spring Boot application in a Kubernetes cluster.
11+
7. You can override environment variables using the `application.yml` file from git repository with spring cloud config server.
12+
---
13+
14+
## Our Use Case
15+
4 and 5 are the most common use cases for overriding environment variables in a Spring Boot application. Maybe next time we can talk about the Kubernetes and Spring Cloud Config Server use cases.
16+
17+
---
18+
19+
## Environments Naming Convention
20+
When you run a Spring Boot application, the application properties are converted to environment variable like UPPER_CASE with underscores.
21+
22+
* application.yml file
23+
```yml
24+
app-properties:
25+
name: "Spring Boot Overriding Environment Variables"
26+
version: "0.0.1"
27+
description: "Spring Boot Overriding Environment Variables with application.yml"
28+
author: "Cevheri"
29+
email: "cevheribozoglan@gmail.com"
30+
```
31+
* OS Environment Variable
32+
```shell
33+
APP_PROPERTIES_NAME=overridden-name
34+
APP_PROPERTIES_VERSION=overridden-version
35+
APP_PROPERTIES_DESCRIPTION=overridden-description
36+
APP_PROPERTIES_AUTHOR=overridden-author
37+
APP_PROPERTIES_EMAIL=overridden-email
38+
```
39+
40+
---
41+
42+
## Use Cases
43+
Environment variables are key-value pairs that are set in the operating system.
44+
They are used to configure applications and services.
45+
46+
### java -jar
47+
When you run a Spring Boot application using the `java -jar` command, you can override environment variables using the `-D` flag.
48+
49+
50+
51+
### docker run
52+
When you run a Spring Boot application in a Docker container, you can override environment variables using the `-e` flag.
53+
54+
55+
---
56+
57+
## Build
58+
### Maven Build
59+
```shell
60+
mvn clean install
61+
```
62+
63+
### Build Docker Image
64+
```shell
65+
docker build -t soe:latest .
66+
```
67+
68+
---
69+
70+
## Run
71+
Run the Spring Boot application using the `java -jar` command.
72+
73+
### Java
74+
#### Simple
75+
```shell
76+
java -jar target/soe.jar
77+
```
78+
79+
---
80+
81+
#### Override Environment Variables
82+
```shell
83+
java -jar '-Dapp-properties.name=overridden-name-cevheri' '-Dapp-roles[0]=overridden-role-0-cevheri' '-Dapp-paths[0].roles[4]=overridden-path-0-role-0-cevheri' target/soe.jar
84+
```
85+
### Docker
86+
#### Simple
87+
```shell
88+
docker run -p 8080:8080 soe:latest
89+
```
90+
91+
#### Override Environment Variables
92+
```shell
93+
docker run -p 8080:8080 -e APP_PROPERTIES_NAME=overridden-name-cevheri-docker -e APP_ROLES_0=overridden-role-0-cevheri-docker -e APP_PATHS_0_ROLES_4=overridden-path-0-role-0-cevheri-docker soe:latest
94+
```
95+
96+
97+
### Reference Documentation
98+
99+
For further reference, please consider the following sections:
100+
* https://spring.io/guides/gs/spring-boot
101+
* https://docs.spring.io/spring-boot/reference/features/external-config.html
102+
* https://maven.apache.org/guides/index.html
103+
* https://docs.spring.io/spring-boot/3.3.2/maven-plugin
104+
* https://docs.spring.io/spring-boot/3.3.2/maven-plugin/build-image.html
105+
* https://docs.docker.com/compose/environment-variables/set-environment-variables/

images/default-values.png

241 KB
Loading

images/overrided-values.png

244 KB
Loading

0 commit comments

Comments
 (0)