Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# using microsoft image as base dev container for best support vscode
FROM mcr.microsoft.com/devcontainers/java:17 AS vsc-java-dev
ENV XDG_CACHE_HOME="/workspace/local/tmp/java-17/.caches" \
XDG_CONFIG_HOME="/workspace/local/tmp/java-17/config" \
HISTFILE="/workspace/local/tmp/.zsh_history" \
MAVEN_OPTS="-Dmaven.repo.local=/workspace/local/tmp/java-17/m2"
# RUN apt update \
# && apt install -y libnss3 libnspr4 libdbus-1-3 libatk1.0-0 \
# libatk-bridge2.0-0 libcups2 libatspi2.0-0 libxcomposite1 \
# libxdamage1 libxfixes3 libxrandr2 libgbm1 libdrm2 libxkbcommon0 \
# libasound2 \
# && apt clean
21 changes: 21 additions & 0 deletions .devcontainer/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
services:
# put other prod container to prod group
# those services will not start with devcontainer
server:
profiles: ["prod"]
be:
container_name: sb-be
build:
dockerfile: .devcontainer/Dockerfile
target: vsc-java-dev
image: sb-be-java
volumes:
# assign tmp/caching folder for dev container to host,
# increase time and performance when we need to rebuild container
- ~/tmp:/workspace/local/tmp
- .:/workspace
command: sleep infinity
environment:
POSTGRES_HOST: host.docker.internal
depends_on:
- postgres
46 changes: 46 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "SpringBoot Java 11",
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// // Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn",
// Configure tool-specific properties.
"customizations": {
"vscode": {
"extensions": [
"naumovs.color-highlight",
"vscjava.vscode-java-pack",
"vmware.vscode-boot-dev-pack"
],
"settings": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit",
"source.fixAll": "explicit"
},
"editor.defaultFormatter": "redhat.java",
"editor.detectIndentation": false,
"files.trimTrailingWhitespace": true,
"java.compile.nullAnalysis.mode": "automatic",
"java.configuration.updateBuildConfiguration": "automatic"
}
}
},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
"dockerComposeFile": [
"../compose.yml",
"./compose.yml"
],
"service": "be",
"workspaceFolder": "/workspace",
"features": {
"ghcr.io/devcontainers/features/java:1": {
"installMaven": "true"
}
}
}
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
local
.devcontainer
.vscode
9 changes: 9 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## DB config optionala
# pg host, default: localhost
POSTGRES_HOST=
# pg port, default: 5432
POSTGRES_PORT=
# pg username, default: postgres
POSTGRES_USERNAME=postgres
# pg password, default: postgres
POSTGRES_PASSWORD=postgres
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,39 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

.env
local/
19 changes: 19 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.7/apache-maven-3.9.7-bin.zip
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM maven:3.9.7-eclipse-temurin-17-alpine AS builder
WORKDIR build
COPY . .
RUN mvn clean package -DskipTests

FROM eclipse-temurin:17-alpine AS layers
WORKDIR layer
COPY --from=builder /build/target/template-0.0.1.jar app.jar
RUN java -Djarmode=layertools -jar app.jar extract

FROM eclipse-temurin:17-jre-alpine AS artifact
WORKDIR /opt/app
RUN addgroup --system appuser && adduser -S -s /usr/sbin/nologin -G appuser appuser
COPY --from=layers /layer/dependencies/ ./
COPY --from=layers /layer/spring-boot-loader/ ./
COPY --from=layers /layer/snapshot-dependencies/ ./
COPY --from=layers /layer/application/ ./
RUN chown -R appuser:appuser /opt/app
USER appuser
HEALTHCHECK --interval=30s --timeout=3s --retries=1 CMD wget -qO- http://localhost:8080/_info | grep name || exit 1
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
MC Spring Boot Template

## Requirements

- Docker CE/Desktop: 20 or above
- Java: 17
- Maven: 3.9

## Preparing

- Run `yarn` to install all dependencies
- Clone `sample.env` to `.env` and provide all required environment variables
```
# minimum env to run for local development
```

## Backend development

1. Local development:

a. Using vscode:
- support OOTB [devcontainer](https://code.visualstudio.com/docs/devcontainers/containers)
- please make sure all extenstions are enabled after Open project in DevContainer and you're all set
- run:

b. IntelliJ:
- Use docker to start postgres local: `docker compose up -d postgres`

- Run: `mvn spring-boot:run` to start application
- Run: `mvn test` to run test case

2. Dockerize & Run standalone:

- Build/Dockerize: `docker compose build server`

- Run standalone:

- `docker compose up -d server`
- with updated code: `docker compose up -d --build server`
> Tips: if the `docker compose up -d` command fails, please run build command at lease one first

After successfully running:

- server hosted at: <http://localhost:8080/>
54 changes: 54 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: spring-boot-template

services:
postgres:
container_name: sb-pg
image: postgres:14-alpine
restart: unless-stopped
deploy:
restart_policy:
condition: on-failure
resources:
limits:
memory: 128Mb
ports:
- '5432:5432'
command: -c shared_buffers=500MB -c fsync=off
healthcheck:
start_period: 5s
test: 'pg_isready'
environment:
POSTGRES_PASSWORD: postgres
volumes:
- sb_postgres_data:/var/lib/postgresql/data
networks:
- sb

server:
container_name: sb-api
platform: linux/amd64
build:
dockerfile: Dockerfile
context: .
target: artifact
ports:
- '8080:8080'
healthcheck:
start_period: 5s
environment:
POSTGRES_HOST: postgres
POSTGRES_POST: 5432
POSTGRES_USERNAME: postgres
POSTGRES_PASSWORD: postgres
networks:
- sb
depends_on:
- postgres


volumes:
sb_postgres_data:
driver: local

networks:
sb:
Loading