23

是否可以将条目添加到 netbeans 生成的 jar 的 manifest.mf 文件中?

例如构建一个 osgi 包。

4

8 回答 8

22

请注意,您可以通过 ant 任务即时创建清单并动态设置属性。

首先,您必须更新“nbproject”目录中的 Netbeans“project.properties”文件。将以下行添加到文件中:

manifest.file=manifest.mf

接下来,创建一个 ant 任务以使用“build.xml”文件创建/更新清单。在本例中,我们将设置 jar 文件的版本号和日期。

<target name="-pre-init">
   <property name="project.name" value="My Library" />
   <property name="version.num" value="1.4.1" />
   <tstamp>
      <format property="NOW" pattern="yyyy-MM-dd HH:mm:ss z" />
   </tstamp>

   <!--
   <exec outputproperty="svna.version" executable="svnversion">
       <arg value="-c" />
       <redirector>
           <outputfilterchain>
               <tokenfilter>
                   <replaceregex pattern="^[0-9]*:?" replace="" flags="g"/>
                   <replaceregex pattern="M" replace="" flags="g"/>
               </tokenfilter>
           </outputfilterchain>
       </redirector>
   </exec>
   -->


   <manifest file="MANIFEST.MF">
      <attribute name="Bundle-Name" value="${project.name}" />           
      <attribute name="Bundle-Version" value="${version.num}" />
      <attribute name="Bundle-Date" value="${NOW}" />
      <!--<attribute name="Bundle-Revision" value="${svna.version}" />-->
      <attribute name="Implementation-Title" value="${project.name}" />
      <attribute name="Implementation-Version" value="${version.num}" />
      <attribute name="Implementation-URL" value="http://www.example.com" />
   </manifest>

</target>

这将在您的 netbeans 项目目录中创建一个清单文件并将其填充到您的 jar 文件中。如果您想从您的 netbeans 项目目录中删除自动生成的清单文件,只需创建另一个 ant 任务(当然是发布 jar):

<target name="-post-jar">
  <delete file="MANIFEST.MF"/>
</target> 
于 2011-12-04T12:53:52.037 回答
6

有趣的信息可能在这里:

http://wiki.netbeans.org/FaqNoMainClass

于 2009-10-22T14:49:17.220 回答
2

我有一个带有自定义清单文件的 Java 类库项目 - 非常适合 OSGI 包。要让这个工作首先编辑 project.properties 并设置:

manifest.file=manifest.mf
manifest.available=true

在项目目录中创建您自己的自定义 manifest.mf 文件。

(此时,如果您尝试清理/构建,您仍然不会获得自定义清单文件 - NetBeans 将提供自己的。这是因为 build-impl.xml Ant 目标“-do-jar-with-libraries-without -manifest”在“-do-jar-with-manifest”之后立即被调用,用默认的 NetBeans 清单 JAR 覆盖您的自定义清单 JAR 文件。)

将自定义目标添加到 build.xml 文件中,如下所示:

<target name="-do-jar-with-libraries-without-manifest">
    <!-- Inserted to prevent target from running so we can have a custom
         manifest file with a class library project type. -->
</target>

在 NetBeans 6.7.1 中测试

于 2010-09-26T06:38:27.887 回答
1

在与 build.xml 相同的目录中,您可以将 manifest.mf 文件

我正在使用 Netbeans 6.7.1 原来 build-imp.xml (Netbeans 使用的实际构建脚本)

  • 如果“有清单,没有主类”,则没有运行目标
  • 但它确实有一个像“带有清单,带有主类”

所以..确保你的项目属性,运行,主类充满了-anything-

我认为这是一些未记录的功能:(

这是我的清单内容:

Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
Bundle-ManifestVersion: 2
Bundle-Name: jinstall
Bundle-SymbolicName: jinstall
Import-Package: ( .... )
Export-Package: ( .... )
Bundle-Activator: ( ..... )
于 2009-08-12T15:06:57.097 回答
1

如果您使用 maven (nbm-maven-plugin),请查看此

NBM Maven 插件

于 2013-11-18T12:55:05.280 回答
0

为什么不使用对我来说效果很好的 maven 项目呢?例如阿帕奇菲利克斯

请参阅我在 netbeans 中创建的这个可插入的 Swing 示例。

于 2009-11-07T12:17:40.383 回答
0

您可以编辑nbproject/build-impl.xml添加必要的属性,如下所示:

....
<target depends="init,-do-jar-create-manifest,-do-jar-copy-manifest" if="do.archive+main.class.available" name="-do-jar-set-mainclass">
    <manifest encoding="UTF-8" file="${tmp.manifest.file}" mode="update">
        <attribute name="Main-Class" value="${main.class}"/>
        <attribute name="Property1" value="foo"/>
        <attribute name="Property2" value="bar"/>
    </manifest>
</target>
....

这将产生一个MANIFEST.MF像这样的 jar 文件:

Manifest-Version: 1.0
...
Property1: foo
Property2: bar

在 Netbeans 8.1 上测试。

于 2018-10-09T20:39:35.797 回答
-2

这篇文章

这里描述了如何

  • 创建自己的蚂蚁目标
  • 为输出 JAR 添加手动条目到 manifest.mf
  • 从 Netbeans 运行自定义 ant 目标
于 2009-09-29T10:08:56.587 回答