diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java index b3ad9a5b6c..4164919e42 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java @@ -99,7 +99,7 @@ public ToAuthConfiguration() { } } - /** Used to configure {@code extraDirectory.permissions} parameter. */ + /** Used to configure {@code extraDirectories.permissions} parameter. */ public static class PermissionConfiguration { @Nullable @Parameter private String file; @@ -183,35 +183,44 @@ public static class ContainerParameters { @Nullable @Parameter private String workingDirectory; } + /** Configuration for the {@code extraDirectories} parameter. */ + public static class ExtraDirectoriesParameters { + + @Parameter private List paths = Collections.emptyList(); + + @Parameter private List permissions = Collections.emptyList(); + + public List getPaths() { + return paths; + } + } + /** Configuration for the {@code extraDirectory} parameter. */ + @Deprecated public static class ExtraDirectoryParameters { // retained for backward-compatibility for ... @Deprecated @Nullable @Parameter private File path; - @Parameter private List paths = Collections.emptyList(); - - @Parameter private List permissions = Collections.emptyList(); + @Deprecated @Parameter + private List permissions = Collections.emptyList(); - /** - * Allows users to configure {@code path} using just {@code } instead of {@code - * }. - * - * @param path the value to set {@code path} to - */ + // Allows users to configure a single path using just instead of + // . @Deprecated public void set(File path) { - this.paths = Collections.singletonList(path); + this.path = path; } + @Deprecated public List getPaths() { - return path != null ? Collections.singletonList(path) : paths; + return path == null ? Collections.emptyList() : Collections.singletonList(path); } } @Nullable @Parameter(defaultValue = "${session}", readonly = true) - MavenSession session; + private MavenSession session; @Nullable @Parameter(defaultValue = "${project}", readonly = true) @@ -224,7 +233,11 @@ public List getPaths() { @Parameter private ContainerParameters container = new ContainerParameters(); // this parameter is cloned in FilesMojo - @Parameter private ExtraDirectoryParameters extraDirectory = new ExtraDirectoryParameters(); + @Deprecated @Parameter + private ExtraDirectoryParameters extraDirectory = new ExtraDirectoryParameters(); + + // this parameter is cloned in FilesMojo + @Parameter private ExtraDirectoriesParameters extraDirectories = new ExtraDirectoriesParameters(); @Parameter( defaultValue = "false", @@ -512,12 +525,38 @@ String getFormat() { */ List getExtraDirectories() { // TODO: Should inform user about nonexistent directory if using custom directory. - String property = getProperty(PropertyNames.EXTRA_DIRECTORY_PATH); + String deprecatedProperty = getProperty(PropertyNames.EXTRA_DIRECTORY_PATH); + String newProperty = getProperty(PropertyNames.EXTRA_DIRECTORIES_PATHS); + + List deprecatedPaths = extraDirectory.getPaths(); + List newPaths = extraDirectories.getPaths(); + + if (deprecatedProperty != null) { + getLog() + .warn( + "The property 'jib.extraDirectory.path' is deprecated; " + + "use 'jib.extraDirectories.paths' instead"); + } + if (!deprecatedPaths.isEmpty()) { + getLog().warn(" is deprecated; use with "); + } + if (deprecatedProperty != null && newProperty != null) { + throw new IllegalArgumentException( + "You cannot configure both 'jib.extraDirectory.path' and 'jib.extraDirectories.paths'"); + } + if (!deprecatedPaths.isEmpty() && !newPaths.isEmpty()) { + throw new IllegalArgumentException( + "You cannot configure both and "); + } + + String property = newProperty != null ? newProperty : deprecatedProperty; if (property != null) { List paths = ConfigurationPropertyValidator.parseListProperty(property); return paths.stream().map(Paths::get).collect(Collectors.toList()); } - return extraDirectory.getPaths().stream().map(File::toPath).collect(Collectors.toList()); + + List paths = !newPaths.isEmpty() ? newPaths : deprecatedPaths; + return paths.stream().map(File::toPath).collect(Collectors.toList()); } /** @@ -526,7 +565,29 @@ List getExtraDirectories() { * @return the configured extra layer file permissions */ List getExtraDirectoryPermissions() { - String property = getProperty(PropertyNames.EXTRA_DIRECTORY_PERMISSIONS); + String deprecatedProperty = getProperty(PropertyNames.EXTRA_DIRECTORY_PERMISSIONS); + String newProperty = getProperty(PropertyNames.EXTRA_DIRECTORIES_PERMISSIONS); + + List deprecatedPermissions = extraDirectory.permissions; + List newPermissions = extraDirectories.permissions; + + if (deprecatedProperty != null) { + getLog() + .warn( + "The property 'jib.extraDirectory.permissions' is deprecated; " + + "use 'jib.extraDirectories.permissions' instead"); + } + if (deprecatedProperty != null && newProperty != null) { + throw new IllegalArgumentException( + "You cannot configure both 'jib.extraDirectory.permissions' and " + + "'jib.extraDirectories.permissions'"); + } + if (!deprecatedPermissions.isEmpty() && !newPermissions.isEmpty()) { + throw new IllegalArgumentException( + "You cannot configure both and "); + } + + String property = newProperty != null ? newProperty : deprecatedProperty; if (property != null) { return ConfigurationPropertyValidator.parseMapProperty(property) .entrySet() @@ -534,7 +595,10 @@ List getExtraDirectoryPermissions() { .map(entry -> new PermissionConfiguration(entry.getKey(), entry.getValue())) .collect(Collectors.toList()); } - return extraDirectory.permissions; + + return !extraDirectories.getPaths().isEmpty() + ? extraDirectories.permissions + : extraDirectory.permissions; } boolean getAllowInsecureRegistries() { @@ -554,6 +618,11 @@ void setProject(MavenProject project) { this.project = project; } + @VisibleForTesting + void setSession(MavenSession session) { + this.session = session; + } + @Nullable String getProperty(String propertyName) { return MavenProjectProperties.getProperty(propertyName, project, session); diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojo.java index 3e5a2f6b0e..4c77e6698e 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojo.java @@ -16,8 +16,11 @@ package com.google.cloud.tools.jib.maven.skaffold; +import com.google.cloud.tools.jib.maven.JibPluginConfiguration.ExtraDirectoriesParameters; import com.google.cloud.tools.jib.maven.JibPluginConfiguration.ExtraDirectoryParameters; import com.google.cloud.tools.jib.maven.MavenProjectProperties; +import com.google.cloud.tools.jib.plugins.common.ConfigurationPropertyValidator; +import com.google.cloud.tools.jib.plugins.common.PropertyNames; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; @@ -44,6 +47,7 @@ import org.apache.maven.project.DependencyResolutionResult; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectDependenciesResolver; +import org.eclipse.aether.graph.Dependency; import org.eclipse.aether.graph.DependencyFilter; /** @@ -78,7 +82,11 @@ public class FilesMojo extends AbstractMojo { @Nullable @Component private ProjectDependenciesResolver projectDependenciesResolver; // This parameter is cloned from JibPluginConfiguration - @Parameter private ExtraDirectoryParameters extraDirectory = new ExtraDirectoryParameters(); + @Deprecated @Parameter + private ExtraDirectoryParameters extraDirectory = new ExtraDirectoryParameters(); + + // This parameter is cloned from JibPluginConfiguration + @Parameter private ExtraDirectoriesParameters extraDirectories = new ExtraDirectoriesParameters(); @Override public void execute() throws MojoExecutionException, MojoFailureException { @@ -143,7 +151,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { resolutionResult .getDependencies() .stream() - .map(org.eclipse.aether.graph.Dependency::getArtifact) + .map(Dependency::getArtifact) .filter(org.eclipse.aether.artifact.Artifact::isSnapshot) .map(org.eclipse.aether.artifact.Artifact::getFile) .forEach(System.out::println); @@ -153,8 +161,32 @@ public void execute() throws MojoExecutionException, MojoFailureException { } } - private List resolveExtraDirectories() { - List paths = extraDirectory.getPaths(); + private List resolveExtraDirectories() throws MojoExecutionException { + // TODO: Should inform user about nonexistent directory if using custom directory. + String deprecatedProperty = + MavenProjectProperties.getProperty(PropertyNames.EXTRA_DIRECTORY_PATH, project, session); + String newProperty = + MavenProjectProperties.getProperty(PropertyNames.EXTRA_DIRECTORIES_PATHS, project, session); + + List deprecatedPaths = extraDirectory.getPaths(); + List newPaths = extraDirectories.getPaths(); + + if (deprecatedProperty != null && newProperty != null) { + throw new MojoExecutionException( + "You cannot configure both 'jib.extraDirectory.path' and 'jib.extraDirectories.paths'"); + } + if (!deprecatedPaths.isEmpty() && !newPaths.isEmpty()) { + throw new MojoExecutionException( + "You cannot configure both and "); + } + + String property = newProperty != null ? newProperty : deprecatedProperty; + if (property != null) { + List paths = ConfigurationPropertyValidator.parseListProperty(property); + return paths.stream().map(Paths::get).map(Path::toAbsolutePath).collect(Collectors.toList()); + } + + List paths = !newPaths.isEmpty() ? newPaths : deprecatedPaths; if (paths.isEmpty()) { Path projectBase = Preconditions.checkNotNull(project).getBasedir().getAbsoluteFile().toPath(); diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojoV2.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojoV2.java index a73e74ca5a..656cf96551 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojoV2.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojoV2.java @@ -17,6 +17,7 @@ package com.google.cloud.tools.jib.maven.skaffold; import com.google.cloud.tools.jib.maven.MavenProjectProperties; +import com.google.cloud.tools.jib.plugins.common.ConfigurationPropertyValidator; import com.google.cloud.tools.jib.plugins.common.PropertyNames; import com.google.cloud.tools.jib.plugins.common.SkaffoldFilesOutput; import com.google.common.annotations.VisibleForTesting; @@ -167,12 +168,22 @@ public void execute() throws MojoExecutionException, MojoFailureException { } } - private List resolveExtraDirectories(MavenProject project) { + private List resolveExtraDirectories(MavenProject project) throws MojoExecutionException { // Try getting extra directory from project/session properties - String extraDirectoryProperty = + String deprecatedProperty = MavenProjectProperties.getProperty(PropertyNames.EXTRA_DIRECTORY_PATH, project, session); - if (extraDirectoryProperty != null) { - return Collections.singletonList(Paths.get(extraDirectoryProperty)); + String newProperty = + MavenProjectProperties.getProperty(PropertyNames.EXTRA_DIRECTORIES_PATHS, project, session); + + if (deprecatedProperty != null && newProperty != null) { + throw new MojoExecutionException( + "You cannot configure both 'jib.extraDirectory.path' and 'jib.extraDirectories.paths'"); + } + + String property = newProperty != null ? newProperty : deprecatedProperty; + if (property != null) { + List paths = ConfigurationPropertyValidator.parseListProperty(property); + return paths.stream().map(Paths::get).collect(Collectors.toList()); } // Try getting extra directory from project pom @@ -180,21 +191,31 @@ private List resolveExtraDirectories(MavenProject project) { if (jibMavenPlugin != null) { Xpp3Dom pluginConfiguration = (Xpp3Dom) jibMavenPlugin.getConfiguration(); if (pluginConfiguration != null) { + Xpp3Dom extraDirectoryConfiguration = pluginConfiguration.getChild("extraDirectory"); - if (extraDirectoryConfiguration != null) { - Xpp3Dom pathChild = extraDirectoryConfiguration.getChild("path"); - if (pathChild != null) { - // ... - return Collections.singletonList(Paths.get(pathChild.getValue())); - } - Xpp3Dom pathsChild = extraDirectoryConfiguration.getChild("paths"); - if (pathsChild != null) { - // ...... - return Arrays.stream(pathsChild.getChildren()) + Xpp3Dom extraDirectoriesConfiguration = pluginConfiguration.getChild("extraDirectories"); + if (extraDirectoryConfiguration != null && extraDirectoriesConfiguration != null) { + throw new MojoExecutionException( + "You cannot configure both and "); + } + + if (extraDirectoriesConfiguration != null) { + Xpp3Dom child = extraDirectoriesConfiguration.getChild("paths"); + if (child != null) { + // ...... + return Arrays.stream(child.getChildren()) .map(Xpp3Dom::getValue) .map(Paths::get) .collect(Collectors.toList()); } + } + + if (extraDirectoryConfiguration != null) { + Xpp3Dom child = extraDirectoryConfiguration.getChild("path"); + if (child != null) { + // ... + return Collections.singletonList(Paths.get(child.getValue())); + } // ... String value = extraDirectoryConfiguration.getValue(); if (value != null) { diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java index 69ec8cb8a3..e5218f5504 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java @@ -98,13 +98,9 @@ private static boolean isJava11RuntimeOrHigher() { return Integer.valueOf(split.iterator().next()) >= 11; } - /** - * Builds and runs jib:build on a project at {@code projectRoot} pushing to {@code - * imageReference}. - */ - private static String buildAndRun( - Path projectRoot, String imageReference, String pomXml, boolean runTwice) - throws VerificationException, IOException, InterruptedException, DigestException { + private static Verifier build( + Path projectRoot, String imageReference, String pomXml, boolean buildTwice) + throws VerificationException, IOException { Verifier verifier = new Verifier(projectRoot.toString()); verifier.setSystemProperty("jib.useOnlyProjectCache", "true"); verifier.setSystemProperty("_TARGET_IMAGE", imageReference); @@ -117,7 +113,7 @@ private static String buildAndRun( verifier.executeGoal("jib:build"); float timeOne = getBuildTimeFromVerifierLog(verifier); - if (runTwice) { + if (buildTwice) { verifier.resetStreams(); verifier.executeGoal("jib:build"); float timeTwo = getBuildTimeFromVerifierLog(verifier); @@ -126,7 +122,17 @@ private static String buildAndRun( Assert.assertTrue(String.format(failMessage, timeOne, timeTwo), timeOne > timeTwo); } - verifier.verifyErrorFreeLog(); + return verifier; + } + + /** + * Builds with {@code jib:build} on a project at {@code projectRoot} pushing to {@code + * imageReference} and run the image after pulling it. + */ + private static String buildAndRun( + Path projectRoot, String imageReference, String pomXml, boolean buildTwice) + throws VerificationException, IOException, InterruptedException, DigestException { + build(projectRoot, imageReference, pomXml, buildTwice).verifyErrorFreeLog(); String output = pullAndRunBuiltImage(imageReference); @@ -417,6 +423,29 @@ public void testExecute_multipleExtraDirectories() assertLayerSizer(9, targetImage); // one more than usual } + @Test + public void testExecute_bothDeprecatedAndNewExtraDirectoryConfigUsed() throws IOException { + try { + build( + simpleTestProject.getProjectRoot(), "foo", "pom-deprecated-and-new-extra-dir.xml", false); + Assert.fail(); + } catch (VerificationException ex) { + Assert.assertThat( + ex.getMessage(), + CoreMatchers.containsString( + "You cannot configure both and ")); + } + } + + @Test + public void testExecute_deprecatedExtraDirectoryConfigUsed() + throws IOException, VerificationException { + String targetImage = getGcrImageReference("simpleimage:maven"); + build(simpleTestProject.getProjectRoot(), targetImage, "pom-deprecated-extra-dir.xml", false) + .verifyTextInLog( + " is deprecated; use with "); + } + @Test public void testExecute_defaultTarget() throws IOException { // Test error when 'to' is missing diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/JibPluginConfigurationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/JibPluginConfigurationTest.java index 4a96dff604..457f4e9790 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/JibPluginConfigurationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/JibPluginConfigurationTest.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Properties; import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.junit.Assert; import org.junit.Before; @@ -41,6 +42,7 @@ public class JibPluginConfigurationTest { private final MavenProject project = new MavenProject(); private final Properties sessionProperties = new Properties(); @Mock private MavenSession session; + @Mock private Log log; private JibPluginConfiguration testPluginConfiguration; @Before @@ -50,9 +52,14 @@ public void setup() { new JibPluginConfiguration() { @Override public void execute() {} + + @Override + public Log getLog() { + return log; + } }; testPluginConfiguration.setProject(project); - testPluginConfiguration.session = session; + testPluginConfiguration.setSession(session); } @Test @@ -111,10 +118,10 @@ public void testSystemProperties() { sessionProperties.put("jib.container.workingDirectory", "working directory"); Assert.assertEquals("working directory", testPluginConfiguration.getWorkingDirectory()); - sessionProperties.put("jib.extraDirectory.path", "custom-jib"); + sessionProperties.put("jib.extraDirectories.paths", "custom-jib"); Assert.assertEquals( Arrays.asList(Paths.get("custom-jib")), testPluginConfiguration.getExtraDirectories()); - sessionProperties.put("jib.extraDirectory.permissions", "/test/file1=123,/another/file=456"); + sessionProperties.put("jib.extraDirectories.permissions", "/test/file1=123,/another/file=456"); List permissions = testPluginConfiguration.getExtraDirectoryPermissions(); Assert.assertEquals("/test/file1", permissions.get(0).getFile().get()); @@ -173,12 +180,12 @@ public void testPomProperties() { project.getProperties().setProperty("jib.container.workingDirectory", "working directory"); Assert.assertEquals("working directory", testPluginConfiguration.getWorkingDirectory()); - project.getProperties().setProperty("jib.extraDirectory.path", "custom-jib"); + project.getProperties().setProperty("jib.extraDirectories.paths", "custom-jib"); Assert.assertEquals( Arrays.asList(Paths.get("custom-jib")), testPluginConfiguration.getExtraDirectories()); project .getProperties() - .setProperty("jib.extraDirectory.permissions", "/test/file1=123,/another/file=456"); + .setProperty("jib.extraDirectories.permissions", "/test/file1=123,/another/file=456"); List permissions = testPluginConfiguration.getExtraDirectoryPermissions(); Assert.assertEquals("/test/file1", permissions.get(0).getFile().get()); @@ -200,4 +207,110 @@ public void testEmptyOrNullTags() { Assert.assertEquals("jib.to.tags has empty tag", ex.getMessage()); } } + + @Test + public void testDeprecatedSystemProperties() { + sessionProperties.put("jib.extraDirectory.path", "custom-jib"); + Assert.assertEquals( + Arrays.asList(Paths.get("custom-jib")), testPluginConfiguration.getExtraDirectories()); + sessionProperties.put("jib.extraDirectory.permissions", "/test/file13=650,/another/file24=777"); + List permissions = + testPluginConfiguration.getExtraDirectoryPermissions(); + Assert.assertEquals("/test/file13", permissions.get(0).getFile().get()); + Assert.assertEquals("650", permissions.get(0).getMode().get()); + Assert.assertEquals("/another/file24", permissions.get(1).getFile().get()); + Assert.assertEquals("777", permissions.get(1).getMode().get()); + + Mockito.verify(log, Mockito.times(1)) + .warn( + "The property 'jib.extraDirectory.path' is deprecated; " + + "use 'jib.extraDirectories.paths' instead"); + } + + @Test + public void testDeprecatedProperties() { + Properties projectProperties = project.getProperties(); + + projectProperties.setProperty("jib.extraDirectory.path", "this-is-extra"); + Assert.assertEquals( + Arrays.asList(Paths.get("this-is-extra")), testPluginConfiguration.getExtraDirectories()); + + projectProperties.setProperty( + "jib.extraDirectory.permissions", "/test/file1=654,/dir/file2=321"); + List permissions = + testPluginConfiguration.getExtraDirectoryPermissions(); + Assert.assertEquals("/test/file1", permissions.get(0).getFile().get()); + Assert.assertEquals("654", permissions.get(0).getMode().get()); + Assert.assertEquals("/dir/file2", permissions.get(1).getFile().get()); + Assert.assertEquals("321", permissions.get(1).getMode().get()); + + Mockito.verify(log, Mockito.times(1)) + .warn( + "The property 'jib.extraDirectory.path' is deprecated; " + + "use 'jib.extraDirectories.paths' instead"); + } + + @Test + public void testGetExtraDirectories_bothSystemPropertiesUsed() { + sessionProperties.put("jib.extraDirectory.path", "deprecated-property"); + sessionProperties.put("jib.extraDirectories.paths", "new-property"); + + try { + testPluginConfiguration.getExtraDirectories(); + Assert.fail(); + } catch (IllegalArgumentException ex) { + Assert.assertEquals( + "You cannot configure both 'jib.extraDirectory.path' and 'jib.extraDirectories.paths'", + ex.getMessage()); + } + } + + @Test + public void testGetExtraDirectories_bothPropertiesUsed() { + Properties projectProperties = project.getProperties(); + projectProperties.setProperty("jib.extraDirectory.path", "deprecated-property"); + projectProperties.setProperty("jib.extraDirectories.paths", "new-property"); + + try { + testPluginConfiguration.getExtraDirectories(); + Assert.fail(); + } catch (IllegalArgumentException ex) { + Assert.assertEquals( + "You cannot configure both 'jib.extraDirectory.path' and 'jib.extraDirectories.paths'", + ex.getMessage()); + } + } + + @Test + public void testGetExtraDirectoryPermissions_bothSystemPropertiesUsed() { + sessionProperties.put("jib.extraDirectory.permissions", "deprecated-property"); + sessionProperties.put("jib.extraDirectories.permissions", "new-property"); + + try { + testPluginConfiguration.getExtraDirectoryPermissions(); + Assert.fail(); + } catch (IllegalArgumentException ex) { + Assert.assertEquals( + "You cannot configure both 'jib.extraDirectory.permissions' and " + + "'jib.extraDirectories.permissions'", + ex.getMessage()); + } + } + + @Test + public void testGetExtraDirectoryPermissions_bothPropertiesUsed() { + Properties projectProperties = project.getProperties(); + projectProperties.setProperty("jib.extraDirectory.permissions", "deprecated-property"); + projectProperties.setProperty("jib.extraDirectories.permissions", "new-property"); + + try { + testPluginConfiguration.getExtraDirectoryPermissions(); + Assert.fail(); + } catch (IllegalArgumentException ex) { + Assert.assertEquals( + "You cannot configure both 'jib.extraDirectory.permissions' and " + + "'jib.extraDirectories.permissions'", + ex.getMessage()); + } + } } diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojoV2Test.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojoV2Test.java index 4571bf6506..1941e3b31b 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojoV2Test.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/skaffold/FilesMojoV2Test.java @@ -49,6 +49,7 @@ private static void verifyFiles( Path projectRoot, String pomXml, String module, + List extraCliOptions, List buildFiles, List inputFiles) throws VerificationException, IOException { @@ -62,6 +63,7 @@ private static void verifyFiles( verifier.addCliOption(module); verifier.addCliOption("-am"); } + extraCliOptions.forEach(verifier::addCliOption); verifier.executeGoal("jib:" + FilesMojoV2.GOAL_NAME); verifier.verifyErrorFreeLog(); @@ -84,6 +86,7 @@ public void testFilesMojo_singleModule() throws VerificationException, IOExcepti projectRoot, "pom.xml", null, + Collections.emptyList(), Collections.singletonList(projectRoot.resolve("pom.xml").toString()), Arrays.asList( projectRoot.resolve("src/main/java").toString(), @@ -100,6 +103,7 @@ public void testFilesMojo_singleModuleWithMultipleExtraDirectories() projectRoot, "pom-extra-dirs.xml", null, + Collections.emptyList(), Collections.singletonList(projectRoot.resolve("pom-extra-dirs.xml").toString()), Arrays.asList( projectRoot.resolve("src/main/java").toString(), @@ -117,6 +121,7 @@ public void testFilesMojo_multiModuleSimpleService() throws VerificationExceptio projectRoot, "pom.xml", "simple-service", + Collections.emptyList(), Arrays.asList( projectRoot.resolve("pom.xml").toString(), simpleServiceRoot.resolve("pom.xml").toString()), @@ -136,6 +141,7 @@ public void testFilesMojo_multiModuleComplexService() throws VerificationExcepti projectRoot, "pom.xml", "complex-service", + Collections.emptyList(), Arrays.asList( projectRoot.resolve("pom.xml").toString(), libRoot.resolve("pom.xml").toString(), @@ -153,4 +159,21 @@ public void testFilesMojo_multiModuleComplexService() throws VerificationExcepti ".m2/repository/com/google/guava/guava/HEAD-jre-SNAPSHOT/guava-HEAD-jre-SNAPSHOT.jar") .toString())); } + + @Test + public void testFilesMojo_extraDirectoriesProperty() throws VerificationException, IOException { + Path projectRoot = simpleTestProject.getProjectRoot(); + + verifyFiles( + projectRoot, + "pom.xml", + null, + Arrays.asList("-Djib.extraDirectories.paths=/some/extra/dir,/another/extra/dir"), + Collections.singletonList(projectRoot.resolve("pom.xml").toString()), + Arrays.asList( + projectRoot.resolve("src/main/java").toString(), + projectRoot.resolve("src/main/resources").toString(), + Paths.get("/some/extra/dir").toString(), + Paths.get("/another/extra/dir").toString())); + } } diff --git a/jib-maven-plugin/src/test/resources/maven/projects/simple/pom-deprecated-and-new-extra-dir.xml b/jib-maven-plugin/src/test/resources/maven/projects/simple/pom-deprecated-and-new-extra-dir.xml new file mode 100644 index 0000000000..19f2919955 --- /dev/null +++ b/jib-maven-plugin/src/test/resources/maven/projects/simple/pom-deprecated-and-new-extra-dir.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + com.test + my-artifact-id + 1 + + + UTF-8 + UTF-8 + @@PluginVersion@@ + ${_TARGET_IMAGE} + An argument. + 1000/tcp,2000-2003/udp + key1=value1,key2=value2 + + + + + com.test + dependency + 1.0.0 + system + ${project.basedir}/libs/dependency-1.0.0.jar + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + com.google.cloud.tools + jib-maven-plugin + ${jib-maven-plugin.version} + + deprecated-config + good-config + + + + + diff --git a/jib-maven-plugin/src/test/resources/maven/projects/simple/pom-deprecated-extra-dir.xml b/jib-maven-plugin/src/test/resources/maven/projects/simple/pom-deprecated-extra-dir.xml new file mode 100644 index 0000000000..ec9beeaa69 --- /dev/null +++ b/jib-maven-plugin/src/test/resources/maven/projects/simple/pom-deprecated-extra-dir.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + com.test + my-artifact-id + 1 + + + UTF-8 + UTF-8 + @@PluginVersion@@ + ${_TARGET_IMAGE} + An argument. + 1000/tcp,2000-2003/udp + key1=value1,key2=value2 + + + + + com.test + dependency + 1.0.0 + system + ${project.basedir}/libs/dependency-1.0.0.jar + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + com.google.cloud.tools + jib-maven-plugin + ${jib-maven-plugin.version} + + deprecated-config + + + + + diff --git a/jib-maven-plugin/src/test/resources/maven/projects/simple/pom-extra-dirs.xml b/jib-maven-plugin/src/test/resources/maven/projects/simple/pom-extra-dirs.xml index 3e3f399c62..1c594f7d8f 100644 --- a/jib-maven-plugin/src/test/resources/maven/projects/simple/pom-extra-dirs.xml +++ b/jib-maven-plugin/src/test/resources/maven/projects/simple/pom-extra-dirs.xml @@ -43,12 +43,12 @@ jib-maven-plugin ${jib-maven-plugin.version} - + ${project.basedir}/src/main/jib-custom ${project.basedir}/src/main/jib-custom-2 - + diff --git a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/ConfigurationPropertyValidator.java b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/ConfigurationPropertyValidator.java index c2dd0922d4..c4ee6a7c5a 100644 --- a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/ConfigurationPropertyValidator.java +++ b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/ConfigurationPropertyValidator.java @@ -23,10 +23,8 @@ import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.image.InvalidImageReferenceException; import com.google.common.base.Strings; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import java.util.ArrayList; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -38,7 +36,7 @@ public class ConfigurationPropertyValidator { /** Matches key-value pairs in the form of "key=value" */ - private static final Pattern ENVIRONMENT_PATTERN = Pattern.compile("(?[^=]+)=(?.*)"); + private static final Pattern KEY_VALUE_PATTERN = Pattern.compile("(?[^=]+)=(?.*)"); /** * Gets a {@link Credential} from a username and password. First tries system properties, then @@ -149,18 +147,18 @@ public static ImageReference getGeneratedTargetDockerTag( * @return the map of parsed values */ public static Map parseMapProperty(String property) { - Map result = new HashMap<>(); + Map result = new LinkedHashMap<>(); // LinkedHashMap to keep insertion order // Split on non-escaped commas List entries = parseListProperty(property); for (String entry : entries) { - Matcher matcher = ENVIRONMENT_PATTERN.matcher(entry); + Matcher matcher = KEY_VALUE_PATTERN.matcher(entry); if (!matcher.matches()) { throw new IllegalArgumentException("'" + entry + "' is not a valid key-value pair"); } result.put(matcher.group("name"), matcher.group("value")); } - return ImmutableMap.copyOf(result); + return result; } /** @@ -183,7 +181,7 @@ public static List parseListProperty(String property) { } } items.add(property.substring(startIndex)); - return ImmutableList.copyOf(items); + return items; } private ConfigurationPropertyValidator() {} diff --git a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PropertyNames.java b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PropertyNames.java index 5347625eb6..0e9c46326b 100644 --- a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PropertyNames.java +++ b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PropertyNames.java @@ -46,8 +46,13 @@ public class PropertyNames { public static final String BASE_IMAGE_CACHE = "jib.baseImageCache"; public static final String APPLICATION_CACHE = "jib.applicationCache"; public static final String ALLOW_INSECURE_REGISTRIES = "jib.allowInsecureRegistries"; - public static final String EXTRA_DIRECTORY_PATH = "jib.extraDirectory.path"; + @Deprecated public static final String EXTRA_DIRECTORY_PATH = "jib.extraDirectory.path"; + public static final String EXTRA_DIRECTORIES_PATHS = "jib.extraDirectories.paths"; + + @Deprecated public static final String EXTRA_DIRECTORY_PERMISSIONS = "jib.extraDirectory.permissions"; + + public static final String EXTRA_DIRECTORIES_PERMISSIONS = "jib.extraDirectories.permissions"; public static final String DOCKER_CLIENT_EXECUTABLE = "jib.dockerClient.executable"; public static final String DOCKER_CLIENT_ENVIRONMENT = "jib.dockerClient.environment"; public static final String SKIP = "jib.skip";