Skip to content

Commit

Permalink
Merge pull request #61 from cl-a-us/master
Browse files Browse the repository at this point in the history
Udate spring and provide spring native support with codespace
  • Loading branch information
MaxStroh authored Nov 12, 2024
2 parents db9b0d9 + 1332b7f commit 2a51144
Show file tree
Hide file tree
Showing 24 changed files with 258 additions and 83 deletions.
28 changes: 28 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// see https://github.com/microsoft/vscode-remote-try-java for detailed example on vscode remote java project
{
"name": "dev-container-with-graalvm",
// java images can be found here: https://mcr.microsoft.com/en-us/artifact/mar/devcontainers/java/about
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"features": {
"ghcr.io/devcontainers/features/java:1": {
// version is installed via sdkman
// see https://sdkman.io/jdks#graalce and https://www.graalvm.org/downloads/ for version
"version": "21-graalce",
"installMaven": "true",
"mavenVersion": "3.8.6",
"installGradle": "false"
}
},
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
"settings": {},
"extensions": [
"streetsidesoftware.code-spell-checker"
]
}
}
// Use 'postCreateCommand' to run commands after the container is created.
, "postCreateCommand": "java -version"
}
40 changes: 14 additions & 26 deletions .github/workflows/build_push_to_ecr.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
name: build release and push to ECR
on:
on:
release:
types: [prereleased, released]
jobs:
build_and_push:
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4


- name: Login to Public ECR
uses: docker/login-action@v1
with:
Expand All @@ -19,32 +17,23 @@ jobs:
password: ${{ secrets.AWS_PROD_ECR_VIADEE_SECRET_ACCESS_KEY }}
env:
AWS_REGION: us-east-1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Set up JDK 1.11
uses: actions/setup-java@v3
with:
distribution: 'adopt' # See 'Supported distributions' for available options
java-version: '11'

- name: Cache local Maven repository
uses: actions/cache@v2

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
java-version: "21"
distribution: "adopt" # See 'Supported distributions' for available options
cache: 'maven'

- name: Maven package
run: mvn package -B
run: mvn -B package


-
name: Docker meta
- name: Docker meta
id: meta
uses: docker/metadata-action@v3
uses: docker/metadata-action@v5
with:
images: public.ecr.aws/m4k8r3n4/k8s-demo-app
tags: |
Expand All @@ -54,11 +43,10 @@ jobs:
type=semver,pattern={{major}}.{{minor}}
- name: Build and push Docker images
uses: docker/build-push-action@v2.10.0
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

labels: ${{ steps.meta.outputs.labels }}
27 changes: 14 additions & 13 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

name: Java CI with Maven

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'adopt'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: "21"
distribution: "liberica" # See 'Supported distributions' for available options
cache: 'maven'

- name: Maven package
run: mvn -B package
33 changes: 33 additions & 0 deletions .github/workflows/native_build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

name: Java CI Native Build with Maven

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: "21"
distribution: "graalvm" # See 'Supported distributions' for available options
cache: 'maven'

- name: Maven native build
run: mvn package -Pnative native:compile
- name: 'Upload Artifact'
uses: actions/upload-artifact@v4
with:
name: native-build
path: target/k8s-demo-app
retention-days: 5
52 changes: 52 additions & 0 deletions .github/workflows/native_build_push_to_ecr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: build release and push to ECR
on:
release:
types: [prereleased, released]
jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Login to Public ECR
uses: docker/login-action@v1
with:
registry: public.ecr.aws
username: ${{ secrets.AWS_PROD_ECR_VIADEE_ACCESS_KEY_ID }}
password: ${{ secrets.AWS_PROD_ECR_VIADEE_SECRET_ACCESS_KEY }}
env:
AWS_REGION: us-east-1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: "21"
distribution: "graalvm" # See 'Supported distributions' for available options
cache: 'maven'

- name: Maven native build
run: mvn package -Pnative native:compile

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: public.ecr.aws/m4k8r3n4/k8s-demo-app-native
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
- name: Build and push Docker images
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk:11-slim
FROM openjdk:17-slim
COPY target/k8s-demo-app.jar app.jar
RUN groupadd -g 1000 appuser && \
useradd -u 1000 -g 1000 appuser
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.Multistage
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM maven:3.6-jdk-11-slim as build_container
FROM maven:3.8-openjdk-17-slim as build_container

WORKDIR /build
COPY pom.xml .
Expand All @@ -8,7 +8,7 @@ COPY . /usr/src/build
WORKDIR /usr/src/build
RUN mvn package -B

FROM openjdk:11-slim
FROM openjdk:17-slim
COPY --from=build_container /usr/src/build/target/k8s-demo-app.jar app.jar
RUN groupadd -g 1000 appuser && \
useradd -u 1000 -g 1000 appuser
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,20 @@ Or build and run with maven locally:

Or just do a local docker build: `docker build -t imagename .`
Then you can run the container locally.


## Native Build in github Codespaces

```shell
mvn clean
# 'package' contains step 'process-aot' which is needed for native:compile
mvn package -Pnative
mvn native:compile
# If "[1/8] Initializing..." fails, it might be due to a lag of resources. Try increasing the VM resources.
```

### start native build
```shell
./target/k8s-demo-app
./target/k8s-demo-app --spring.profiles.active=red
```
22 changes: 13 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>de.viadee.k8s.testapp</groupId>
<artifactId>k8s-demo-app</artifactId>
<version>0.2.0-SNAPSHOT</version>
<version>0.2.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>k8s-demo-app</name>
Expand All @@ -14,14 +14,15 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<version>3.3.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
<java.version>21</java.version>
<!-- <hibernate.version>6.4.2.Final</hibernate.version> https://github.com/spring-projects/spring-boot/issues/39744 -->
</properties>

<dependencies>
Expand Down Expand Up @@ -54,21 +55,24 @@
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
<version>0.54</version>
<!-- On update replace version in htmls (needed for native )-->
<version>5.3.3</version>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>

</configuration>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.viadee.k8s.testapp.config;

import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.annotation.RegisterReflectionForBinding;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.HashMap;
import java.util.Map;


@Configuration
@RegisterReflectionForBinding(classes = {HashMap.class})
@ImportRuntimeHints(NativeReflectionRuntimeHintConfig.NativeRuntimeHints.class)
@EnableWebMvc
public class NativeReflectionRuntimeHintConfig {

static class NativeRuntimeHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {

// especially for rendering k8s_env.html
hints.reflection().registerType(Map.Entry.class, MemberCategory.values());
// hints.reflection().registerType(HashMap.class, MemberCategory.values());
// Innere Klassen
hints.reflection().registerTypeIfPresent(classLoader, "java.util.HashMap$Node"
, MemberCategory.values());

hints.resources().registerPattern("META-INF/resources/webjars/bootstrap/5.3.3/css/bootstrap.min.css");
hints.resources().registerPattern("META-INF/resources/webjars/bootstrap/5.3.3/js/bootstrap.min.js");
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/de/viadee/k8s/testapp/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.viadee.k8s.testapp.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}

}
Loading

0 comments on commit 2a51144

Please sign in to comment.