Use ant achieve three MyEclipse in the way packaged jar

Packaging can be run with a ant jar file (third-party jar package into your own jar package)

Run the file by double-clicking or Jar

Java –jar xxxx.jar

To be able to run direct entry must be added to the class in the MANIFEST.MF file in the jar package, it is to bring the Main function.

例如:Main-Class: com.safx.processor.Starter

Add to this the Main-Class attribute in the MANIFEST.MF file, run the jar when the package is called the default class as an entry class.

3 in the following manner using the ant jar file packaged include:

1. Extract third-party jar package your class files and then packaged together into a jar file

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project SeleniumAutomation-3.1">
    <!--this file was created by Eclipse Runnable JAR Export Wizard-->
    <!--ANT 1.7 is required                                        -->
    <target name="create_run_jar">
        <jar destfile="C:/Users/topcat/Desktop/111.jar" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Main-Class" value="com.safx.processor.Starter"/>
                <attribute name="Class-Path" value="."/>
            </manifest>
            <fileset dir="D:/SeleniumAutomation/SAFX-3.1/bin"/>
            <zipfileset excludes="META-INF/*.SF" src="D:/SeleniumAutomation/SAFX-3.1/lib/jaxen-1.1-beta-6.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="D:/SeleniumAutomation/SAFX-3.1/lib/log4j-1.2.16.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="D:/SeleniumAutomation/SAFX-3.1/lib/mail.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="D:/SeleniumAutomation/SAFX-3.1/lib/testng-6.0.1.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="D:/SeleniumAutomation/SAFX-3.1/lib/dom4j-1.6.1.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="D:/SeleniumAutomation/SAFX-3.1/lib/guava-r09.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="D:/SeleniumAutomation/SAFX-3.1/lib/jxl.jar"/>
            <= zipfileset excludes "the META-INF / SF *." the src = "D: /SeleniumAutomation/SAFX-3.1/lib/selenium-java-2.21.0.jar" />
        </ JAR>
    </ target>
</ Project >
above <filesetdir = "D: /SeleniumAutomation/SAFX-3.1/bin" /> this paragraph is my own class file path.

Our Main-Class attribute is set to com.safx.processor.Starter

After we packed all of the content using the first method to figure jar file is.

 

2. The third-party jar package directly into the jar package we want to generate

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project SeleniumAutomation-3.1 with Jar-in-Jar Loader">
    <!--this file was created by Eclipse Runnable JAR Export Wizard-->
    <!--ANT 1.7 is required                                        -->
    <target name="create_run_jar">
        <jar destfile="C:/Users/topcat/Desktop/1212.jar">
            <manifest>
                <attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/>
                <attribute name="Rsrc-Main-Class" value="com.safx.processor.Starter"/>
                <attribute name="Class-Path" value="."/>
                <attribute name="Rsrc-Class-Path" value="./ jaxen-1.1-beta-6.jar log4j-1.2.16.jar mail.jar testng-6.0.1.jar dom4j-1.6.1.jar guava-r09.jar jxl.jar selenium-java-2.21.0.jar"/>
            </manifest>
            <zipfileset src="jar-in-jar-loader.zip"/>
            <fileset dir="D:/SeleniumAutomation/SAFX-3.1/bin"/>
            <zipfileset dir="D:\SeleniumAutomation\SAFX-3.1\lib" includes="jaxen-1.1-beta-6.jar"/>
            <zipfileset dir="D:\SeleniumAutomation\SAFX-3.1\lib" includes="log4j-1.2.16.jar"/>
            <zipfileset dir="D:\SeleniumAutomation\SAFX-3.1\lib" includes="mail.jar"/>
            <zipfileset dir="D:\SeleniumAutomation\SAFX-3.1\lib" includes="testng-6.0.1.jar"/>
            <zipfileset the dir = "D: \ SeleniumAutomation \ SAFX-3.1 \ lib" Includes = "the dom4j-1.6.1.jar" />
            <zipfileset the dir = "D: \ SeleniumAutomation \ SAFX-3.1 \ lib" Includes = "guava- r09.jar "/>
            <zipfileset the dir =" D: \ SeleniumAutomation \ SAFX-3.1 \ lib "Includes =" jxl.jar-"/>
            <zipfileset the dir =" D: \ SeleniumAutomation \ SAFX-3.1 \ lib "= Includes" Java-2.21.0.jar-Selenium "/>
        </ JAR>
    </ target>
</ Project>
upper side compared to the first method of ant script more Rsrc-Main-Class Rsrc-Class-Path and two Attributes.

Using this method, we need to introduce a jar-in-jar.zip additional files.

<zipfileset src="jar-in-jar-loader.zip"/>

This is the eclipse comes after the package is completed, will jar-in-jar.zip to extract your jar package

Jar-in-jar.zip included above these class, writing in class

org.eclipse.jdt.internal.jarinjarloader next package

The figure is all the jar files packaged with the bag after our second method.

The figure is all the jar files packaged with the bag after our second method.

 

 

3. Copy the third-party to a jar file (such as lib folder), this folder with our own jar file in the same directory

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project SeleniumAutomation-3.1 with libraries in sub-folder">
    <!--this file was created by Eclipse Runnable JAR Export Wizard-->
    <!--ANT 1.7 is required                                        -->
    <target name="create_run_jar">
        <jar destfile="C:/Users/topcat/Desktop/333.jar">
            <manifest>
                <attribute name="Main-Class" value="com.safx.processor.Starter"/>
                <attribute name="Class-Path" value=". lib/jaxen-1.1-beta-6.jar lib / log4j 1.2.16.jar lib / lib mail.jar / TestNG-6.0.1.jar lib / dom4j-1.6.1.jar lib / guava-r09.jar lib / lib jxl.jar / selenium-java -2.21.0.jar "/>
            </manifest>
            <fileset dir="D:/SeleniumAutomation/SAFX-3.1/bin"/>
        </jar>
        <delete dir="C:/Users/topcat/Desktop/lib"/>
        <mkdir dir="C:/Users/topcat/Desktop/lib"/>
        <copy file="D:/SeleniumAutomation/SAFX-3.1/lib/jaxen-1.1-beta-6.jar" todir="C:/Users/topcat/Desktop/lib"/>
        <copy file="D:/SeleniumAutomation/SAFX-3.1/lib/log4j-1.2.16.jar" todir="C:/Users/topcat/Desktop/lib"/>
        <copy file="D:/SeleniumAutomation/SAFX-3.1/lib/mail.jar" todir="C:/Users/topcat/Desktop/lib"/>
        <copy file="D:/SeleniumAutomation/SAFX-3.1/lib/testng-6.0.1.jar" todir="C:/Users/topcat/Desktop/lib"/>
        <copy file="D:/SeleniumAutomation/SAFX-3.1/lib/dom4j-1.6.1.jar" todir="C:/Users/topcat/Desktop/lib"/>
        <copy file="D:/SeleniumAutomation/SAFX-3.1/lib/guava-r09.jar" todir="C:/Users/topcat/Desktop/lib"/>
        <copy file="D:/SeleniumAutomation/SAFX-3.1/lib/jxl.jar" todir="C:/Users/topcat/Desktop/lib"/>
        <copy file="D:/SeleniumAutomation/SAFX-3.1/lib/selenium-java-2.21.0.jar" todir="C:/Users/topcat/Desktop/lib"/>
    </target>
</project>
 

Above ant script, our own package jar package called 333.jar to the desktop. In the desktop build a lib file, third-party jar files copied to the lib folder. Disposed opposite to third path jar in Class-Path attribute.

The figure is then packaged format is complete.

The third embodiment in the first and second embodiment is different, this method will have a plurality of files, unlike in the first and second embodiment generates only a jar file, but also has the advantage that relatively simple structure. In which mode selection, or depends on personal preference.
--------------------- 
Author: caiqcong 
Source: CSDN 
Original: https: //blog.csdn.net/caiqcong/article/details/7618582 
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/yuan1164345228/article/details/91046745