Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (jkube-kit/jkube-kit-spring-boot) : Support for Spring Native (#2138) #2320

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Usage:
./scripts/extract-changelog-for-version.sh 1.3.37 5
```
### 1.15-SNAPSHOT
* Fix #2138: Support for Spring Boot Native Image

### 1.14.0 (2023-08-31)
* Fix #1674: SpringBootGenerator utilizes the layered jar if present and use it as Docker layers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,27 @@ public static boolean isLayeredJar(File fatJar) {
throw new IllegalStateException("Failure in inspecting fat jar for layers.idx file", ioException);
}
}

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 findNativeArtifactFile(JavaProject project) {
for (String location : new String[] {"", "native/nativeCompile/"}) {
File nativeArtifactDir = new File(project.getBuildDirectory(), location);
File[] nativeExecutableArtifacts = nativeArtifactDir.listFiles(f -> f.isFile() && f.canExecute());
if (nativeExecutableArtifacts != null && nativeExecutableArtifacts.length > 0) {
if (nativeExecutableArtifacts.length == 1) {
return nativeExecutableArtifacts[0];
}
throw new IllegalStateException("More than one native executable file found in " + nativeArtifactDir.getAbsolutePath());
}
}
return null;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,148 @@ void isLayeredJar_whenJarContainsLayers_thenReturnTrue(@TempDir File temporaryFo
// Then
assertThat(result).isTrue();
}

@Test
void getNativePlugin_whenNoNativePluginPresent_thenReturnNull() {
assertThat(SpringBootUtil.getNativePlugin(JavaProject.builder().build())).isNull();
}

@Test
void getNativePlugin_whenMavenNativePluginPresent_thenReturnPlugin() {
// Given
JavaProject javaProject = JavaProject.builder()
.plugin(Plugin.builder()
.groupId("org.graalvm.buildtools")
.artifactId("native-maven-plugin")
.build())
.build();

// When
Plugin plugin = SpringBootUtil.getNativePlugin(javaProject);

// Then
assertThat(plugin).isNotNull();
}

@Test
void getNativePlugin_whenGradleNativePluginPresent_thenReturnPlugin() {
// Given
JavaProject javaProject = JavaProject.builder()
.plugin(Plugin.builder()
.groupId("org.graalvm.buildtools.native")
.artifactId("org.graalvm.buildtools.native.gradle.plugin")
.build())
.build();

// When
Plugin plugin = SpringBootUtil.getNativePlugin(javaProject);

// Then
assertThat(plugin).isNotNull();
}

@Test
void findNativeArtifactFile_whenNativeExecutableNotFound_thenReturnNull(@TempDir File temporaryFolder) throws IOException {
// Given
JavaProject javaProject = JavaProject.builder()
.artifactId("sample")
.buildDirectory(temporaryFolder)
.plugin(Plugin.builder()
.groupId("org.graalvm.buildtools")
.artifactId("native-maven-plugin")
.build())
.build();

// When
File nativeArtifactFound = SpringBootUtil.findNativeArtifactFile(javaProject);

// Then
assertThat(nativeArtifactFound).isNull();
}

@Test
void findNativeArtifactFile_whenNativeExecutableInStandardMavenBuildDirectory_thenReturnNativeArtifact(@TempDir File temporaryFolder) throws IOException {
// Given
File nativeArtifactFile = Files.createFile(temporaryFolder.toPath().resolve("sample")).toFile();
assertThat(nativeArtifactFile.setExecutable(true)).isTrue();
JavaProject javaProject = JavaProject.builder()
.artifactId("sample")
.buildDirectory(temporaryFolder)
.plugin(Plugin.builder()
.groupId("org.graalvm.buildtools")
.artifactId("native-maven-plugin")
.build())
.build();

// When
File nativeArtifactFound = SpringBootUtil.findNativeArtifactFile(javaProject);

// Then
assertThat(nativeArtifactFound).hasName("sample");
}

@Test
void findNativeArtifactFile_whenMoreThanOneNativeExecutableInStandardMavenBuildDirectory_thenThrowException(@TempDir File temporaryFolder) throws IOException {
// Given
File nativeArtifactFile = Files.createFile(temporaryFolder.toPath().resolve("sample")).toFile();
assertThat(nativeArtifactFile.setExecutable(true)).isTrue();
File nativeArtifactFile2 = Files.createFile(temporaryFolder.toPath().resolve("sample2")).toFile();
assertThat(nativeArtifactFile2.setExecutable(true)).isTrue();
JavaProject javaProject = JavaProject.builder()
.artifactId("sample")
.buildDirectory(temporaryFolder)
.plugin(Plugin.builder()
.groupId("org.graalvm.buildtools")
.artifactId("native-maven-plugin")
.build())
.build();

// When + Then
assertThatIllegalStateException()
.isThrownBy(() -> SpringBootUtil.findNativeArtifactFile(javaProject))
.withMessage("More than one native executable file found in " + temporaryFolder.getAbsolutePath());
}

@Test
void findNativeArtifactFile_whenNativeExecutableInStandardMavenBuildDirectoryAndImageNameOverridden_thenReturnNativeArtifact(@TempDir File temporaryFolder) throws IOException {
// Given
File nativeArtifactFile = Files.createFile(temporaryFolder.toPath().resolve("custom-native-name")).toFile();
assertThat(nativeArtifactFile.setExecutable(true)).isTrue();
JavaProject javaProject = JavaProject.builder()
.artifactId("sample")
.buildDirectory(temporaryFolder)
.plugin(Plugin.builder()
.groupId("org.graalvm.buildtools")
.artifactId("native-maven-plugin")
.build())
.build();

// When
File nativeArtifactFound = SpringBootUtil.findNativeArtifactFile(javaProject);

// Then
assertThat(nativeArtifactFound).hasName("custom-native-name");
}

@Test
void findNativeArtifactFile_whenNativeExecutableInStandardGradleNativeDirectory_thenReturnNativeArtifact(@TempDir File temporaryFolder) throws IOException {
// Given
Files.createDirectories(temporaryFolder.toPath().resolve("native").resolve("nativeCompile"));
File nativeArtifactFile = Files.createFile(temporaryFolder.toPath().resolve("native").resolve("nativeCompile").resolve("sample")).toFile();
assertThat(nativeArtifactFile.setExecutable(true)).isTrue();
JavaProject javaProject = JavaProject.builder()
.artifactId("sample")
.buildDirectory(temporaryFolder)
.plugin(Plugin.builder()
.groupId("org.graalvm.buildtools.native")
.artifactId("org.graalvm.buildtools.native.gradle.plugin")
.build())
.build();

// When
File nativeArtifactFound = SpringBootUtil.findNativeArtifactFile(javaProject);

// Then
assertThat(nativeArtifactFound).hasName("sample");
}
}
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
Expand Up @@ -15,9 +15,15 @@

import org.eclipse.jkube.generator.api.GeneratorConfig;
import org.eclipse.jkube.generator.api.GeneratorContext;
import org.eclipse.jkube.generator.api.GeneratorMode;
import org.eclipse.jkube.generator.javaexec.JavaExecGenerator;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;
import org.eclipse.jkube.kit.common.util.SpringBootUtil;

import java.util.Map;
import java.util.function.Function;

public abstract class AbstractSpringBootNestedGenerator implements SpringBootNestedGenerator {

Expand All @@ -44,6 +50,22 @@ public String getTargetDir() {
return generatorConfig.get(JavaExecGenerator.Config.TARGET_DIR);
}

@Override
public Map<String, String> getEnv(Function<Boolean, Map<String, String>> javaExecEnvSupplier, boolean prePackagePhase) {
final Map<String, String> res = javaExecEnvSupplier.apply(prePackagePhase);
if (generatorContext.getGeneratorMode() == GeneratorMode.WATCH) {
// adding dev tools token to env variables to prevent override during recompile
final String secret = SpringBootUtil.getSpringBootApplicationProperties(
SpringBootUtil.getSpringBootActiveProfile(getProject()),
JKubeProjectUtil.getClassLoader(getProject()))
.getProperty(SpringBootUtil.DEV_TOOLS_REMOTE_SECRET);
if (secret != null) {
res.put(SpringBootUtil.DEV_TOOLS_REMOTE_SECRET_ENV, secret);
}
}
return res;
}

protected KitLogger getLogger() {
return generatorContext.getLogger();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

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

Expand All @@ -48,8 +48,10 @@ public Arguments getBuildEntryPoint() {
}

@Override
public Map<String, String> getEnv() {
return Collections.singletonMap("JAVA_MAIN_CLASS", MAIN_CLASS);
public Map<String, String> getEnv(Function<Boolean, Map<String, String>> javaExecEnvSupplier, boolean prePackagePhase) {
final Map<String, String> res = super.getEnv(javaExecEnvSupplier, prePackagePhase);
res.put("JAVA_MAIN_CLASS", MAIN_CLASS);
return res;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

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("./" + nativeBinary.getName())
.build();
}

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

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

@Override
public Map<String, String> getEnv(Function<Boolean, Map<String, String>> javaExecEnvSupplier, boolean prePackagePhase) {
return Collections.emptyMap();
}

@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 @@ -84,19 +84,7 @@ public List<ImageConfiguration> customize(List<ImageConfiguration> configs, bool

@Override
protected Map<String, String> getEnv(boolean prePackagePhase) {
Map<String, String> res = super.getEnv(prePackagePhase);
if (getContext().getGeneratorMode() == GeneratorMode.WATCH) {
// adding dev tools token to env variables to prevent override during recompile
final String secret = SpringBootUtil.getSpringBootApplicationProperties(
SpringBootUtil.getSpringBootActiveProfile(getProject()),
JKubeProjectUtil.getClassLoader(getProject()))
.getProperty(SpringBootUtil.DEV_TOOLS_REMOTE_SECRET);
if (secret != null) {
res.put(SpringBootUtil.DEV_TOOLS_REMOTE_SECRET_ENV, secret);
}
}
res.putAll(nestedGenerator.getEnv());
return res;
return nestedGenerator.getEnv(ppp -> super.getEnv(ppp), prePackagePhase);
}

@Override
Expand Down
Loading