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

Warns the user if specified mainClass is not a valid Java class. #228

Merged
merged 9 commits into from
Apr 23, 2018
Merged
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 @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
1 change: 1 addition & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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())) {
Expand Down