Skip to content

Commit

Permalink
Fixes quarkusio#1443 by moving manifest generation in a specific meth…
Browse files Browse the repository at this point in the history
…od, invoked after copying all files

The goal of this method is to make sure the entries we write in manfiest do not exist already or, if they exist, are compatible with quarkus requirements.
This goal is reached for MAIN_CLASS attribute, but not totally for CLASSPATH
  • Loading branch information
Riduidel committed Mar 17, 2019
1 parent 58fdbf0 commit 806d3a1
Showing 1 changed file with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,11 @@ public void accept(Path path) {
}
});

final Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, classPath.toString());
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass);
try (OutputStream os = Files.newOutputStream(runnerZipFs.getPath("META-INF", "MANIFEST.MF"))) {
manifest.write(os);
}

copyFiles(augmentOutcome.getAppClassesDir(), runnerZipFs);
copyFiles(augmentOutcome.getTransformedClassesDir(), runnerZipFs);

generateManifest(runnerZipFs, classPath.toString());

for (Map.Entry<String, List<byte[]>> entry : services.entrySet()) {
try (OutputStream os = Files.newOutputStream(runnerZipFs.getPath(entry.getKey()))) {
for (byte[] i : entry.getValue()) {
Expand All @@ -346,6 +340,54 @@ public void accept(Path path) {
}
}

/**
* Manifest generation is quite simple : we just have to push some attributes in manfest.
* However, it gets a little more complex is manifest preexists.
* So we first try to see if a manifest exists, and otherwise create a new one.
*
* <b>BEWARE</b> this method should be invoked after file copy from target/classes and so on.
* Otherwise this manifest manipulation will be useless.
*
* @see https://github.com/quarkusio/quarkus/issues/1443
* @param runnerZipFs
* @param classPath
* @throws IOException
*/
private void generateManifest(FileSystem runnerZipFs, final String classPath) throws IOException {
final Path manifestPath = runnerZipFs.getPath("META-INF", "MANIFEST.MF");
final Manifest manifest = new Manifest();
if (Files.exists(manifestPath)) {
log.warn(
"Seems like you have a pre-existing MANIFEST.MF. Since quarkus generates an executable JAR, some entries will be added.");
try (InputStream is = Files.newInputStream(manifestPath)) {
manifest.read(is);
}
}
Attributes attributes = manifest.getMainAttributes();
if (!attributes.containsKey(Attributes.Name.MANIFEST_VERSION)) {
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
}
if (attributes.containsKey(Attributes.Name.CLASS_PATH)) {
log.warn(
"Your MANIFEST.MF defines a CLASSPATH. We don't yet support merge of CLASSPATHS. So your CLASSPATH will be overwritten by Quarkus one.");
}
attributes.put(Attributes.Name.CLASS_PATH, classPath.toString());
if (attributes.containsKey(Attributes.Name.MAIN_CLASS)) {
String existingMainClass = attributes.getValue(Attributes.Name.MAIN_CLASS);
if (!mainClass.equals(existingMainClass)) {
log.errorf("Your MANIFEST.MF defines as %s %s, while Quarkus is configured to use %s. "
+ "Your main class will be replaced by quarkus one. This behaviour configured by property mainClass of quarkus plugin.",
Attributes.Name.MAIN_CLASS, existingMainClass, mainClass);
attributes.put(Attributes.Name.MAIN_CLASS, mainClass);
}
} else {
attributes.put(Attributes.Name.MAIN_CLASS, mainClass);
}
try (OutputStream os = Files.newOutputStream(manifestPath)) {
manifest.write(os);
}
}

private void copyFiles(Path dir, FileSystem fs) throws IOException {
Files.walk(dir).forEach(new Consumer<Path>() {
@Override
Expand Down

0 comments on commit 806d3a1

Please sign in to comment.