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

Infer main class if it isn't configured anywhere #278

Merged
merged 12 commits into from
May 17, 2018
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2018 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.frontend;

import com.google.cloud.tools.jib.filesystem.DirectoryWalker;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import javax.annotation.Nullable;

/** Infers the main class in an application. */
public class MainClassFinder {

/** Helper for loading a .class file. */
private static class ClassFileLoader extends ClassLoader {

private final Path classFile;

private ClassFileLoader(Path classFile) {
this.classFile = classFile;
}

@Nullable
@Override
public Class findClass(String name) {
try {
byte[] bytes = Files.readAllBytes(classFile);
return defineClass(name, bytes, 0, bytes.length);
} catch (IOException | ClassFormatError ignored) {
// Not a valid class file
return null;
}
}
}

/**
* Searches for a .class file containing a main method given an absolute root directory.
*
* @return the name of the class if one is found, null if no class is found.
* @throws IOException if searching/reading files fails.
*/
public static List<String> findMainClass(Path rootDirectory) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's returning a list now, findMainClasses would be clearer.

List<String> results = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more descriptive variable name like classNames


// Make sure rootDirectory is valid
if (!Files.exists(rootDirectory) || !Files.isDirectory(rootDirectory)) {
return results;
}

// Get all .class files
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Get/Searches (or Looks at)

new DirectoryWalker(rootDirectory)
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".class"))
.walk(
classFile -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to put this in a separate method since it is getting to almost 6 levels of tabbing around line 91.

// Convert filename (rootDir/path/to/ClassName.class) to class name
// (path.to.ClassName)
Path relativized = rootDirectory.relativize(classFile);
StringJoiner stringJoiner = new StringJoiner(".");
for (Path path : relativized) {
stringJoiner.add(path.toString());
}
String name = stringJoiner.toString();
name = name.substring(0, name.length() - ".class".length());

Class<?> fileClass = new ClassFileLoader(classFile).findClass(name);
if (fileClass != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An early return here would help reduce the tabbing.

try {
// Check if class contains {@code public static void main(String[] args)}
Method main = fileClass.getMethod("main", String[].class);
if (main != null
&& main.getReturnType() == void.class
&& Modifier.isStatic(main.getModifiers())
&& Modifier.isPublic(main.getModifiers())) {
results.add(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, is it possible to not have to do the path-to-class conversion and instead just get the class from the file via reflection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like it, I think I need to pass the full name into the class loader.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe if you pass in null to defineClass it would resolve the class name from the file.

}
} catch (NoSuchMethodException ignored) {
// main method not found
}
}
});

return results;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have a private constructor


private MainClassFinder() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2018 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.frontend;

import com.google.common.io.Resources;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;

/** Test for MainClassFinder. */
public class MainClassFinderTest {

@Test
public void testFindMainClass_simple() throws URISyntaxException, IOException {
Path rootDirectory = Paths.get(Resources.getResource("class-finder-tests/simple").toURI());
List<String> mainClasses = MainClassFinder.findMainClass(rootDirectory);
Assert.assertEquals(1, mainClasses.size());
Assert.assertTrue(mainClasses.contains("HelloWorld"));
}

@Test
public void testFindMainClass_subdirectories() throws URISyntaxException, IOException {
Path rootDirectory =
Paths.get(Resources.getResource("class-finder-tests/subdirectories").toURI());
List<String> mainClasses = MainClassFinder.findMainClass(rootDirectory);
Assert.assertEquals(1, mainClasses.size());
Assert.assertTrue(mainClasses.contains("multi.layered.HelloWorld"));
}

@Test
public void testFindMainClass_noClass() throws URISyntaxException, IOException {
Path rootDirectory = Paths.get(Resources.getResource("class-finder-tests/no-main").toURI());
List<String> mainClasses = MainClassFinder.findMainClass(rootDirectory);
Assert.assertTrue(mainClasses.isEmpty());
}

@Test
public void testFindMainClass_multiple() throws URISyntaxException, IOException {
Path rootDirectory = Paths.get(Resources.getResource("class-finder-tests/multiple").toURI());
List<String> mainClasses = MainClassFinder.findMainClass(rootDirectory);
Assert.assertEquals(2, mainClasses.size());
Assert.assertTrue(mainClasses.contains("multi.layered.HelloMoon"));
Assert.assertTrue(mainClasses.contains("HelloWorld"));
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hi there.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ jib {
image = 'gcr.io/jib-integration-testing/emptyimage:gradle'
credHelper = 'gcr'
}
mainClass = 'com.test.Empty'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😃

// Does not have tests use user-level cache for base image layers.
useOnlyProjectCache = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jib {
image = 'gcr.io/jib-integration-testing/simpleimage:gradle'
credHelper = 'gcr'
}
mainClass = 'com.test.HelloWorld'
// Does not have tests use user-level cache for base image layers.
useOnlyProjectCache = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@

import com.google.cloud.tools.jib.builder.BuildConfiguration;
import com.google.cloud.tools.jib.builder.SourceFilesConfiguration;
import com.google.cloud.tools.jib.frontend.MainClassFinder;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
import org.gradle.jvm.tasks.Jar;

/** Obtains information about a Gradle {@link Project} that uses Jib. */
Expand All @@ -46,21 +51,45 @@ String getMainClass(@Nullable String mainClass) {
if (mainClass == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the logic is getting a bit more complex, it'd be good to add a description to the Javadoc like:

If {@code mainClass} is {@code null}, tries to infer main class in this order:

1. Looks in a {@code jar} task.
2. Searches for a class defined with a main method.

Warns if main class is not valid.

mainClass = getMainClassFromJarTask();
if (mainClass == null) {
throw new GradleException(
HelpfulSuggestionsProvider.get("Could not find main class specified in a 'jar' task")
.suggest("add a `mainClass` configuration to jib"));
gradleBuildLogger.debug(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was talking to @patflynn and Patrick mentioned that it might be better to log something like Searching for main class... Add a 'mainClass' configuration to 'jib' to improve build speed. at the info level to help users in case they have a ton of classes to search.

"Could not find main class specified in a 'jar' task; attempting to "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a todo to consolidate this code with the Maven one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already added it to #262, but I can add a TODO in the code as well if you want.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay that's fine then.

+ "infer main class.");

final String mainClassSuggestion = "add a `mainClass` configuration to jib";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh by constant, I meant like a private static final field.

try {
// Adds each file in each classes output directory to the classes files list.
JavaPluginConvention javaPluginConvention =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I meant here to just directly use the classes files from SourceFilesConfiguration

Copy link
Contributor Author

@TadCordle TadCordle May 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was either this or something like getSourceFilesConfiguration().getClassesFiles().get(0).getParent() since I need the root directory to resolve the class names correctly, and getClassesFiles seems to return a list of the files in the root directory. Plus using getSourceFilesConfiguration() was weird in testing, since I would either need to refactor a bit, or mock a bunch of the GradleSourceFilesConfiguration internals to avoid null pointer exceptions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, though we do need some way to guarantee that the classes we are searching in are the classes that will be in the container.

project.getConvention().getPlugin(JavaPluginConvention.class);
SourceSet mainSourceSet = javaPluginConvention.getSourceSets().getByName("main");
Path classesDirs = Paths.get(mainSourceSet.getOutput().getClassesDirs().getAsPath());
List<String> mainClasses = new ArrayList<>(MainClassFinder.findMainClass(classesDirs));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new ArrayList<> doesn't seem to be necessary.


if (mainClasses.size() == 1) {
mainClass = mainClasses.get(0);
} else if (mainClasses.size() == 0) {
throw new GradleException(
HelpfulSuggestionsProvider.get("Main class was not found")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these messages are the same as in Maven, they can be added to HelpfulSuggestions.

.suggest(mainClassSuggestion));
} else {
throw new GradleException(
HelpfulSuggestionsProvider.get(
"Multiple valid main classes were found: " + String.join(", ", mainClasses))
.suggest(mainClassSuggestion));
}
} catch (IOException ex) {
throw new GradleException(
HelpfulSuggestionsProvider.get("Failed to get main class")
.suggest(mainClassSuggestion),
ex);
}
}
}
if (!BuildConfiguration.isValidJavaClass(mainClass)) {
getLogger().warn("'mainClass' is not a valid Java class : " + mainClass);
gradleBuildLogger.warn("'mainClass' is not a valid Java class : " + mainClass);
}
return mainClass;
}

GradleBuildLogger getLogger() {
return gradleBuildLogger;
}

/** @return the {@link SourceFilesConfiguration} based on the current project */
SourceFilesConfiguration getSourceFilesConfiguration() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@
import java.util.Collections;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.java.archives.Manifest;
import org.gradle.api.java.archives.internal.DefaultManifest;
import org.gradle.api.plugins.Convention;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.SourceSetOutput;
import org.gradle.internal.impldep.com.google.common.collect.ImmutableMap;
import org.gradle.internal.impldep.com.google.common.collect.ImmutableSet;
import org.gradle.jvm.tasks.Jar;
Expand All @@ -43,6 +49,13 @@ public class ProjectPropertiesTest {
@Mock private Project mockProject;
@Mock private GradleBuildLogger mockGradleBuildLogger;

@Mock private Convention mockConvention;
@Mock private JavaPluginConvention mockPluginConvention;
@Mock private SourceSetContainer mockSourceSetContainer;
@Mock private SourceSet mockSourceSet;
@Mock private SourceSetOutput mockSourceSetOutput;
@Mock private FileCollection mockFileCollection;

private Manifest fakeManifest;
private ProjectProperties testProjectProperties;

Expand All @@ -51,6 +64,15 @@ public void setUp() {
fakeManifest = new DefaultManifest(mockFileResolver);
Mockito.when(mockJar.getManifest()).thenReturn(fakeManifest);

Mockito.when(mockProject.getConvention()).thenReturn(mockConvention);
Mockito.when(mockConvention.getPlugin(JavaPluginConvention.class))
.thenReturn(mockPluginConvention);
Mockito.when(mockPluginConvention.getSourceSets()).thenReturn(mockSourceSetContainer);
Mockito.when(mockSourceSetContainer.getByName("main")).thenReturn(mockSourceSet);
Mockito.when(mockSourceSet.getOutput()).thenReturn(mockSourceSetOutput);
Mockito.when(mockSourceSetOutput.getClassesDirs()).thenReturn(mockFileCollection);
Mockito.when(mockFileCollection.getAsPath()).thenReturn("a/b/c");

testProjectProperties = new ProjectProperties(mockProject, mockGradleBuildLogger);
}

Expand Down Expand Up @@ -95,9 +117,9 @@ private void assertGetMainClassFails() {
Assert.fail("Main class not expected");

} catch (GradleException ex) {
Assert.assertThat(
ex.getMessage(),
CoreMatchers.containsString("Could not find main class specified in a 'jar' task"));
Mockito.verify(mockGradleBuildLogger)
.debug(
"Could not find main class specified in a 'jar' task; attempting to infer main class.");
Assert.assertThat(
ex.getMessage(), CoreMatchers.containsString("add a `mainClass` configuration to jib"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import com.google.cloud.tools.jib.builder.BuildConfiguration;
import com.google.cloud.tools.jib.builder.SourceFilesConfiguration;
import com.google.cloud.tools.jib.frontend.MainClassFinder;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import javax.annotation.Nullable;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -57,10 +60,32 @@ String getMainClass(@Nullable String mainClass) throws MojoExecutionException {
if (mainClass == null) {
mainClass = getMainClassFromMavenJarPlugin();
if (mainClass == null) {
throw new MojoExecutionException(
HelpfulSuggestionsProvider.get(
"Could not find main class specified in maven-jar-plugin")
.suggest("add a `mainClass` configuration to jib-maven-plugin"));
log.debug(
"Could not find main class specified in maven-jar-plugin; attempting to infer main "
+ "class.");

final String mainClassSuggestion = "add a `mainClass` configuration to jib-maven-plugin";
try {
List<String> mainClasses =
MainClassFinder.findMainClass(Paths.get(project.getBuild().getOutputDirectory()));
if (mainClasses.size() == 1) {
mainClass = mainClasses.get(0);
} else if (mainClasses.size() == 0) {
throw new MojoExecutionException(
HelpfulSuggestionsProvider.get("Main class was not found")
.suggest(mainClassSuggestion));
} else {
throw new MojoExecutionException(
HelpfulSuggestionsProvider.get(
"Multiple valid main classes were found: " + String.join(", ", mainClasses))
.suggest(mainClassSuggestion));
}
} catch (IOException ex) {
throw new MojoExecutionException(
HelpfulSuggestionsProvider.get("Failed to get main class")
.suggest(mainClassSuggestion),
ex);
}
}
}
Preconditions.checkNotNull(mainClass);
Expand Down
Loading