Skip to content

Commit

Permalink
Add simple java build client project
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Nov 20, 2024
1 parent 0f6b5b8 commit 0305b17
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,19 @@ int exitCode = BuildConfig.builder()
## Error Handling
If the build fails for any reason, a `BuildpackException` will be thrown, this is a RuntimeException, so does not need an explicit catch block. There are many many ways in which a build can fail, from something environmental, like docker being unavailable, to build related issues, like the chosen builder image requiring a platformlevel not implemented by this library.
If the build fails for any reason, a `BuildpackException` will be thrown, this is a RuntimeException, so does not need an explicit catch block. There are many ways in which a build can fail, from something environmental, like docker being unavailable, to build related issues, like the chosen builder image requiring a platformlevel not implemented by this library.
## Using the buildpack client with jbang
## Using the buildpack client
### Maven exec:java
To play with the Java Buildpack client & DSL, use the following simple java project: [samples/build-me](samples/build-me)
```bash
export PROJECT_PATH="$HOME/path_to/java-buildpack-client/samples/hello-quarkus"
mvn compile exec:java
```
### Jbang
The easiest way to invoke arbitrary java code, without much hassle is by using [jbang](https://www.jbang.dev/).
Expand Down
16 changes: 16 additions & 0 deletions samples/build-me/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

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

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
14 changes: 14 additions & 0 deletions samples/build-me/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Buildpack Builder config example

Example of a java project able to build a Quarkus, spring Boot, ... project
using the DSL `BuildConfig.builder()`

To use it, just configure the following env var pointing to a project to be built as a container image

```bash
exort PROJECT_PATH=<JAVA_PROJECT>
```
and execute this command in a terminal:
```bash
mvn compile exec:java
```
74 changes: 74 additions & 0 deletions samples/build-me/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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>dev.snowdrop</groupId>
<artifactId>build-me</artifactId>
<name>Snowdrop :: Java Buildpack Client :: Samples :: Build me</name>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>dev.snowdrop</groupId>
<artifactId>buildpack-client</artifactId>
<version>0.0.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>

<!-- docker -->
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-core</artifactId>
<version>3.4.0</version>
</dependency>

<!-- logging -->
<!--
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
</dependency>
-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<!-- `-proc:only`: Explicitly enable annotation processing. -->
<compilerArgument>-proc:full</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<mainClass>dev.snowdrop.BuildMe</mainClass>
</configuration>
</plugin>
</plugins>
</build>

</project>
61 changes: 61 additions & 0 deletions samples/build-me/src/main/java/dev/snowdrop/BuildMe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package dev.snowdrop;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.github.dockerjava.api.DockerClient;
import dev.snowdrop.buildpack.*;
import dev.snowdrop.buildpack.config.*;

import static dev.snowdrop.buildpack.docker.DockerClientUtils.getDockerClient;

public class BuildMe {

public static void main(String... args) {

System.setProperty("org.slf4j.simpleLogger.log.dev.snowdrop.buildpack","debug");
System.setProperty("org.slf4j.simpleLogger.log.dev.snowdrop.buildpack.docker","debug");
System.setProperty("org.slf4j.simpleLogger.log.dev.snowdrop.buildpack.lifecycle","debug");
System.setProperty("org.slf4j.simpleLogger.log.dev.snowdrop.buildpack.lifecycle.phases","debug");

String REGISTRY_USERNAME = System.getenv("REGISTRY_USERNAME");
String REGISTRY_PASSWORD = System.getenv("REGISTRY_PASSWORD");
String REGISTRY_SERVER = System.getenv("REGISTRY_SERVER");

String PROJECT_PATH = System.getenv("PROJECT_PATH");
File filePath = new File(PROJECT_PATH);

Map<String, String> envMap = new HashMap<>();
envMap.put("BP_JVM_VERSION", "21");

DockerClient client = getDockerClient();
client.authConfig()
.withUsername(REGISTRY_USERNAME)
.withPassword(REGISTRY_PASSWORD)
.withRegistryAddress(REGISTRY_SERVER);

int exitCode = BuildConfig.builder()
.withBuilderImage(new ImageReference("paketocommunity/builder-ubi-base:latest"))
.withOutputImage(new ImageReference("quay.io/snowdrop/my-quarkus-app"))
.withNewPlatformConfig()
.withEnvironment(Map.ofEntries(
Map.entry("BP_JVM_VERSION", "21")
// Map.entry("BP_MAVEN_BUILT_ARTIFACT","target/quarkus-app/lib/ target/quarkus-app/*.jar target/quarkus-app/app/ target/quarkus-app/quarkus"),
// Map.entry("CNB_LOG_LEVEL","trace")
))
.endPlatformConfig()
.withNewDockerConfig()
.withDockerClient(client)
.endDockerConfig()
.withNewLogConfig()
.withLogger(new SystemLogger())
.withLogLevel("debug")
.and()
.addNewFileContentApplication(filePath)
.build()
.getExitCode();

System.exit(exitCode);
}
}

0 comments on commit 0305b17

Please sign in to comment.