Clean and Fat Jar in Netbeans

netbeans是采用ANT对代码进行打包部署的,至于什么是ANT,可以参考下面博客链接
http://www.blogjava.net/amigoxie/archive/2007/11/09/159413.html
好吧,这都不是重点,重点是如何将我的java源代码和调用第三方类库编译打包到一个jar文件里面
方便运行。
第一步,选定你当前操作的项目



第二步,打开build.xml文件 在</project>前(倒数第一行前)添加下列代码
 
<!-- provide a path where executable distros should be saved  -->
<property name="release.dir" value="${basedir}/release"/>
<!-- provide the path to the shared native libraries directory -->
<property name="natives.dir" value="../SharedLibraries/lib/natives/"/>
<!-- provide a base name for your executable. -->
<property name="standalone.jar.name" value="HelloWorld"/>
 
<target name="-unjar-and-copy-lib-jars">
    <unjar dest="${build.classes.dir}">
        <fileset dir="lib">
            <include name="**/*.jar"/>
        </fileset>
        <patternset>
            <exclude name="META-INF/**"/>
            <exclude name="/*"/>
        </patternset>
    </unjar>
</target>
 
<target depends="init,compile,-pre-pre-jar,-pre-jar,-unjar-and-copy-lib-jars" name="fat-jar">
    <property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
    <jar destfile="${dist.jar}">
        <fileset dir="${build.classes.dir}"/>
        <manifest>
            <attribute name="Main-Class" value="${main.class}"/>
        </manifest>
    </jar>
    <echo>To run this application from the command line without Ant, try:</echo>
    <property location="${dist.jar}" name="dist.jar.resolved"/>
    <echo>java -jar "${dist.jar.resolved}"</echo>
</target>
 
<target depends="clean,fat-jar" name="clean-and-fat-jar"/>



第三步
,右键你的build.xml文件按照选择运行目标-》其他目标》clean-and-fat-jar就可以完成打包了

第四,在project文件夹下dist文件下便是打包好的jar文件了

参考
http://www.btechclasses.com/java/clean-and-fat-jar-in-netbeans/

你可能感兴趣的:(Netbeans,fat-jar)