Skip to content

Commit

Permalink
init for graalvm workshops
Browse files Browse the repository at this point in the history
  • Loading branch information
paulparkinson committed Oct 2, 2024
1 parent 8b2a4c8 commit cce1ecf
Show file tree
Hide file tree
Showing 19 changed files with 728 additions and 2 deletions.
34 changes: 34 additions & 0 deletions graalvm-nativeimage/micronaut/aot-jar.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# AOT configuration properties for jar packaging
# Please review carefully the optimizations enabled below
# Check https://micronaut-projects.github.io/micronaut-aot/latest/guide/ for more details

# Caches environment property values: environment properties will be deemed immutable after application startup.
cached.environment.enabled=true

# Precomputes Micronaut configuration property keys from the current environment variables
precompute.environment.properties.enabled=true

# Converts YAML configuration files to Java configuration
yaml.to.java.config.enabled=true

# Scans for service types ahead-of-time, avoiding classpath scanning at startup
serviceloading.jit.enabled=true

# Scans reactive types at build time instead of runtime
scan.reactive.types.enabled=true

# Deduces the environment at build time instead of runtime
deduce.environment.enabled=true

# Checks of existence of some types at build time instead of runtime
known.missing.types.enabled=true

# Precomputes property sources at build time
sealed.property.source.enabled=true

# The list of service types to be scanned (comma separated)
service.types=io.micronaut.context.env.PropertySourceLoader,io.micronaut.inject.BeanConfiguration,io.micronaut.inject.BeanDefinitionReference,io.micronaut.http.HttpRequestFactory,io.micronaut.http.HttpResponseFactory,io.micronaut.core.beans.BeanIntrospectionReference,io.micronaut.core.convert.TypeConverterRegistrar,io.micronaut.context.env.PropertyExpressionResolver

# A list of types that the AOT analyzer needs to check for existence (comma separated)
known.missing.types.list=io.reactivex.Observable,reactor.core.publisher.Flux,kotlinx.coroutines.flow.Flow,io.reactivex.rxjava3.core.Flowable,io.reactivex.rxjava3.core.Observable,io.reactivex.Single,reactor.core.publisher.Mono,io.reactivex.Maybe,io.reactivex.rxjava3.core.Single,io.reactivex.rxjava3.core.Maybe,io.reactivex.Completable,io.reactivex.rxjava3.core.Completable,io.methvin.watchservice.MacOSXListeningWatchService,io.micronaut.core.async.publisher.CompletableFuturePublisher,io.micronaut.core.async.publisher.Publishers.JustPublisher,io.micronaut.core.async.subscriber.Completable

6 changes: 6 additions & 0 deletions graalvm-nativeimage/micronaut/micronaut-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
applicationType: default
defaultPackage: com.oracle.dev.jdbc.micronaut
testFramework: junit
sourceLanguage: java
buildTool: maven
features: [app-name, data, data-jdbc, flyway, http-client-test, java, java-application, jdbc-hikari, junit, logback, maven, maven-enforcer-plugin, micronaut-aot, micronaut-http-validation, netty-server, oracle-cloud-atp, oracle-cloud-sdk, properties, readme, serialization-jackson, shade, testcontainers]
249 changes: 249 additions & 0 deletions graalvm-nativeimage/micronaut/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.oracle.dev.jdbc</groupId>
<artifactId>micronaut-guide</artifactId>
<version>0.1</version>
<packaging>${packaging}</packaging>
<parent>
<groupId>io.micronaut.platform</groupId>
<artifactId>micronaut-parent</artifactId>
<version>4.5.1</version>
</parent>
<properties>
<packaging>jar</packaging>
<jdk.version>17</jdk.version>
<release.version>17</release.version>
<micronaut.version>4.5.1</micronaut.version>
<micronaut.test.resources.enabled>true</micronaut.test.resources.enabled>
<micronaut.aot.enabled>false</micronaut.aot.enabled>
<micronaut.runtime>netty</micronaut.runtime>
<micronaut.aot.packageName>com.oracle.dev.jdbc.micronaut.aot.generated</micronaut.aot.packageName>
<exec.mainClass>com.oracle.dev.jdbc.micronaut.Application</exec.mainClass>
<micronaut.sourcegen.version>1.2.1</micronaut.sourcegen.version>
<micronaut-maven-plugin.version>4.6.1</micronaut-maven-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-jdbc</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.serde</groupId>
<artifactId>micronaut-serde-jackson</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.sql</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.sourcegen</groupId>
<artifactId>micronaut-sourcegen-annotations</artifactId>
<version>${micronaut.sourcegen.version}</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>io.micronaut.testresources</groupId>
<artifactId>micronaut-test-resources-client</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.oraclecloud</groupId>
<artifactId>micronaut-oraclecloud-atp</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.oraclecloud</groupId>
<artifactId>micronaut-oraclecloud-sdk</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-oracle</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.flyway</groupId>
<artifactId>micronaut-flyway</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.micronaut.maven</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
<configuration>
<configFile>aot-${packaging}.properties</configFile>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
<quickBuild>true</quickBuild>
<!-- https://www.graalvm.org/latest/reference-manual/native-image/guides/specify-class-initialization/ -->
<buildArgs>
<buildArg>--initialize-at-build-time=io.micronaut.flyway.StaticResourceProvider</buildArg>
<buildArg>--initialize-at-build-time=io.micronaut.flyway.StaticResourceProvider$StaticLoadableResource</buildArg>
<buildArg>--initialize-at-build-time=io.netty.buffer.UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeDirectByteBuf</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.ResourceLeakDetector</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.internal.logging.LocationAwareSlf4JLogger</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.ResourceLeakDetector$Level</buildArg>
<buildArg>--initialize-at-build-time=io.netty.buffer.AbstractReferenceCountedByteBuf$1</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.spi.AppenderAttachableImpl</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.util.COWArrayList</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.ConsoleAppender</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.spi.FilterAttachableImpl</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.joran.spi.ConsoleTarget</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.joran.spi.ConsoleTarget$2</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.joran.spi.ConsoleTarget$1</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.classic.encoder.PatternLayoutEncoder</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.classic.PatternLayout</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.classic.Logger</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.internal.CleanerJava9</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.util.Duration</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.subst.Token</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.util.StatusPrinter</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.pattern.parser.Parser</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.status.InfoStatus</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.classic.PatternLayout</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.util.Loader</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.model.processor.ImplicitModelHandler$1</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.internal.StringUtil</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.ResourceLeakDetectorFactory$DefaultResourceLeakDetectorFactory</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.internal.PlatformDependent0</buildArg>
<buildArg>--initialize-at-build-time=org.slf4j.LoggerFactory</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.CoreConstants</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.ResourceLeakDetectorFactory</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.internal.PlatformDependent</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.subst.Parser$1</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.classic.model.processor.LogbackClassicDefaultNestedComponentRules</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.internal.logging.Slf4JLoggerFactory$NopInstanceHolder</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.classic.Level</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.CharsetUtil</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.internal.SystemPropertyUtil</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.status.StatusBase</buildArg>
<buildArg>--initialize-at-build-time=org.slf4j.helpers.SubstituteServiceProvider</buildArg>
<buildArg>--initialize-at-build-time=org.slf4j.helpers.SubstituteLoggerFactory</buildArg>
<buildArg>--initialize-at-build-time=org.slf4j.helpers.NOP_FallbackServiceProvider</buildArg>
<buildArg>--initialize-at-build-time=org.slf4j.helpers.NOPLoggerFactory</buildArg>
<buildArg>--initialize-at-build-time=io.netty.util.internal.logging.Slf4JLoggerFactory</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.model.processor.ChainedModelFilter$1</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.subst.NodeToStringTransformer$1</buildArg>
<buildArg>--initialize-at-build-time=ch.qos.logback.core.model.processor.DefaultProcessor$1</buildArg>
</buildArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
<annotationProcessorPaths combine.self="override">
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>${micronaut.core.version}</version>
</path>
<path>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-processor</artifactId>
<version>${micronaut.data.version}</version>
<exclusions>
<exclusion>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
</exclusion>
</exclusions>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-graal</artifactId>
<version>${micronaut.core.version}</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-validation</artifactId>
<version>${micronaut.core.version}</version>
</path>
<path>
<groupId>io.micronaut.serde</groupId>
<artifactId>micronaut-serde-processor</artifactId>
<version>${micronaut.serialization.version}</version>
<exclusions>
<exclusion>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
</exclusion>
</exclusions>
</path>
<path>
<groupId>io.micronaut.sourcegen</groupId>
<artifactId>micronaut-sourcegen-generator-java</artifactId>
<version>${micronaut.sourcegen.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amicronaut.processing.group=com.oracle.dev.jdbc.micronaut</arg>
<arg>-Amicronaut.processing.module=micronaut-guide</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.oracle.dev.jdbc.micronaut;

import io.micronaut.runtime.Micronaut;

public class Application {

public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.oracle.dev.jdbc.micronaut;

import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.event.StartupEvent;
import io.micronaut.runtime.event.annotation.EventListener;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import jakarta.transaction.Transactional;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;

import javax.sql.DataSource;

import org.flywaydb.core.Flyway;

import com.oracle.dev.jdbc.micronaut.domain.Thing;
import com.oracle.dev.jdbc.micronaut.repository.ThingRepository;

@Singleton
@Requires(notEnv = "test")
@Requires(property = "flyway.datasources.default.enabled", value = "true")
public class DataPopulator {

private final ThingRepository thingRepository;
private final String QUERY = "SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER = 'ADMIN' AND TABLE_NAME = 'THING'";
private DataSource dataSource;
private boolean baselineOnMigrate;
private String baselineVersion;

@Inject
public DataPopulator(ThingRepository thingRepository, DataSource dataSource,
@Property(name = "flyway.datasources.default.baseline-on-migrate") boolean baselineOnMigrate,
@Property(name = "flyway.datasources.default.baseline-version") String baselineVersion)
throws SQLException {
this.thingRepository = thingRepository;
this.dataSource = dataSource;
this.baselineOnMigrate = baselineOnMigrate;
this.baselineVersion = baselineVersion;
}

@EventListener
@Transactional
void init(StartupEvent event) throws SQLException {

// flyway migration
migrateDatabase();

// clear out any existing data
thingRepository.deleteAll();

// add sample data
populateTable();

}

public void migrateDatabase() throws SQLException {

// Configure Flyway
Flyway flyway = Flyway.configure().dataSource(dataSource)
.baselineOnMigrate(baselineOnMigrate).baselineVersion(baselineVersion)
.load();

// Check if the table exists
boolean tableExists = checkTableExists(dataSource.getConnection());
// Migrate if the table does not exist
if (!tableExists) {
flyway.migrate();
}
}

private boolean checkTableExists(Connection connection) {
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(QUERY)) {
return resultSet.next();
} catch (SQLException e) {
throw new RuntimeException("TABLE DOES NOT EXIST", e);
}
}

public void populateTable() {
Thing juarez = new Thing("Juarez");
Thing kuassi = new Thing("Kuassi");
Thing paul = new Thing("Paul");
thingRepository.saveAll(Arrays.asList(juarez, kuassi, paul));
}

}
Loading

0 comments on commit cce1ecf

Please sign in to comment.