Skip to content

Commit

Permalink
fix: SpringBoot Native Generator doesn't require a main class
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Sep 8, 2023
1 parent 6ad37f2 commit 03cfc91
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static Plugin getNativePlugin(JavaProject project) {
return JKubeProjectUtil.getPlugin(project, "org.graalvm.buildtools.native", "org.graalvm.buildtools.native.gradle.plugin");
}

public static File getNativeArtifactFile(JavaProject project) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void getNativePlugin_whenGradleNativePluginPresent_thenReturnPlugin() {
}

@Test
void getNativeArtifactFile_whenNativeExecutableNotFound_thenReturnNull(@TempDir File temporaryFolder) throws IOException {
void findNativeArtifactFile_whenNativeExecutableNotFound_thenReturnNull(@TempDir File temporaryFolder) throws IOException {
// Given
JavaProject javaProject = JavaProject.builder()
.artifactId("sample")
Expand All @@ -349,14 +349,14 @@ void getNativeArtifactFile_whenNativeExecutableNotFound_thenReturnNull(@TempDir
.build();

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

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

@Test
void getNativeArtifactFile_whenNativeExecutableInStandardMavenBuildDirectory_thenReturnNativeArtifact(@TempDir File temporaryFolder) throws IOException {
void findNativeArtifactFile_whenNativeExecutableInStandardMavenBuildDirectory_thenReturnNativeArtifact(@TempDir File temporaryFolder) throws IOException {
// Given
File nativeArtifactFile = Files.createFile(temporaryFolder.toPath().resolve("sample")).toFile();
assertThat(nativeArtifactFile.setExecutable(true)).isTrue();
Expand All @@ -370,14 +370,14 @@ void getNativeArtifactFile_whenNativeExecutableInStandardMavenBuildDirectory_the
.build();

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

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

@Test
void getNativeArtifactFile_whenMoreThanOneNativeExecutableInStandardMavenBuildDirectory_thenThrowException(@TempDir File temporaryFolder) throws IOException {
void findNativeArtifactFile_whenMoreThanOneNativeExecutableInStandardMavenBuildDirectory_thenThrowException(@TempDir File temporaryFolder) throws IOException {
// Given
File nativeArtifactFile = Files.createFile(temporaryFolder.toPath().resolve("sample")).toFile();
assertThat(nativeArtifactFile.setExecutable(true)).isTrue();
Expand All @@ -394,12 +394,12 @@ void getNativeArtifactFile_whenMoreThanOneNativeExecutableInStandardMavenBuildDi

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

@Test
void getNativeArtifactFile_whenNativeExecutableInStandardMavenBuildDirectoryAndImageNameOverridden_thenReturnNativeArtifact(@TempDir File temporaryFolder) throws IOException {
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();
Expand All @@ -413,14 +413,14 @@ void getNativeArtifactFile_whenNativeExecutableInStandardMavenBuildDirectoryAndI
.build();

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

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

@Test
void getNativeArtifactFile_whenNativeExecutableInStandardGradleNativeDirectory_thenReturnNativeArtifact(@TempDir File temporaryFolder) throws IOException {
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();
Expand All @@ -435,7 +435,7 @@ void getNativeArtifactFile_whenNativeExecutableInStandardGradleNativeDirectory_t
.build();

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

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

Expand All @@ -37,7 +40,6 @@ public NativeGenerator(GeneratorContext generatorContext, GeneratorConfig genera
fromSelector = new FromSelector.Default(generatorContext, "springboot-native");
}


@Override
public String getFrom() {
return fromSelector.getFrom();
Expand Down Expand Up @@ -70,6 +72,11 @@ 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();
Expand All @@ -89,4 +96,4 @@ public AssemblyConfiguration createAssemblyConfiguration(List<AssemblyFileSet> d
.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
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
import org.eclipse.jkube.kit.common.AssemblyFileSet;
import org.eclipse.jkube.kit.common.JavaProject;

import java.util.Collections;
import java.util.List;
import java.util.Map;

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.isLayeredJar;
import java.io.File;
import static org.eclipse.jkube.kit.common.util.SpringBootUtil.getNativeArtifactFile;
import java.util.function.Function;

import static org.eclipse.jkube.kit.common.util.SpringBootUtil.findNativeArtifactFile;
import static org.eclipse.jkube.kit.common.util.SpringBootUtil.getNativePlugin;

public interface SpringBootNestedGenerator {
Expand Down Expand Up @@ -59,13 +60,11 @@ default Arguments getBuildEntryPoint() {

String getTargetDir();

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

static SpringBootNestedGenerator from(GeneratorContext generatorContext, GeneratorConfig generatorConfig, FatJarDetector.Result fatJarDetectorResult) {
if (getNativePlugin(generatorContext.getProject()) != null) {
File nativeBinary = getNativeArtifactFile(generatorContext.getProject());
File nativeBinary = findNativeArtifactFile(generatorContext.getProject());
if (nativeBinary != null) {
return new NativeGenerator(generatorContext, generatorConfig, nativeBinary);
}
Expand Down
Loading

0 comments on commit 03cfc91

Please sign in to comment.