Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/main/java/org/apache/maven/shared/archiver/MavenArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public Manifest getManifest(Session session, Project project, MavenArchiveConfig

for (Map.Entry<String, String> entry : sectionEntries.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
String value = sanitizeManifestValue(entry.getValue());
Manifest.Attribute attr = new Manifest.Attribute(key, value);
theSection.addConfiguredAttribute(attr);
}
Expand Down Expand Up @@ -207,15 +207,16 @@ private void addManifestAttribute(Manifest manifest, Map<String, String> map, St
}

private void addManifestAttribute(Manifest manifest, String key, String value) throws ManifestException {
if (!(value == null || value.isEmpty())) {
Manifest.Attribute attr = new Manifest.Attribute(key, value);
manifest.addConfiguredAttribute(attr);
} else {
// if the value is empty, create an entry with an empty string
// to prevent null print in the manifest file
Manifest.Attribute attr = new Manifest.Attribute(key, "");
manifest.addConfiguredAttribute(attr);
String sanitized = sanitizeManifestValue(value);
Manifest.Attribute attr = new Manifest.Attribute(key, sanitized);
manifest.addConfiguredAttribute(attr);
}

private static String sanitizeManifestValue(String value) {
if (value == null) {
return "";
}
return value.replace("\r\n", " ").replace('\r', ' ').replace('\n', ' ');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,6 @@ void carriageReturnInManifestEntry() throws Exception {
config.setForced(true);
config.getManifest().setAddDefaultImplementationEntries(true);
config.addManifestEntry("Description", project.getModel().getDescription());
// config.addManifestEntry( "EntryWithTab", " foo tab " + ( '\u0009' ) + ( '\u0009' ) // + " bar tab" + ( //
// '\u0009' // ) );
archiver.createArchive(session, project, config);
assertThat(jarFile).exists();

Expand All @@ -328,6 +326,46 @@ void carriageReturnInManifestEntry() throws Exception {
assertThat(value.indexOf(ls)).isLessThanOrEqualTo(0);
}

@Test
void newlinesInManifestEntryGetReplacedWithSpaces() throws Exception {
ProjectStub project = getDummyProject();

MavenArchiveConfiguration config = new MavenArchiveConfiguration();
config.addManifestEntry("MultiLine", "line1\nline2\nline3");
config.addManifestEntry("WithCRLF", "line1\r\nline2");
config.addManifestEntry("WithTrailingNewline", "trailing\n");

MavenArchiver archiver = new MavenArchiver();
Manifest manifest = archiver.getManifest(session, project, config);
Attributes attributes = manifest.getMainAttributes();

assertThat(attributes.getValue("MultiLine")).isEqualTo("line1 line2 line3");
assertThat(attributes.getValue("WithCRLF")).isEqualTo("line1 line2");
assertThat(attributes.getValue("WithTrailingNewline")).doesNotContain("\n");
}

@Test
void manifestEntryWithEmptyLinesDoesNotCorruptManifest() throws Exception {
File jarFile = new File("target/test/dummy.jar");
JarArchiver jarArchiver = getCleanJarArchiver(jarFile);

MavenArchiver archiver = getMavenArchiver(jarArchiver);

ProjectStub project = getDummyProject();
MavenArchiveConfiguration config = new MavenArchiveConfiguration();
config.setForced(true);
config.addManifestEntry("WithEmptyLine", "para1\n\npara3");
config.addManifestEntry("Normal", "value");

archiver.createArchive(session, project, config);
assertThat(jarFile).exists();

Manifest manifest = getJarFileManifest(jarFile);
Attributes attributes = manifest.getMainAttributes();
assertThat(attributes.getValue("WithEmptyLine")).isEqualTo("para1 para3");
assertThat(attributes.getValue("Normal")).isEqualTo("value");
}

@Test
void deprecatedCreateArchiveAPI() throws Exception {
File jarFile = new File("target/test/dummy.jar");
Expand Down
Loading