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

[issue #4175] SpringBootExplodedProcessor now uses JAR's Main-Class w… #4177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -135,14 +136,23 @@ public List<FileEntriesLayer> createLayers() throws IOException {
}

@Override
public ImmutableList<String> computeEntrypoint(List<String> jvmFlags) {
ImmutableList.Builder<String> entrypoint = ImmutableList.builder();
entrypoint.add("java");
entrypoint.addAll(jvmFlags);
entrypoint.add("-cp");
entrypoint.add(JarLayers.APP_ROOT.toString());
entrypoint.add("org.springframework.boot.loader.JarLauncher");
return entrypoint.build();
public ImmutableList<String> computeEntrypoint(List<String> jvmFlags) throws IOException {
try (JarFile jarFile = new JarFile(jarPath.toFile())) {
String mainClass =
jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
if (mainClass == null) {
throw new IllegalArgumentException(
"`Main-Class:` attribute for an application main class not defined in the input JAR's "
+ "manifest (`META-INF/MANIFEST.MF` in the JAR).");
}
ImmutableList.Builder<String> entrypoint = ImmutableList.builder();
entrypoint.add("java");
entrypoint.addAll(jvmFlags);
entrypoint.add("-cp");
entrypoint.add(JarLayers.APP_ROOT.toString());
entrypoint.add(mainClass);
return entrypoint.build();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.tools.jib.cli.jar;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath;
import com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer;
Expand All @@ -43,6 +44,8 @@ public class SpringBootExplodedProcessorTest {
private static final String SPRING_BOOT_LAYERED_WITH_ALL_EMPTY_LAYERS_LISTED =
"jar/spring-boot/springboot_layered_allEmptyLayers.jar";
private static final String SPRING_BOOT_NOT_LAYERED = "jar/spring-boot/springboot_notLayered.jar";
private static final String SPRING_BOOT_NO_MANIFEST =
"jar/spring-boot/springboot_noMainClass_in_manifest.jar";
private static final Integer JAR_JAVA_VERSION = 0; // any value

@Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder();
Expand Down Expand Up @@ -239,27 +242,53 @@ public void testCreateLayers_nonLayered() throws IOException, URISyntaxException
}

@Test
public void testComputeEntrypoint() {
public void testComputeEntrypoint() throws URISyntaxException, IOException {
Path sampleSpringBootJar = Paths.get(Resources.getResource(SPRING_BOOT_LAYERED).toURI());
SpringBootExplodedProcessor bootProcessor =
new SpringBootExplodedProcessor(
Paths.get("ignored"), Paths.get("ignored"), JAR_JAVA_VERSION);
sampleSpringBootJar, Paths.get("ignored"), JAR_JAVA_VERSION);
ImmutableList<String> actualEntrypoint = bootProcessor.computeEntrypoint(new ArrayList<>());
assertThat(actualEntrypoint)
.isEqualTo(
ImmutableList.of("java", "-cp", "/app", "org.springframework.boot.loader.JarLauncher"));
ImmutableList.of(
"java", "-cp", "/app", "org.springframework.boot.loader.launch.JarLauncher"));
}

@Test
public void testComputeEntrypoint_withJvmFlags() {
public void testComputeEntrypoint_withJvmFlags() throws URISyntaxException, IOException {
Path sampleSpringBootJar = Paths.get(Resources.getResource(SPRING_BOOT_LAYERED).toURI());
SpringBootExplodedProcessor bootProcessor =
new SpringBootExplodedProcessor(
Paths.get("ignored"), Paths.get("ignored"), JAR_JAVA_VERSION);
sampleSpringBootJar, Paths.get("ignored"), JAR_JAVA_VERSION);
ImmutableList<String> actualEntrypoint =
bootProcessor.computeEntrypoint(ImmutableList.of("-jvm-flag"));
assertThat(actualEntrypoint)
.isEqualTo(
ImmutableList.of(
"java", "-jvm-flag", "-cp", "/app", "org.springframework.boot.loader.JarLauncher"));
"java",
"-jvm-flag",
"-cp",
"/app",
"org.springframework.boot.loader.launch.JarLauncher"));
}

@Test
public void testComputeEntrypoint_noMainClass() throws URISyntaxException {
Path layeredSpringBootJar = Paths.get(Resources.getResource(SPRING_BOOT_NO_MANIFEST).toURI());
SpringBootExplodedProcessor bootProcessor =
new SpringBootExplodedProcessor(
layeredSpringBootJar, Paths.get("ignored"), JAR_JAVA_VERSION);

IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() -> bootProcessor.computeEntrypoint(ImmutableList.of()));

assertThat(exception)
.hasMessageThat()
.isEqualTo(
"`Main-Class:` attribute for an application main class not defined in the input JAR's manifest "
+ "(`META-INF/MANIFEST.MF` in the JAR).");
}

@Test
Expand Down
Binary file modified jib-cli/src/test/resources/jar/spring-boot/springboot_layered.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified jib-cli/src/test/resources/jar/spring-boot/springboot_sample.jar
Binary file not shown.