From 1b2b1801a29b387c91ecf0c8989f833050a22c04 Mon Sep 17 00:00:00 2001 From: Qingyang Chen Date: Mon, 23 Apr 2018 16:21:54 -0400 Subject: [PATCH] Warns the user if specified mainClass is not a valid Java class. (#228) --- .../tools/jib/builder/BuildConfiguration.java | 14 ++++++++++++++ .../tools/jib/builder/BuildConfigurationTest.java | 12 ++++++++++++ jib-gradle-plugin/CHANGELOG.md | 1 + .../cloud/tools/jib/gradle/BuildImageTask.java | 3 +++ jib-maven-plugin/CHANGELOG.md | 1 + .../cloud/tools/jib/maven/BuildImageMojo.java | 5 ++++- 6 files changed, 35 insertions(+), 1 deletion(-) diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java index 80b221f51f..515b8af391 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java @@ -20,12 +20,14 @@ import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate; import com.google.cloud.tools.jib.image.json.V22ManifestTemplate; import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials; +import com.google.common.base.Splitter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Nullable; +import javax.lang.model.SourceVersion; /** Immutable configuration options for the builder process. */ public class BuildConfiguration { @@ -158,6 +160,18 @@ public BuildConfiguration build() { } } + /** + * @return {@code true} if {@code className} is a valid Java class name; {@code false} otherwise + */ + public static boolean isValidJavaClass(String className) { + for (String part : Splitter.on('.').split(className)) { + if (!SourceVersion.isIdentifier(part)) { + return false; + } + } + return true; + } + private final BuildLogger buildLogger; private ImageReference baseImageReference; private ImageReference targetImageReference; diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java index 5a4c9a62f2..05941ab316 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildConfigurationTest.java @@ -153,4 +153,16 @@ public void testBuilder_missingValues() { ex.getMessage()); } } + + @Test + public void testValidJavaClassRegex() { + Assert.assertTrue(BuildConfiguration.isValidJavaClass("my.Class")); + Assert.assertTrue(BuildConfiguration.isValidJavaClass("my.java_Class$valid")); + Assert.assertTrue(BuildConfiguration.isValidJavaClass("multiple.package.items")); + Assert.assertTrue(BuildConfiguration.isValidJavaClass("is123.valid")); + Assert.assertFalse(BuildConfiguration.isValidJavaClass("${start-class}")); + Assert.assertFalse(BuildConfiguration.isValidJavaClass("123not.Valid")); + Assert.assertFalse(BuildConfiguration.isValidJavaClass("{class}")); + Assert.assertFalse(BuildConfiguration.isValidJavaClass("not valid")); + } } diff --git a/jib-gradle-plugin/CHANGELOG.md b/jib-gradle-plugin/CHANGELOG.md index 429feb966e..f65e271152 100644 --- a/jib-gradle-plugin/CHANGELOG.md +++ b/jib-gradle-plugin/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. ### Added +- Warns if specified `mainClass` is not a valid Java class ([#206](https://github.com/google/jib/issues/206)) - Can specify registry credentials to use directly with `from.auth` and `to.auth` ([#215](https://github.com/google/jib/issues/215)) ### Changed diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java index ad7b6880d2..1926dd303e 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java @@ -127,6 +127,9 @@ public void buildImage() throws InvalidImageReferenceException, IOException { throw new GradleException("Could not find main class specified in a 'jar' task"); } } + if (!BuildConfiguration.isValidJavaClass(mainClass)) { + getLogger().warn("'mainClass' is not a valid Java class : " + mainClass); + } SourceFilesConfiguration sourceFilesConfiguration = getSourceFilesConfiguration(); diff --git a/jib-maven-plugin/CHANGELOG.md b/jib-maven-plugin/CHANGELOG.md index 193304ef95..f6d014c798 100644 --- a/jib-maven-plugin/CHANGELOG.md +++ b/jib-maven-plugin/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. ## 0.1.7 ### Added - Better feedback for build failures ([#197](https://github.com/google/jib/pull/197)) +- Warns if specified `mainClass` is not a valid Java class ([#206](https://github.com/google/jib/issues/206)) ## 0.1.6 ### Changed diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java index d62dc644e6..2d6106b4ed 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java @@ -131,6 +131,9 @@ public void execute() throws MojoExecutionException, MojoFailureException { "add a `mainClass` configuration to jib-maven-plugin"); } } + if (!BuildConfiguration.isValidJavaClass(mainClass)) { + getLog().warn("'mainClass' is not a valid Java class : " + mainClass); + } SourceFilesConfiguration sourceFilesConfiguration = projectProperties.getSourceFilesConfiguration(); @@ -293,7 +296,7 @@ private void validateParameters() throws MojoFailureException { throw new MojoFailureException("Invalid configuration parameters"); } } - // Validates 'imageFormat' + // Validates 'imageFormat'. boolean validFormat = false; for (ImageFormat format : ImageFormat.values()) { if (imageFormat.equals(format.name())) {