Skip to content

Commit

Permalink
feat (jkube-kit/jkube-kit-spring-boot) : Support for Spring Native (e…
Browse files Browse the repository at this point in the history
…clipse-jkube#2138)

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Aug 2, 2023
1 parent 1d10117 commit 21555cc
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package org.eclipse.jkube.kit.common.util;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
Expand Down Expand Up @@ -111,5 +112,27 @@ public static boolean isSpringBootRepackage(JavaProject project) {
.map(e -> e.contains("repackage"))
.orElse(false);
}

public static Plugin getNativePlugin(JavaProject project) {
Plugin plugin = JKubeProjectUtil.getPlugin(project, "org.graalvm.buildtools", "native-maven-plugin");
if (plugin != null) {
return plugin;
}
return JKubeProjectUtil.getPlugin(project, "org.graalvm.buildtools.native", "org.graalvm.buildtools.native.gradle.plugin");
}

public static File getNativeArtifactFile(JavaProject project) {
Plugin plugin = getNativePlugin(project);
String nativeArtifactName = (String) Optional.ofNullable(plugin.getConfiguration())
.map(c -> c.get("imageName"))
.orElse(project.getArtifactId());
for (String location : new String[] {"", "native/nativeCompile/"}) {
File nativeArtifact = new File(project.getBuildDirectory(), location + nativeArtifactName);
if (nativeArtifact.exists()) {
return nativeArtifact;
}
}
return null;
}
}

14 changes: 14 additions & 0 deletions jkube-kit/jkube-kit-spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,18 @@

</dependencies>

<build>
<resources>
<!-- Copy over default images versions defined above -->
<resource>
<directory>src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.springboot.generator;

import org.eclipse.jkube.generator.api.FromSelector;
import org.eclipse.jkube.generator.api.GeneratorConfig;
import org.eclipse.jkube.generator.api.GeneratorContext;
import org.eclipse.jkube.kit.common.Arguments;
import org.eclipse.jkube.kit.common.Assembly;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.AssemblyFileSet;
import org.eclipse.jkube.kit.common.JavaProject;

import java.io.File;
import java.util.List;

import static org.eclipse.jkube.kit.common.util.FileUtil.getRelativePath;

public class NativeGenerator extends AbstractSpringBootNestedGenerator {
private final File nativeBinary;
private final FromSelector fromSelector;

public NativeGenerator(GeneratorContext generatorContext, GeneratorConfig generatorConfig, File nativeBinary) {
super(generatorContext, generatorConfig);
this.nativeBinary = nativeBinary;
fromSelector = new FromSelector.Default(generatorContext, "springboot-native");
}


@Override
public String getFrom() {
return fromSelector.getFrom();
}

@Override
public String getDefaultJolokiaPort() {
return "0";
}

@Override
public String getDefaultPrometheusPort() {
return "0";
}

@Override
public Arguments getBuildEntryPoint() {
return Arguments.builder()
.execArgument("./" + getProject().getArtifactId())
.build();
}

@Override
public String getBuildWorkdir() {
return "/";
}

@Override
public String getTargetDir() {
return "/";
}

@Override
public AssemblyConfiguration createAssemblyConfiguration(List<AssemblyFileSet> defaultFileSets) {
Assembly.AssemblyBuilder assemblyBuilder = Assembly.builder();
final JavaProject project = getProject();
final AssemblyFileSet.AssemblyFileSetBuilder artifactFileSetBuilder = AssemblyFileSet.builder()
.outputDirectory(new File("."))
.directory(getRelativePath(project.getBaseDirectory(), nativeBinary.getParentFile()))
.fileMode("0755");
artifactFileSetBuilder.include(nativeBinary.getName());

assemblyBuilder.fileSets(defaultFileSets);
assemblyBuilder.fileSet(artifactFileSetBuilder.build());

return AssemblyConfiguration.builder()
.targetDir(getTargetDir())
.excludeFinalOutputArtifact(true)
.layer(assemblyBuilder.build())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected String getDefaultWebPort() {

@Override
protected AssemblyConfiguration createAssembly() {
return Optional.ofNullable(nestedGenerator.createAssemblyConfiguration())
return Optional.ofNullable(nestedGenerator.createAssemblyConfiguration(addAdditionalFiles()))
.orElse(super.createAssembly());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
import org.eclipse.jkube.generator.api.GeneratorContext;
import org.eclipse.jkube.kit.common.Arguments;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.AssemblyFileSet;
import org.eclipse.jkube.kit.common.JavaProject;

import java.io.File;
import java.util.List;

import static org.eclipse.jkube.generator.javaexec.JavaExecGenerator.JOLOKIA_PORT_DEFAULT;
import static org.eclipse.jkube.generator.javaexec.JavaExecGenerator.PROMETHEUS_PORT_DEFAULT;
import static org.eclipse.jkube.kit.common.util.SpringBootUtil.getNativeArtifactFile;
import static org.eclipse.jkube.kit.common.util.SpringBootUtil.getNativePlugin;

public interface SpringBootNestedGenerator {
JavaProject getProject();

default AssemblyConfiguration createAssemblyConfiguration() {
default AssemblyConfiguration createAssemblyConfiguration(List<AssemblyFileSet> defaultFileSets) {
return null;
}

Expand All @@ -50,6 +56,12 @@ default Arguments getBuildEntryPoint() {
String getTargetDir();

static SpringBootNestedGenerator from(GeneratorContext generatorContext, GeneratorConfig generatorConfig) {
if (getNativePlugin(generatorContext.getProject()) != null) {
File nativeBinary = getNativeArtifactFile(generatorContext.getProject());
if (nativeBinary != null) {
return new NativeGenerator(generatorContext, generatorConfig, nativeBinary);
}
}
return new FatJarGenerator(generatorContext, generatorConfig);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

# Properties for specifying the default images version to use
# The replacement values are defined in the parent pom.xml as properties

# Upstream images
springboot-native.upstream.s2i=${image.springboot-native.upstream.s2i}
springboot-native.upstream.docker=${image.springboot-native.upstream.docker}
springboot-native.upstream.istag=${image.springboot-native.upstream.istag}
5 changes: 5 additions & 0 deletions jkube-kit/parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
<image.helidon-native.upstream.docker>registry.access.redhat.com/ubi8/ubi-minimal:${version.image.ubi-minimal}</image.helidon-native.upstream.docker>
<image.helidon-native.upstream.istag>registry.access.redhat.com/ubi8/ubi-minimal:${version.image.ubi-minimal}</image.helidon-native.upstream.istag>

<!-- SpringBoot -->
<image.springboot-native.upstream.s2i>registry.access.redhat.com/ubi8/ubi-minimal:${version.image.ubi-minimal}</image.springboot-native.upstream.s2i>
<image.springboot-native.upstream.docker>registry.access.redhat.com/ubi8/ubi-minimal:${version.image.ubi-minimal}</image.springboot-native.upstream.docker>
<image.springboot-native.upstream.istag>registry.access.redhat.com/ubi8/ubi-minimal:${version.image.ubi-minimal}</image.springboot-native.upstream.istag>

<!-- Quarkus -->
<image.quarkus-native.upstream.s2i>quay.io/quarkus/ubi-quarkus-native-binary-s2i:1.0</image.quarkus-native.upstream.s2i>
<image.quarkus-native.upstream.docker>registry.access.redhat.com/ubi8/ubi-minimal:${version.image.ubi-minimal}</image.quarkus-native.upstream.docker>
Expand Down

0 comments on commit 21555cc

Please sign in to comment.