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

Compute entrypoint for a standard jar in exploded mode. #2862

Merged
merged 8 commits into from
Oct 27, 2020

Conversation

mpeddada1
Copy link
Contributor

For #2845
This PR also contains some minor refactorings.

@mpeddada1 mpeddada1 requested a review from a team October 24, 2020 01:25
@google-cla google-cla bot added the cla: yes label Oct 24, 2020
throws IOException, URISyntaxException {
Path standardJar =
Paths.get(Resources.getResource(STANDARD_JAR_WITH_CLASS_PATH_MANIFEST).toURI());
Path destDir = temporaryFolder.newFolder().toPath();
Copy link
Member

Choose a reason for hiding this comment

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

Just so you know, if you don't need a nested directory or multiple directories, you can use the root: temporaryFolder.getRoot().toPath().

* provided doesn't exist
* @throws IllegalArgumentException if main class is not found in the jar manifest
*/
public static ImmutableList<String> computeEntrypoint_explodedStandard(
Copy link
Member

Choose a reason for hiding this comment

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

Using _ is only accepted in test method names. computeEntrypointForExplodedStandard()?

And I don't know who will require this method, but you should always double-checking if a method may have a narrower access scope. (Just don't habitually make everything public until necessary.)

Copy link
Contributor Author

@mpeddada1 mpeddada1 Oct 26, 2020

Choose a reason for hiding this comment

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

oops I keep mixing up the naming with python convention

Oh good point. Changed this method and explodeStandardJar to protected since they will be accessed from within the same package (specifically in the class that will be used to take a jar and convert it into a JibContainerBuilder -- next pr).

mainClass = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
}
if (mainClass == null) {
throw new IllegalArgumentException("Main-Class not found in jar's manifest.");
Copy link
Member

Choose a reason for hiding this comment

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

The CLI will crash with this message. And providing a clear message implying an actionable fix increases the chance of the user understanding and fixing the issue on their own, resulting in less frustrated getting-started experience (which is important to increase the chance of adoption). It also reduces our workload by reducing the likelyhood of multiple user opening the same issue.

Suggested change
throw new IllegalArgumentException("Main-Class not found in jar's manifest.");
throw new IllegalArgumentException("'Main-Class:' attribute to define an application main class not defined in the input JAR's manifest ('META-INF/MANIFEST.MF` in the JAR).");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah you're right. Better error messaging is certainly helpful especially with something that causes the code to crash. Thanks.

List<String> classpath = new ArrayList<>();
for (FileEntriesLayer layer : layers) {
if (layer.getName().equals(DEPENDENCIES) || layer.getName().equals(SNAPSHOT_DEPENDENCIES)) {
classpath.add(APP_ROOT.resolve("dependencies").toString());
Copy link
Member

Choose a reason for hiding this comment

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

At least this should be

Suggested change
classpath.add(APP_ROOT.resolve("dependencies").toString());
classpath.add(APP_ROOT + "/dependencies/*");

Just so you know, there's an issue with the order of loading jars (like #1871 for Jib build plugins), but I think there's nothing we can do about it for a standard jar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is the ordering issue not a problem for a standard jar atm?

@@ -76,7 +81,7 @@ public static JarType determineJarType(Path jarPath) throws IOException {
* @throws IOException if I/O error occurs when opening the jar file or if temporary directory
* provided doesn't exist
*/
public static List<FileEntriesLayer> explodeStandardJar(Path jarPath, Path tempDirPath)
protected static List<FileEntriesLayer> explodeStandardJar(Path jarPath, Path tempDirPath)
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps this can be package-private?

Suggested change
protected static List<FileEntriesLayer> explodeStandardJar(Path jarPath, Path tempDirPath)
static List<FileEntriesLayer> explodeStandardJar(Path jarPath, Path tempDirPath)

And if this needs to be package-private only to be able to call from tests (i.e., it should have been private if not for the tests), add @VisibleForTesting.

}
if (mainClass == null) {
throw new IllegalArgumentException(
"`Main-Class:` attribute to define an application main class not defined in the input Jar's manifest (`META-INF/MANIFEST.MF` in the JAR).");
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I think this reads better:

Suggested change
"`Main-Class:` attribute to define an application main class not defined in the input Jar's manifest (`META-INF/MANIFEST.MF` in the JAR).");
"`Main-Class:` attribute for an application main class not defined in the input Jar's manifest (`META-INF/MANIFEST.MF` in the Jar).");

protected static ImmutableList<String> computeEntrypointForExplodedStandard(
Path jarPath, Path tempDirPath, List<FileEntriesLayer> layers) throws IOException {
Path localExplodedJarRoot = tempDirPath;
ZipUtil.unzip(jarPath, localExplodedJarRoot);
Copy link
Member

Choose a reason for hiding this comment

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

Oh, actually, I remember unzipping a jar somewhere else too. Are we unzipping it multiple times? We should unzip only once for efficiency.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, I think there is no need to unzip a jar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cleaned this up a bit.

Comment on lines 168 to 170
try (JarFile jarFile = new JarFile(jarPath.toFile())) {
mainClass = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
}
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think this code block is super fast, so it's good to put everything in to the try-resource block.

Suggested change
try (JarFile jarFile = new JarFile(jarPath.toFile())) {
mainClass = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
}
try (JarFile jarFile = new JarFile(jarPath.toFile())) {
String mainClass = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
if (mainClass == null)
...
return ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@mpeddada1 mpeddada1 merged commit dbba0c6 into master Oct 27, 2020
@mpeddada1 mpeddada1 deleted the cli-jar-compute-entrypoint branch October 27, 2020 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants