-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
jib-cli/src/main/java/com/google/cloud/tools/jib/cli/jar/JarProcessor.java
Outdated
Show resolved
Hide resolved
throws IOException, URISyntaxException { | ||
Path standardJar = | ||
Paths.get(Resources.getResource(STANDARD_JAR_WITH_CLASS_PATH_MANIFEST).toURI()); | ||
Path destDir = temporaryFolder.newFolder().toPath(); |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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).
jib-cli/src/main/java/com/google/cloud/tools/jib/cli/jar/JarProcessor.java
Outdated
Show resolved
Hide resolved
mainClass = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.MAIN_CLASS); | ||
} | ||
if (mainClass == null) { | ||
throw new IllegalArgumentException("Main-Class not found in jar's manifest."); |
There was a problem hiding this comment.
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.
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)."); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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
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.
There was a problem hiding this comment.
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?
jib-cli/src/main/java/com/google/cloud/tools/jib/cli/jar/JarProcessor.java
Outdated
Show resolved
Hide resolved
@@ -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) |
There was a problem hiding this comment.
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?
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)."); |
There was a problem hiding this comment.
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:
"`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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
try (JarFile jarFile = new JarFile(jarPath.toFile())) { | ||
mainClass = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.MAIN_CLASS); | ||
} |
There was a problem hiding this comment.
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.
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 ... | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
For #2845
This PR also contains some minor refactorings.