以编程方式替换 JAR 中的 MANIFEST.MF 文件
Replacing the MANIFEST.MF file in a JAR programmatically
我想在创建 JAR
后将 MANIFEST.MF
修改为 exclude certain Class-Path
entries. For this I decided to use zip4j
。提取似乎工作正常但是为了将 MANIFEST.MF
文件放回 JAR
,我使用以下代码:
String metaInfFolderName = "META-INF";
Path extractedManifestFilePath = Paths.get("...");
ZipFile zipFile = new ZipFile("Test-Zip-File.zip");
ZipParameters zipParameters = new ZipParameters();
zipParameters.setDefaultFolderPath(metaInfFolderName);
zipFile.addFile(extractedManifestFilePath.toFile(), zipParameters);
但是,此代码无法按预期工作:父目录最终总是被命名为 NF
而不是完整的 META-INF
。似乎起始字符被切断了。这可能是什么原因,或者是否有另一种有意义的可能性来替换 JAR
s(实际上只是 ZIP
s)内的文件?
maven
依赖关系:
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.6.1</version>
</dependency>
此外,我尝试使用 jar
实用程序,如 here 所述,但是当调用命令 jar uf MyJAR.jar META-INF/MANIFEST.MF
时, JAR
中的 MANIFEST.MF
被删除而不是更换。通过 zip -ur MyJAR.jar "META-INF/MANIFEST.MF"
使用 zip
实用程序可以工作,但会损坏 JAR
文件,因此它不再可运行:
Error: An unexpected error occurred while trying to open file MyJAR.jar
你可以这样使用:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.Manifest;
public class ManifestManipulator {
public static final void main(String... args) throws IOException {
if (args.length == 0) {
throw new IllegalArgumentException("at least the path to the JAR file expected!");
}
Path jarPath = Paths.get(args[0]);
Set<String> attributeNames = args.length > 1 ? new LinkedHashSet<>(List.of(args).subList(1, args.length)) : Set.of();
if (!attributeNames.isEmpty()) {
ManifestManipulator manifestManipulator = new ManifestManipulator();
manifestManipulator.removeManifestEntries(jarPath, attributeNames);
} else {
System.out.println("Warning: no entries specified to remove!");
}
}
private void removeManifestEntries(Path jarPath, Set<String> attributeNames) throws IOException {
System.out.println("Going to remove: " + attributeNames);
try (FileSystem jarFS = FileSystems.newFileSystem(URI.create("jar:" + jarPath.toUri()), Map.of())) {
Path manifestPath = jarFS.getPath("META-INF", "MANIFEST.MF");
Manifest manifest = readManifest(manifestPath);
Attributes mainAttributes = manifest.getMainAttributes();
System.out.println("Found main attribute names: " + mainAttributes.keySet());
boolean removed = mainAttributes.entrySet().removeIf(entry -> attributeNames.contains(((Name) entry.getKey()).toString()));
if (removed) {
writeManifest(manifestPath, manifest);
} else {
System.out.println("Warning: nothing removed");
}
}
}
private Manifest readManifest(Path manifestPath) throws IOException {
try (InputStream is = Files.newInputStream(manifestPath)) {
return new Manifest(is);
}
}
private void writeManifest(Path manifestPath, Manifest manifest) throws IOException {
try (OutputStream os = Files.newOutputStream(manifestPath)) {
manifest.write(os);
}
}
}
请确保您已添加 jdk.zipfs module, which will provide a FileSystemProvider for ZIP/JAR files (see the technote)。
你不需要任何外部依赖来改变 ZIP 或 JAR 文件的内容 Java 8.
public static void main(String[] args) throws IOException {
Path zipFilePath = Paths.get("path/to/my/app.jar");
try (FileSystem zipFileSystem = FileSystems.newFileSystem(zipFilePath, null)) {
Path manifestFile = zipFileSystem.getPath("META-INF/MANIFEST.MF");
String newManifestContent;
// Read from MANIFEST.MF.
try (Stream<String> lines = Files.lines(manifestFile, StandardCharsets.UTF_8)) {
newManifestContent = lines.filter(l -> !l.startsWith("Class-Path entry I want to remove"))
.collect(Collectors.joining("\n"));
}
// Replace MANIFEST.MF content.
Files.write(manifestFile, newManifestContent.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
}
}
我想在创建 JAR
后将 MANIFEST.MF
修改为 exclude certain Class-Path
entries. For this I decided to use zip4j
。提取似乎工作正常但是为了将 MANIFEST.MF
文件放回 JAR
,我使用以下代码:
String metaInfFolderName = "META-INF";
Path extractedManifestFilePath = Paths.get("...");
ZipFile zipFile = new ZipFile("Test-Zip-File.zip");
ZipParameters zipParameters = new ZipParameters();
zipParameters.setDefaultFolderPath(metaInfFolderName);
zipFile.addFile(extractedManifestFilePath.toFile(), zipParameters);
但是,此代码无法按预期工作:父目录最终总是被命名为 NF
而不是完整的 META-INF
。似乎起始字符被切断了。这可能是什么原因,或者是否有另一种有意义的可能性来替换 JAR
s(实际上只是 ZIP
s)内的文件?
maven
依赖关系:
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.6.1</version>
</dependency>
此外,我尝试使用 jar
实用程序,如 here 所述,但是当调用命令 jar uf MyJAR.jar META-INF/MANIFEST.MF
时, JAR
中的 MANIFEST.MF
被删除而不是更换。通过 zip -ur MyJAR.jar "META-INF/MANIFEST.MF"
使用 zip
实用程序可以工作,但会损坏 JAR
文件,因此它不再可运行:
Error: An unexpected error occurred while trying to open file MyJAR.jar
你可以这样使用:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.Manifest;
public class ManifestManipulator {
public static final void main(String... args) throws IOException {
if (args.length == 0) {
throw new IllegalArgumentException("at least the path to the JAR file expected!");
}
Path jarPath = Paths.get(args[0]);
Set<String> attributeNames = args.length > 1 ? new LinkedHashSet<>(List.of(args).subList(1, args.length)) : Set.of();
if (!attributeNames.isEmpty()) {
ManifestManipulator manifestManipulator = new ManifestManipulator();
manifestManipulator.removeManifestEntries(jarPath, attributeNames);
} else {
System.out.println("Warning: no entries specified to remove!");
}
}
private void removeManifestEntries(Path jarPath, Set<String> attributeNames) throws IOException {
System.out.println("Going to remove: " + attributeNames);
try (FileSystem jarFS = FileSystems.newFileSystem(URI.create("jar:" + jarPath.toUri()), Map.of())) {
Path manifestPath = jarFS.getPath("META-INF", "MANIFEST.MF");
Manifest manifest = readManifest(manifestPath);
Attributes mainAttributes = manifest.getMainAttributes();
System.out.println("Found main attribute names: " + mainAttributes.keySet());
boolean removed = mainAttributes.entrySet().removeIf(entry -> attributeNames.contains(((Name) entry.getKey()).toString()));
if (removed) {
writeManifest(manifestPath, manifest);
} else {
System.out.println("Warning: nothing removed");
}
}
}
private Manifest readManifest(Path manifestPath) throws IOException {
try (InputStream is = Files.newInputStream(manifestPath)) {
return new Manifest(is);
}
}
private void writeManifest(Path manifestPath, Manifest manifest) throws IOException {
try (OutputStream os = Files.newOutputStream(manifestPath)) {
manifest.write(os);
}
}
}
请确保您已添加 jdk.zipfs module, which will provide a FileSystemProvider for ZIP/JAR files (see the technote)。
你不需要任何外部依赖来改变 ZIP 或 JAR 文件的内容 Java 8.
public static void main(String[] args) throws IOException {
Path zipFilePath = Paths.get("path/to/my/app.jar");
try (FileSystem zipFileSystem = FileSystems.newFileSystem(zipFilePath, null)) {
Path manifestFile = zipFileSystem.getPath("META-INF/MANIFEST.MF");
String newManifestContent;
// Read from MANIFEST.MF.
try (Stream<String> lines = Files.lines(manifestFile, StandardCharsets.UTF_8)) {
newManifestContent = lines.filter(l -> !l.startsWith("Class-Path entry I want to remove"))
.collect(Collectors.joining("\n"));
}
// Replace MANIFEST.MF content.
Files.write(manifestFile, newManifestContent.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
}
}