如何使用 Maven 将属性添加到 java 清单
How to add properties to java Manifest using Maven
我从 Java 8 迁移到 Java 11,从 Ant 项目迁移到 Maven 项目,清单丢失了项目版本和构建日期信息。
我使用以下代码打印了清单的所有主要属性:
manifest = new Manifest(new URL(manifestPath).openStream());
Attributes attr = manifest.getMainAttributes();
// Enumerate each attribute
for (Iterator it=attr.keySet().iterator(); it.hasNext(); ) {
// Get attribute name
Attributes.Name attrName = (Attributes.Name)it.next();
String attrValue = attr.getValue(attrName);
AppLogger.getInstance().Log(attrName + ": " + attrValue);
}
这是我得到的所有属性名称:
Main-Class, Class-Path, Build-Jdk, Built-By, Created-By, Manifest-Version, jar
没有项目版本或构建日期。
我应该在我的 POM 文件中做哪些更改才能添加此信息?
您可以在 pom.xml:
中执行类似的操作
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<archive>
<manifestEntries>
<Build-Jdk>${java.version} (${java.vendor} ${java.vm.version})</Build-Jdk>
<Build-OS>${os.name} ${os.arch} ${os.version}</Build-OS>
<Build-Timestamp>${maven.build.timestamp}</Build-Timestamp>
<Project-Version>${project.version}</Project-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
参考文献:https://andresalmiray.com/customize-jar-manifest-entries-with-maven-gradle/, https://maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html
我从 Java 8 迁移到 Java 11,从 Ant 项目迁移到 Maven 项目,清单丢失了项目版本和构建日期信息。
我使用以下代码打印了清单的所有主要属性:
manifest = new Manifest(new URL(manifestPath).openStream());
Attributes attr = manifest.getMainAttributes();
// Enumerate each attribute
for (Iterator it=attr.keySet().iterator(); it.hasNext(); ) {
// Get attribute name
Attributes.Name attrName = (Attributes.Name)it.next();
String attrValue = attr.getValue(attrName);
AppLogger.getInstance().Log(attrName + ": " + attrValue);
}
这是我得到的所有属性名称: Main-Class, Class-Path, Build-Jdk, Built-By, Created-By, Manifest-Version, jar
没有项目版本或构建日期。
我应该在我的 POM 文件中做哪些更改才能添加此信息?
您可以在 pom.xml:
中执行类似的操作 <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<archive>
<manifestEntries>
<Build-Jdk>${java.version} (${java.vendor} ${java.vm.version})</Build-Jdk>
<Build-OS>${os.name} ${os.arch} ${os.version}</Build-OS>
<Build-Timestamp>${maven.build.timestamp}</Build-Timestamp>
<Project-Version>${project.version}</Project-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
参考文献:https://andresalmiray.com/customize-jar-manifest-entries-with-maven-gradle/, https://maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html