Skip to content

Commit

Permalink
Merge pull request #23147 from glefloch/fix/manifest-add-attr
Browse files Browse the repository at this point in the history
Add the ability to specify main MANIFEST.MF attributes from configuration
  • Loading branch information
gsmet authored Jan 25, 2022
2 parents dcc1c48 + d3357ce commit cb1ff41
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ public class ManifestConfig {
@ConfigItem(defaultValue = "true")
public boolean addImplementationEntries;

/**
* Custom manifest attributes to be added to the main section of the MANIFEST.MF file.
* An example of the user defined property:
* quarkus.package.manifest.attributes."Entry-key1"=Value1
* quarkus.package.manifest.attributes."Entry-key2"=Value2
*/
@ConfigItem()
public Map<String, String> attributes;

/**
* Custom manifest sections to be added to the MANIFEST.MF file.
* An example of the user defined property:
* quarkus.package.manifest.manifest-sections.{Section-Name}.{Entry-Key1}={Value1}
* quarkus.package.manifest.manifest-sections.{Section-Name}.{Entry-Key2}={Value2}
* quarkus.package.manifest.manifest-sections."Section-Name"."Entry-Key1"=Value1
* quarkus.package.manifest.manifest-sections."Section-Name"."Entry-Key2"=Value2
*/
@ConfigItem()
public Map<String, Map<String, String>> manifestSections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1333,15 +1333,22 @@ private void generateManifest(FileSystem runnerZipFs, final String classPath, Pa
Files.createDirectories(manifestPath.getParent());
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");

if (config.manifest.attributes.size() > 0) {
for (Map.Entry<String, String> attribute : config.manifest.attributes.entrySet()) {
attributes.putValue(attribute.getKey(), attribute.getValue());
}
}
if (attributes.containsKey(Attributes.Name.CLASS_PATH)) {
log.warn(
"Your MANIFEST.MF already defined a CLASS_PATH entry. Quarkus has overwritten this existing entry.");
"A CLASS_PATH entry was already defined in your MANIFEST.MF or using the property quarkus.package.manifest.attributes.\"Class-Path\". Quarkus has overwritten this existing entry.");
}
attributes.put(Attributes.Name.CLASS_PATH, classPath);
if (attributes.containsKey(Attributes.Name.MAIN_CLASS)) {
String existingMainClass = attributes.getValue(Attributes.Name.MAIN_CLASS);
if (!mainClassName.equals(existingMainClass)) {
log.warn("Your MANIFEST.MF already defined a MAIN_CLASS entry. Quarkus has overwritten your existing entry.");
log.warn(
"A MAIN_CLASS entry was already defined in your MANIFEST.MF or using the property quarkus.package.manifest.attributes.\"Main-Class\". Quarkus has overwritten your existing entry.");
}
}
attributes.put(Attributes.Name.MAIN_CLASS, mainClassName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkus.maven;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;

public class CustomManifestEntriesThinJarTest {

@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.withEmptyApplication()
.setApplicationName("Custom-Manifest-Thin")
.setApplicationVersion("0.1-SNAPSHOT")
.withConfigurationResource("projects/custom-manifest-section/custom-entries-thin.properties");

@ProdBuildResults
private ProdModeTestResults prodModeTestResults;

@Test
public void testManifestEntries() throws Exception {
assertThat(prodModeTestResults.getResults()).hasSize(1);
Path jarPath = prodModeTestResults.getResults().get(0).getPath();

try (InputStream fileInputStream = new FileInputStream(jarPath.toFile())) {
try (JarInputStream stream = new JarInputStream(fileInputStream)) {
Manifest manifest = stream.getManifest();
assertThat(manifest).isNotNull();

String customAttribute = manifest.getMainAttributes().getValue("Built-By");
assertThat(customAttribute).isNotNull();
assertThat(customAttribute).isEqualTo("Quarkus Plugin");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkus.maven;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;

public class CustomManifestEntriesUberJarTest {

@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.withEmptyApplication()
.setApplicationName("Custom-Manifest-Uber")
.setApplicationVersion("0.1-SNAPSHOT")
.withConfigurationResource("projects/custom-manifest-section/custom-entries-uber.properties");

@ProdBuildResults
private ProdModeTestResults prodModeTestResults;

@Test
public void testManifestEntries() throws Exception {
assertThat(prodModeTestResults.getResults()).hasSize(1);
Path jarPath = prodModeTestResults.getResults().get(0).getPath();

try (InputStream fileInputStream = new FileInputStream(jarPath.toFile())) {
try (JarInputStream stream = new JarInputStream(fileInputStream)) {
Manifest manifest = stream.getManifest();
assertThat(manifest).isNotNull();

String customAttribute = manifest.getMainAttributes().getValue("Built-By");
assertThat(customAttribute).isNotNull();
assertThat(customAttribute).isEqualTo("Quarkus Plugin");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.package.type=jar
quarkus.package.manifest.attributes."Built-By"=Quarkus Plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.package.type=uber-jar
quarkus.package.manifest.attributes."Built-By"=Quarkus Plugin

0 comments on commit cb1ff41

Please sign in to comment.