Last active
January 24, 2026 15:25
-
-
Save turing85/49f935d0f76d004e98c05bc87931dd1f to your computer and use it in GitHub Desktop.
dagger.io: build java with testcontainers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "dagger/build-with-mvn-wrapper/internal/dagger" | |
| "strings" | |
| ) | |
| const volumeDockerLib = "docker-lib" | |
| const volumeMavenRepo = "maven-repo" | |
| const pathDockerLib = "/var/lib/docker" | |
| const pathJavaDownload = "java.tar.gz" | |
| const pathJavaHome = "/opt/java" | |
| const pathSrc = "/src" | |
| const pathMavenRepo = "/" + volumeMavenRepo | |
| type BuildWithMvnWrapper struct { | |
| DinDImage string | |
| JdkUrl string | |
| Src *dagger.Directory | |
| } | |
| func New( | |
| // +default="docker.io/cruizba/ubuntu-dind:noble-systemd-28.5.2-r1" | |
| dindImage string, | |
| // +default="https://github.com/graalvm/mandrel/releases/download/mandrel-23.1.9.0-Final/mandrel-java21-linux-amd64-23.1.9.0-Final.tar.gz" | |
| jdkUrl string, | |
| src *dagger.Directory, | |
| ) *BuildWithMvnWrapper { | |
| return &BuildWithMvnWrapper{ | |
| DinDImage: dindImage, | |
| JdkUrl: jdkUrl, | |
| Src: src, | |
| } | |
| } | |
| func (b *BuildWithMvnWrapper) Verify(ctx context.Context) (string, error) { | |
| return b.buildWithMavenWrapper([]string{"verify"}). | |
| Stdout(ctx) | |
| } | |
| func (b *BuildWithMvnWrapper) buildWithMavenWrapper(targets []string) *dagger.Container { | |
| return b.setupJava(b.setupDinD()). | |
| WithMountedCache(pathMavenRepo, dag.CacheVolume(volumeMavenRepo)). | |
| WithMountedDirectory(pathSrc, b.Src). | |
| WithWorkdir(pathSrc). | |
| WithExec( | |
| startDockerAnd(append( | |
| []string{ | |
| "./mvnw", | |
| "--color", "always", | |
| "-Dmaven.repo.local=" + pathMavenRepo, | |
| }, | |
| targets..., | |
| )), | |
| dagger.ContainerWithExecOpts{ | |
| InsecureRootCapabilities: true, | |
| }) | |
| } | |
| func startDockerAnd(exec []string) []string { | |
| return []string{ | |
| "sh", "-c", | |
| `dockerd --host=unix:///var/run/docker.sock --tls=false >/dev/null 2>&1 & | |
| until docker info >/dev/null 2>&1; do sleep 1; done | |
| ` + strings.Join(exec, " "), | |
| } | |
| } | |
| func (b *BuildWithMvnWrapper) setupDinD() *dagger.Container { | |
| return dag.Container(). | |
| From(b.DinDImage). | |
| WithMountedCache( | |
| pathDockerLib, | |
| dag.CacheVolume(volumeDockerLib), | |
| dagger.ContainerWithMountedCacheOpts{ | |
| Sharing: dagger.CacheSharingModePrivate, | |
| }) | |
| } | |
| func (b *BuildWithMvnWrapper) setupJava(container *dagger.Container) *dagger.Container { | |
| return container. | |
| WithWorkdir(pathJavaHome). | |
| WithExec([]string{ | |
| "wget", | |
| "-O", pathJavaDownload, | |
| b.JdkUrl}). | |
| WithExec([]string{"tar", "-xvf", pathJavaDownload, "--strip-components=1"}). | |
| WithEnvVariable("JAVA_HOME", pathJavaHome). | |
| WithEnvVariable( | |
| "PATH", | |
| pathJavaHome+"/bin:${PATH}", | |
| dagger.ContainerWithEnvVariableOpts{Expand: true}) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment