RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 884084
Accepted
Rootware
Rootware
Asked:2020-09-22 03:14:14 +0000 UTC2020-09-22 03:14:14 +0000 UTC 2020-09-22 03:14:14 +0000 UTC

从 MANIFEST.MF 中读取属性

  • 772

编译Java应用程序时,会将数据输入到MANIFEST.MF文件中,然后在启动应用程序时使用该文件。清单文件包含编译后所需的所有内容。

Ant 脚本的部分代码:

<manifest>
    <attribute name="Built-By" value="${user.name}" />
    <attribute name="Built-Date" value="${build.tstamp}" />
    <attribute name="Implementation-Version" value="1.0.0.0" />
    <attribute name="Implementation-URL" value="http://example.com/" />
    <attribute name="Main-Class" value="src.app.MyApp" />
    <attribute name="Class-Path" value="${manifest.libs}" />
</manifest>

编译后的清单内容:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.10.1
Created-By: 1.8.0_151-b12 (Oracle Corporation)
Built-By: Rootware
Built-Date: 2018-09-21 21:59:11
Implementation-Version: 1.0.0.0
Implementation-URL: http://example.com/
Main-Class: src.app.MyApp
Class-Path: ../libs/c3p0-0.9.6-pre1.jar ../libs/json-simple-1.1.1.jar 
 ../libs/mchange-commons-java-0.2.12.jar ../libs/mysql-connector-java-
 5.1.46.jar

读取属性的Java代码:

private static void getBuildDate()
{
    try (InputStream stream = MyApp.class.getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF"))
    {
        final Manifest manifest = new Manifest();
        manifest.read(stream);
        final Attributes attributes = manifest.getMainAttributes();

        if (attributes.getValue("Built-Date") != null)
            BUILD_DATE = attributes.getValue("Built-Date");
        else
            _log.info("Null attribute.");
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

读取Built-Date属性时始终为null。告诉我在阅读清单时我做错了什么?如果您尝试阅读Implementation-Version,则会显示Class-Path列表中的库列表中指定的第一个库的0.9.6-pre1版本。

java
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Ростислав Красный
    2020-09-22T04:28:15Z2020-09-22T04:28:15Z

    永远不要尝试将其MANIFEST.MF作为资源读取,因为您的类路径中可以有多个 JAR 文件,并且每个都有自己的META-INF/MANIFEST.MF. 运行时读取MANIFEST.MF是程序中出现错误和未定义行为的途径。我是根据在一个项目中工作的个人经验说的,该项目也有人决定这样做。相反,properties在构建时创建一个文件并在那里记录构建日期。

    我不知道如何在 Ant 中做到这一点。在 Maven 中,这很简单:

    1. 创建另一个资源目录src/main/resources-filtered,并在其中com/example/myprog/build.properties包含以下内容:

      build.date=${build.date}
      
    2. 在pom.xml该部分中,<build>指定两个资源目录以及在其复制期间启用资源处理模式的资源目录:

      <resources>
          <resource>
              <directory>src/main/resources</directory>
          </resource>
          <resource>
              <directory>src/main/resources-filtered</directory>
              <filtering>true</filtering>
          </resource>
      </resources>
      
    3. 在同pom.xml一部分中,您<properties>编写了两个新参数:

      <build.date>${maven.build.timestamp}</build.date>
      <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
      

    接下来,在您的代码中,打开资源com/example/myprog/build.properties,从中创建一个实例Properties并从那里读取您在那里编写的所有内容。

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5