23

Is it possible to add entries to the manifest.mf file of jars generated by netbeans?

to build an osgi bundle for instance.

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97

8 Answers8

22

Note that you can create a manifest on-the-fly via an ant task and set properties dynamically.

First, you must update your Netbeans "project.properties" file found in the "nbproject" directory. Add the following line to the file:

manifest.file=manifest.mf

Next, create an ant task to create/update the manifest using the "build.xml" file. In this example, we will set the version number and date of the jar file.

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

This will create a manifest file in your netbeans project directory and stuff it into your jar file. If you want to delete the autogenerated manifest file from your netbeans project directory, simply create another ant task (post jar of course):

<target name="-post-jar">
  <delete file="MANIFEST.MF"/>
</target> 
Peter
  • 1,182
  • 2
  • 12
  • 23
  • This works really well, thanks! But why don’t the Codebase and Permissions change ( it is stil set to default). Can this not be changed here? – hgerdin Feb 26 '14 at 08:58
  • `-pre-init` is run before `project.properties` is read, therefore `manifest.file` is unset while making the first target. However, the manifest file should be created before `-do-init`, otherwise `manifest.available` won't be set. – basin Oct 03 '18 at 12:33
  • Thanks, that helped. I have also found a more detailed version of that answer here: https://www.javaxt.com/Tutorials/Netbeans/How_to_Add_Version_Information_to_a_Jar_File_with_Netbeans – Mario Jan 13 '21 at 19:48
6

Interesting information might be here:

http://wiki.netbeans.org/FaqNoMainClass

java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103
  • 2
    This is the correct answer! Simply create the desired manifest.mf in the project root and append "manifest.file=manifest.mf" to file project.properties. Assuming your project isn't setting any values in the manifest dynamically, that is. If you need to get fancy here, it's time to turn to maven! – Kyle W. Cartmell Sep 06 '10 at 18:09
  • Yepp. This is probably the best solution. Though optimally I would like there to be better gui options for modifying what ends up in the manifest file. But for now, this will do. – UmaN Feb 28 '12 at 17:41
2

I have a Java Class Library project with a custom manifest file - perfect for an OSGI bundle. To get this working first edit project.properties and set:

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

Create your own custom manifest.mf file in the project directory.

(At this point if you try a clean/build you still won't get your custom manifest file - NetBeans will provide its own. This is because the build-impl.xml Ant target "-do-jar-with-libraries-without-manifest" is being called immediately after "-do-jar-with-manifest", overwriting your custom manifest JAR file with a default NetBeans manifest JAR.)

Add a custom target to your build.xml file as follows:

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

Tested in NetBeans 6.7.1

gazzamop
  • 21
  • 1
1

in the same dir as the build.xml you can put your manifest.mf file

I'm using Netbeans 6.7.1 Turns out that the build-imp.xml (the actual build script Netbeans uses)

  • doesn't have a target which runs if 'with manifest, without main-class'
  • but it does have one like 'with manifest, with main-class'

So.. make sure you have the project-properties,run,main-Class filled with -anything-

i think that's some undocumented feature :(

this is my manifest content:

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: ( ..... )
Houtman
  • 2,819
  • 2
  • 24
  • 34
  • Yes, but the problem with that is that netbeans refuses any property it doesn't understand, and it doesn't understand much. All the osgi properties such as Bundle-ManifestVersion or Bundle-Activator are rejected. – Maurice Perry Aug 13 '09 at 05:59
  • I don't have that problem in ordinary netbeans 6.7.1 java-project. I can fill in any Bundle-xxxx property in the custom manifest. What happened in my case was that it completely ignored the whole manifest file and created a default manifest. – Houtman Aug 13 '09 at 07:03
1

In case you using maven (nbm-maven-plugin), look at this

NBM Maven plugin

Lukino
  • 1,407
  • 13
  • 14
0

Why not using the a maven project, which worked well for me? E.g. apache felix

See this pluggable Swing example which I created in netbeans.

Karussell
  • 17,085
  • 16
  • 97
  • 197
0

You can edit the nbproject/build-impl.xml adding the necessary properties like this:

....
<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>
....

This will result in a MANIFEST.MF in jar file like this:

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

Tested on Netbeans 8.1.

czdepski
  • 316
  • 4
  • 9
-2

See this article.

Here it is described how to

  • create own ant targets
  • add manual entries to manifest.mf for the output JAR
  • run custom ant targets from Netbeans
ivan_ivanovich_ivanoff
  • 19,113
  • 27
  • 81
  • 100
  • 1
    The referenced article doesn't speak very directly to this question. If you're referring to the custom ant task ... that's not a very good approach to the simple task of adding an entry to the manifest, IMHO. – Kyle W. Cartmell Sep 06 '10 at 17:58
  • There is no article on the link anymore. – czdepski Oct 09 '18 at 17:52