From 083c2550c7eb4a782373d7726bba6a55f55f6204 Mon Sep 17 00:00:00 2001 From: Chanseok Oh Date: Thu, 28 Feb 2019 11:19:12 -0500 Subject: [PATCH] Support Java 11 WAR projects (#1516) --- jib-gradle-plugin/CHANGELOG.md | 4 +- jib-maven-plugin/CHANGELOG.md | 4 +- .../common/PluginConfigurationProcessor.java | 11 +-- .../PluginConfigurationProcessorTest.java | 74 ++++++++++++++++++- 4 files changed, 84 insertions(+), 9 deletions(-) diff --git a/jib-gradle-plugin/CHANGELOG.md b/jib-gradle-plugin/CHANGELOG.md index 5b579531d9..e0650a3195 100644 --- a/jib-gradle-plugin/CHANGELOG.md +++ b/jib-gradle-plugin/CHANGELOG.md @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file. ### Added +- Java 9+ WAR projects are now supported and run on the distroless Jetty Java 11 image (https://github.com/GoogleContainerTools/distroless) by default. Java 8 projects remain on the distroless Jetty Java 8 image. ([#1510](https://github.com/GoogleContainerTools/jib/issues/1510)) + ### Changed ### Fixed @@ -13,7 +15,7 @@ All notable changes to this project will be documented in this file. ### Added -- Java 9+ projects are now supported and run on the distroless Java 11 image by default. Java 8 projects remain on the distroless Java 8 image. ([#1279](https://github.com/GoogleContainerTools/jib/issues/1279)) +- Java 9+ projects are now supported and run on the distroless Java 11 image (https://github.com/GoogleContainerTools/distroless) by default. Java 8 projects remain on the distroless Java 8 image. ([#1279](https://github.com/GoogleContainerTools/jib/issues/1279)) ### Fixed diff --git a/jib-maven-plugin/CHANGELOG.md b/jib-maven-plugin/CHANGELOG.md index 86df0f2f99..7e7aa5a27f 100644 --- a/jib-maven-plugin/CHANGELOG.md +++ b/jib-maven-plugin/CHANGELOG.md @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file. ### Added +- Java 9+ WAR projects are now supported and run on the distroless Jetty Java 11 image (https://github.com/GoogleContainerTools/distroless) by default. Java 8 projects remain on the distroless Jetty Java 8 image. ([#1510](https://github.com/GoogleContainerTools/jib/issues/1510)) + ### Changed ### Fixed @@ -13,7 +15,7 @@ All notable changes to this project will be documented in this file. ### Added -- Java 9+ projects are now supported and run on the distroless Java 11 image by default. Java 8 projects remain on the distroless Java 8 image. ([#1279](https://github.com/GoogleContainerTools/jib/issues/1279)) +- Java 9+ projects are now supported and run on the distroless Java 11 image (https://github.com/GoogleContainerTools/distroless) by default. Java 8 projects remain on the distroless Java 8 image. ([#1279](https://github.com/GoogleContainerTools/jib/issues/1279)) ### Fixed diff --git a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java index cf480df676..4c57b61aa8 100644 --- a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java +++ b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java @@ -295,14 +295,15 @@ static String getBaseImage(RawConfiguration rawConfiguration, ProjectProperties } // Base image not configured; auto-pick Distroless. - if (projectProperties.isWarProject()) { - return "gcr.io/distroless/java/jetty"; - } if (javaVersion <= 8) { - return "gcr.io/distroless/java:8"; + return projectProperties.isWarProject() + ? "gcr.io/distroless/java/jetty:java8" + : "gcr.io/distroless/java:8"; } if (javaVersion <= 11) { - return "gcr.io/distroless/java:11"; + return projectProperties.isWarProject() + ? "gcr.io/distroless/java/jetty:java11" + : "gcr.io/distroless/java:11"; } throw new IncompatibleBaseImageJavaVersionException(11, javaVersion); } diff --git a/jib-plugins-common/src/test/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessorTest.java b/jib-plugins-common/src/test/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessorTest.java index ce14482939..67b68bd349 100644 --- a/jib-plugins-common/src/test/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessorTest.java +++ b/jib-plugins-common/src/test/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessorTest.java @@ -139,7 +139,7 @@ public void testPluginConfigurationProcessor_warProjectBaseImage() PluginConfigurationProcessor processor = createPluginConfigurationProcessor(); Assert.assertEquals( - ImageReference.parse("gcr.io/distroless/java/jetty").toString(), + ImageReference.parse("gcr.io/distroless/java/jetty:java8").toString(), processor.getBaseImageReference().toString()); Mockito.verifyNoMoreInteractions(logger); } @@ -469,6 +469,46 @@ public void testGetBaseImage_chooseJava11Distroless() PluginConfigurationProcessor.getBaseImage(rawConfiguration, projectProperties)); } + @Test + public void testGetBaseImage_chooseJava8JettyDistroless() + throws IncompatibleBaseImageJavaVersionException { + Mockito.when(projectProperties.getMajorJavaVersion()).thenReturn(6); + Mockito.when(projectProperties.isWarProject()).thenReturn(true); + Assert.assertEquals( + "gcr.io/distroless/java/jetty:java8", + PluginConfigurationProcessor.getBaseImage(rawConfiguration, projectProperties)); + + Mockito.when(projectProperties.getMajorJavaVersion()).thenReturn(7); + Assert.assertEquals( + "gcr.io/distroless/java/jetty:java8", + PluginConfigurationProcessor.getBaseImage(rawConfiguration, projectProperties)); + + Mockito.when(projectProperties.getMajorJavaVersion()).thenReturn(8); + Assert.assertEquals( + "gcr.io/distroless/java/jetty:java8", + PluginConfigurationProcessor.getBaseImage(rawConfiguration, projectProperties)); + } + + @Test + public void testGetBaseImage_chooseJava11JettyDistroless() + throws IncompatibleBaseImageJavaVersionException { + Mockito.when(projectProperties.getMajorJavaVersion()).thenReturn(9); + Mockito.when(projectProperties.isWarProject()).thenReturn(true); + Assert.assertEquals( + "gcr.io/distroless/java/jetty:java11", + PluginConfigurationProcessor.getBaseImage(rawConfiguration, projectProperties)); + + Mockito.when(projectProperties.getMajorJavaVersion()).thenReturn(10); + Assert.assertEquals( + "gcr.io/distroless/java/jetty:java11", + PluginConfigurationProcessor.getBaseImage(rawConfiguration, projectProperties)); + + Mockito.when(projectProperties.getMajorJavaVersion()).thenReturn(11); + Assert.assertEquals( + "gcr.io/distroless/java/jetty:java11", + PluginConfigurationProcessor.getBaseImage(rawConfiguration, projectProperties)); + } + @Test public void testGetBaseImage_projectHigherThanJava11() { Mockito.when(projectProperties.getMajorJavaVersion()).thenReturn(12); @@ -522,6 +562,36 @@ public void testGetBaseImage_incompatibleJava11BaseImage() { } } + @Test + public void testGetBaseImage_incompatibleJava8JettyBaseImage() { + Mockito.when(projectProperties.getMajorJavaVersion()).thenReturn(11); + + Mockito.when(rawConfiguration.getFromImage()) + .thenReturn(Optional.of("gcr.io/distroless/java/jetty:java8")); + try { + PluginConfigurationProcessor.getBaseImage(rawConfiguration, projectProperties); + Assert.fail(); + } catch (IncompatibleBaseImageJavaVersionException ex) { + Assert.assertEquals(8, ex.getBaseImageMajorJavaVersion()); + Assert.assertEquals(11, ex.getProjectMajorJavaVersion()); + } + } + + @Test + public void testGetBaseImage_incompatibleJava11JettyBaseImage() { + Mockito.when(projectProperties.getMajorJavaVersion()).thenReturn(15); + + Mockito.when(rawConfiguration.getFromImage()) + .thenReturn(Optional.of("gcr.io/distroless/java/jetty:java11")); + try { + PluginConfigurationProcessor.getBaseImage(rawConfiguration, projectProperties); + Assert.fail(); + } catch (IncompatibleBaseImageJavaVersionException ex) { + Assert.assertEquals(11, ex.getBaseImageMajorJavaVersion()); + Assert.assertEquals(15, ex.getProjectMajorJavaVersion()); + } + } + @Test public void testGetBaseImage_defaultNonWarPackaging() throws IncompatibleBaseImageJavaVersionException { @@ -538,7 +608,7 @@ public void testGetBaseImage_defaultWarProject() Mockito.when(projectProperties.isWarProject()).thenReturn(true); Assert.assertEquals( - "gcr.io/distroless/java/jetty", + "gcr.io/distroless/java/jetty:java8", PluginConfigurationProcessor.getBaseImage(rawConfiguration, projectProperties)); }