[BAEL-4747] Add read me and utils scripts (#11242)

* [BAEL-4747] Create quarkus and spring boot projects

* [BAEL-4747] Add quarkus implementation and fixing native image plugins setup

* Fixing build config

* [BAEL-4747] Add read me and utils scripts
This commit is contained in:
Thiago dos Santos Hora
2021-09-23 11:07:26 +02:00
committed by GitHub
parent ec94759498
commit 7c5b28e5c7
35 changed files with 13681 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
#!/bin/bash
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
docker build -f $SCRIPTPATH/src/main/docker/Dockerfile.jvm -t spring-project:0.1-SNAPSHOT $SCRIPTPATH/.

View File

@@ -0,0 +1,171 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-project</artifactId>
<groupId>com.baeldung</groupId>
<version>0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
<repackage.classifier/>
<spring-native.version>0.10.3</spring-native.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-native</artifactId>
<version>${spring-native.version}</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>${repackage.classifier}</classifier>
<image>
<builder>paketobuildpacks/builder:tiny</builder>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-aot-maven-plugin</artifactId>
<version>${spring-native.version}</version>
<executions>
<execution>
<id>test-generate</id>
<goals>
<goal>test-generate</goal>
</goals>
</execution>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<profiles>
<profile>
<id>native</id>
<properties>
<repackage.classifier>exec</repackage.classifier>
<native-buildtools.version>0.9.3</native-buildtools.version>
</properties>
<dependencies>
<dependency>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>junit-platform-native</artifactId>
<version>${native-buildtools.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native-buildtools.version}</version>
<configuration>
<buildArgs combine.children="append">
<buildArgs>-H:+AllowVMInspection</buildArgs>
</buildArgs>
</configuration>
<executions>
<execution>
<id>test-native</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
<execution>
<id>build-native</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-DspringAot=true -agentlib:native-image-agent=access-filter-file=src/test/resources/access-filter.json,config-merge-dir=target/classes/META-INF/native-image</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@@ -0,0 +1,12 @@
FROM openjdk:11
ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'
COPY --chown=1001 target/spring-project-0.1-SNAPSHOT-exec.jar /spring-app/
WORKDIR /spring-app
EXPOSE 8080
USER 1001
ENTRYPOINT ["java", "-jar", "spring-project-0.1-SNAPSHOT-exec.jar" ]

View File

@@ -0,0 +1,22 @@
version: '3.1'
services:
db:
image: postgres
ports:
- '5432:5432'
environment:
POSTGRES_PASSWORD: example
app:
image: spring-project:0.1-SNAPSHOT
ports:
- '8080:8080'
environment:
DB_URL: r2dbc:postgresql://db:5432/postgres
links:
- "db"
depends_on:
- "db"
networks:
default:
driver: bridge

View File

@@ -0,0 +1,37 @@
package com.baeldung.spring_project;
import com.baeldung.spring_project.domain.ZIPRepo;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.r2dbc.connection.R2dbcTransactionManager;
import org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer;
import org.springframework.r2dbc.connection.init.ResourceDatabasePopulator;
import org.springframework.transaction.ReactiveTransactionManager;
@SpringBootApplication
public class Startup {
public static void main(String[] args) {
SpringApplication.run(Startup.class, args).getBean(ZIPRepo.class).findById("");
}
@Bean
ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {
var initializer = new ConnectionFactoryInitializer();
initializer.setConnectionFactory(connectionFactory);
initializer.setDatabasePopulator(new ResourceDatabasePopulator(new ByteArrayResource((""
+ "DROP TABLE IF EXISTS zipcode;"
+ "CREATE TABLE zipcode (zip VARCHAR(100) PRIMARY KEY, type VARCHAR(255) NULL, city VARCHAR(255) NULL, state VARCHAR(255) NULL, county VARCHAR(255) NULL, timezone VARCHAR(255) NULL);")
.getBytes())));
return initializer;
}
@Bean ReactiveTransactionManager transactionManager(ConnectionFactory connectionFactory) {
return new R2dbcTransactionManager(connectionFactory);
}
}

View File

@@ -0,0 +1,44 @@
package com.baeldung.spring_project;
import com.baeldung.spring_project.domain.ZIPRepo;
import com.baeldung.spring_project.domain.ZipCode;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.function.Supplier;
@RestController
@RequestMapping(value = "/zipcode")
public class ZipCodeApi {
private ZIPRepo zipRepo;
public ZipCodeApi(ZIPRepo zipRepo) {
this.zipRepo = zipRepo;
}
@GetMapping("/{zipcode}")
public Mono<ZipCode> findById(@PathVariable String zipcode) {
return zipRepo.findById(zipcode);
}
@GetMapping("/by_city")
public Flux<ZipCode> postZipCode(@RequestParam String city) {
return zipRepo.findByCity(city);
}
@Transactional
@PostMapping
public Mono<ZipCode> create(@RequestBody ZipCode zipCode) {
return zipRepo.findById(zipCode.getZip()).switchIfEmpty(Mono.defer(createZipCode(zipCode)));
}
private Supplier<Mono<? extends ZipCode>> createZipCode(ZipCode zipCode) {
return () -> {
zipCode.setId(zipCode.getZip());
return zipRepo.save(zipCode);
};
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.spring_project.domain;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;
public interface ZIPRepo extends ReactiveCrudRepository<ZipCode, String> {
@Query("SELECT * FROM zipcode WHERE city = :city")
Flux<ZipCode> findByCity(String city);
}

View File

@@ -0,0 +1,86 @@
package com.baeldung.spring_project.domain;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Table;
@Table(value = "zipcode")
public class ZipCode implements Persistable<String> {
@Id
private String zip;
private String type;
private String city;
private String state;
private String county;
private String timezone;
@Transient
private boolean persisted;
public String getZip() {
return zip;
}
void setZip(String zip) {
this.zip = zip;
this.persisted = true;
}
public void setId(String zip) {
this.zip = zip;
this.persisted = false;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public String getTimezone() {
return timezone;
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
@JsonIgnore
@Override public String getId() {
return zip;
}
@JsonIgnore
@Override public boolean isNew() {
return !persisted;
}
}

View File

@@ -0,0 +1,5 @@
spring.r2dbc.url=${DB_URL:'r2dbc:postgresql://localhost:5432/postgres'}
spring.r2dbc.username=postgres
spring.r2dbc.password=example
spring.r2dbc.pool.enabled=true
spring.r2dbc.pool.maxSize=20

View File

@@ -0,0 +1,13 @@
package com.baeldung.spring_project;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class StartupIT {
@Test
void contextLoads() {
}
}

View File

@@ -0,0 +1,11 @@
{ "rules": [
{"excludeClasses": "org.apache.maven.surefire.**"},
{"excludeClasses": "net.bytebuddy.**"},
{"excludeClasses": "org.apiguardian.**"},
{"excludeClasses": "org.junit.**"},
{"excludeClasses": "org.mockito.**"},
{"excludeClasses": "org.springframework.test.**"},
{"excludeClasses": "org.springframework.boot.test.**"},
{"excludeClasses": "com.example.demo.test.**"}
]
}

View File

@@ -0,0 +1,6 @@
#!/bin/bash
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
$SCRIPTPATH/target/spring-project -XX:+FlightRecorder -XX:StartFlightRecording="filename=$SCRIPTPATH/recording.jfr,name=Profiling spring"

View File

@@ -0,0 +1,6 @@
#!/bin/bash
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
java -jar $SCRIPTPATH/target/spring-project-0.1-SNAPSHOT-exec.jar