如何使用Ant创建可执行jar

时间:2014-07-09 12:59:41

标签: java eclipse ant jar build

在Eclipse中,我使用ant来创建.jar文件

我需要在另一个项目中调试jar。 我如何生成一个包含调试源代码的jar?

编辑: 我使用:

创建jar

<jar destfile="${my.lib}/${myProject}.jar" basedir="${eclipse.workspace}/${myProject}/bin">

2 个答案:

答案 0 :(得分:2)

包含jar中的源文件 ,这就足够了:

<jar destfile="${my.lib}/${myProject}.jar" basedir="${eclipse.workspace}/${myProject}" includes="bin/**,src/**"/>  

但是,你已经在你的问题中复制了create an executable jar,为此,这应该有效:

<target name="abc">
    ...
     <manifest file = "MANIFEST.MF">
        <attribute name = "Main-Class" value = "yourClassName"/>
        <attribute name = "Class-Path" value = "."/>
     </manifest>
     <jar destfile="${my.lib}/${myProject}.jar" basedir="${eclipse.workspace}/${myProject}" includes="bin/**,src/**" manifest="MANIFEST.MF"/> 
</target>

答案 1 :(得分:0)

使用Apache Ant任务zipgroupfileset

创建可运行的jar 文件
<manifest file = "MANIFEST.MF"> <!-- Manifest-Version: 1.0 -->
    <attribute name = "Main-Class"  value = "${RunnableMain-Class}"/>
    <attribute name = "Class-Path"  value = "."/>
</manifest>
<jar destfile="${exportDestination}/${jarFileName}.jar" basedir="${class-files}"
     manifest="MANIFEST.MF">
    <fileset dir="${class-files}" includes="**/*.class" />
    <zipgroupfileset dir="${buildPath-Libraries}" includes="**/*.jar" />
</jar>

全长示例:

 

<property name="exportDestination"       value="${basedir}"/>

<property name="ProjectName"             value="${basedir}/MyRunnableJAR"/>
<property name="jarFileName"             value="myRunnableJAR"/>
<property name="RunnableMain-Class"      value="org.openqa.grid.selenium.GridLauncher"/>

<property name="class-files"             value="${ProjectName}/bin" />
<property name="buildPath-Libraries"     value="${ProjectName}/lib"/>
<property name="source-dir"              value="${ProjectName}/src" />

<property name="Ant-Version"             value="Apache Ant 1.8.2"/>
<property name="Built-By"                value="Yashwanth.Merugu"/>

<path id="buildLibraries"> <!-- http://ant.apache.org/manual/using.html#path -->
    <fileset dir="${buildPath-Libraries}">
        <include name="*.jar"/>
    </fileset>
</path>
<!-- compile -->
<target name="compile" >
    <mkdir dir="${class-files}"/>

    <javac srcdir="${source-dir}" destdir="${class-files}" includeantruntime="false" encoding="iso-8859-1">
        <classpath refid="buildLibraries"/>
    </javac>
</target>
<!-- jar -->
<target name="jar" depends="compile">

    <manifest file = "MANIFEST.MF"> <!-- Manifest-Version: 1.0 -->
        <attribute name = "Built-By"    value = "${Built-By}"/>
        <attribute name = "Ant-Version" value = "${Ant-Version}"/>
        <attribute name = "Main-Class"  value = "${RunnableMain-Class}"/>
        <attribute name = "Class-Path"  value = "."/>
    </manifest>

    <jar destfile="${exportDestination}/${jarFileName}.jar" basedir="${class-files}" manifest="MANIFEST.MF">
        <fileset dir="${class-files}" includes="**/*.class" />
        <zipgroupfileset dir="${buildPath-Libraries}" includes="**/*.jar" />
    </jar>
</target>

从Eclipse创建Runnable JAR File