如何使用 Maven 将属性添加到 java Manifest

发布于 2025-01-13 00:27:48 字数 770 浏览 2 评论 0原文

我从 Java 8 迁移到 Java 11,从 Ant 项目迁移到 Maven 项目,清单丢失了项目版本和构建日期信息。

我使用以下代码打印了 Manifest 的所有主要属性:

        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 文件中进行哪些更改才能添加此信息?

I migrated from Java 8 to Java 11 and from Ant project to Maven project, the manifest lost the Project version and build date information.

I printed all Main attributes of Manifest using the following code:

        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);

            }

Here is all the attributes names that I get:
Main-Class, Class-Path, Build-Jdk, Built-By, Created-By, Manifest-Version, jar

There is no project version or build date.

What change should I do in my POM file, to add this information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长不大的小祸害 2025-01-20 00:27:48

您可以在 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

You can do something like this in your 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>

References: https://andresalmiray.com/customize-jar-manifest-entries-with-maven-gradle/, https://maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文