-使用Netbeans 6.7打包第三方jar

 

1. 正常打包

在项目上右键,然后选择【Build】即可。
-使用Netbeans 6.7打包第三方jar

打包完成后,会在项目的主目录下,生成一个[dist]文件夹。可以看到所有的第三方包都被放到了lib目录下,而不是形成单个的jar包。

2. 打包第三方jar

搜索了很久才找到这个方法,来自于Sun官方网站,原文在这里

在NetBeans界面的左上方,切换到【Files】标签。打开项目目录下的build.xml文件。

-使用Netbeans 6.7打包第三方jar

在build.xml的最后一行

1
</project>

的前面,加入以下代码。其中,第7行中的value=”MarsRoverViewer”可以改成其他名字。比如你想生成abc.jar,那么改成value=”abc”就可以了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    <target name="package-for-store" depends="jar">
 
        <!-- Change the value of this property to be the name of your JAR,
             minus the .jar extension. It should not have spaces.
             <property name="store.jar.name" value="MyJarName"/>
        -->
        <property name="store.jar.name" value="MarsRoverViewer"/>
 
 
        <!-- don't edit below this line -->
 
        <property name="store.dir" value="store"/>
        <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
 
        <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
 
        <delete dir="${store.dir}"/>
        <mkdir dir="${store.dir}"/>
 
        <jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
            <zipgroupfileset dir="dist" includes="*.jar"/>
            <zipgroupfileset dir="dist/lib" includes="*.jar"/>
 
            <manifest>
                <attribute name="Main-Class" value="${main.class}"/>
            </manifest>
        </jar>
 
        <zip destfile="${store.jar}">
            <zipfileset src="${store.dir}/temp_final.jar"
            excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
        </zip>
 
        <delete file="${store.dir}/temp_final.jar"/>
 
    </target>

改完build.xml后保存,在该文件上右键,依次选择【Run Target】【Other Targets】【package-for-store】
-使用Netbeans 6.7打包第三方jar

等待一会儿运行完之后,可以在项目主目录下发现一个[store]文件夹,里面就是打包好的一个单一的jar包。
-使用Netbeans 6.7打包第三方jar

3. 命令行打包第三方jar

用命令行方式打包应该是最为灵活强大的,可是目前为止我还不会。

 

Use NetBeans IDE 6.7 to Combine JAR Files Into a Single JAR File

 
By Robert Eckstein, August 2009  

Articles Index

The Java Warehouse is the repository for Java and JavaFX applications submitted by developers for distribution through the Java Store to hundreds of millions of desktops worldwide. The Java Warehouse Developer Center Release Notes make clear that there is currently no way to upload applications composed of several Java Archive (JAR) files into the Java Warehouse. However, there is a workaround: Just concatenate each of the user and library classes into a single JAR file. This is very easy to do for both Java and JavaFX projects using NetBeans IDE 6.7.1.

To demonstrate how to do this, let's use one of the sample Java projects that is included in the NetBeans IDE 6.7.1 distribution: the Mars Rover Viewer. Open the NetBeans IDE, select File > New Project, then scroll down to the Samples folder and choose Java from the Categories pane. From there, select Mars Rover Viewer in the Projects pane and click Next, as shown in Figure 1. Next, choose a suitable location for the project and click Finish.

Figure 1 - Creating the Mars Rover Viewer Sample Project in NetBeans.
 

We've chosen this project because it makes use of a popular compile-time library that is not included with the standard Java Development Kit (JDK): the Swing Application Framework. To verify that this library is being used, right-click on the project in the upper left pane and choose Properties. From there, select Libraries on the left pane to verify that the Swing Application Framework is indeed listed, as shown in Figure 2. Then click OK to close the Project Properties dialog box.

Figure 2 - The Mars Rover Viewer Application Uses the Swing Application Framework Library.
 

In order to bundle all of the libraries into a single JAR file, you will need to edit the project's Ant file, which is called build.xml. Choose the Files tab in the Project window in the upper left, then expand the project tab if necessary to show the build.xml file. Double-click this file to edit it in the source-code editor pane and scroll down to the bottom. Now copy and paste the following text to the end, just before the final line, which is the</project> closing tag:

    <target name="package-for-store" depends="jar">

        <!-- Change the value of this property to be the name of your JAR,
             minus the .jar extension. It should not have spaces.
             <property name="store.jar.name" value="MyJarName"/>
        -->
        <property name="store.jar.name" value="MarsRoverViewer"/>


        <!-- don't edit below this line -->

        <property name="store.dir" value="store"/>
        <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>

        <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>

        <delete dir="${store.dir}"/>
        <mkdir dir="${store.dir}"/>

        <jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
            <zipgroupfileset dir="dist" includes="*.jar"/>
            <zipgroupfileset dir="dist/lib" includes="*.jar"/>

            <manifest>
                <attribute name="Main-Class" value="${main.class}"/>
            </manifest>
        </jar>

        <zip destfile="${store.jar}">
            <zipfileset src="${store.dir}/temp_final.jar"
            excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
        </zip>

        <delete file="${store.dir}/temp_final.jar"/>

    </target>
 

Note that you must change the following line in the Ant code above to match your particular project.

<property name="store.jar.name" value="MarsRoverViewer"/>

The property store.jar.name specifies the name of the JAR file that will be created in the store directory — change the value to whatever name you like. After you have changed this name, save the build.xml file. Figure 3 shows the additions to the build.xml file for the sample MarsRoverViewer project relative to the closing </project> tag.

Figure 3 - Additions to the Project's  build.xml File Relative to the Closing Tag.
 

From here, you can continue normal development, editing Java source files and compiling and running as necessary. Figure 4 shows the Mars Rover Viewer application fully built and running within the NetBeans IDE.

Figure 4 - Running the Mars Rover Viewer Within the NetBeans IDE.
 

When you're ready to package all of the classes and libraries into a single JAR file, switch back to the Files view in the upper left, then right-click the build.xml file. Choose Run Target, scroll down to select Other Targets, then choose package-for-store, as shown in Figure 5, to execute the target. In this case, package-for-store is the name of the Ant target that you created previously in the project build.xml file.

Figure 5 - Executing the  package-for-store Ant Target From the Files Tab Inside the NetBeans IDE.
 

Executing this Ant target will create a directory called store inside your project directory. NetBeans and Ant will then copy all of the classes and libraries into a temporary JAR file and strip out any unwanted files, placing the final result under the JAR name that you specified earlier. NetBeans will then delete the temporary file. The results are shown in Figure 6.

Figure 6 - The Final JAR File, Ready to Upload to the Java Warehouse.
 

That's all there is to it — you can use this JAR file to upload to the Java Warehouse. Note that if you get an error stating that the dist/lib directory was not found, you should check that you have indeed included other libraries in your project, as this is the location where their JAR files will be stored.

If you'd like to verify that the JAR file contains the appropriate libraries, you can use one of three methods:

  • Use the command-line tool: jar tvf (filename).
  • Use a number of OS-based tools to inspect the JAR files (or ZIP files, if you change the extension).
  • Double-click on the JAR file to be sure that the Java runtime can execute it.

For More Information

 
 

你可能感兴趣的:(Netbeans)