1

I want to build my project using ant but I have a small problem. My problem is that I need the output jar have all my .class and all my jar dependencies extracted not zipped.

<project name="ivy example" default="compress" xmlns:ivy="antlib:org.apache.ivy.ant">

    <target name="resolve" description="Resolve and retrieve with ivy">
        <ivy:resolve />
        <ivy:cachepath pathid="compile.path" />
    </target>

    <target name="compile" depends="resolve" description="compilation">
       <mkdir dir="build/classes" />
       <javac srcdir="src" destdir="build/classes">
           <classpath refid="compile.path" />
       </javac>
    </target>

    <target name="compress"  depends="compile">
        <jar destfile="output/engine.jar" filesonly="true" update="true">
        <fileset dir="build/classes" />
        <fileset dir="lib"/>
            <manifest>
                <attribute name="Created-By" value="vireton"/>
                <attribute name="Main-Class" value="HelloIvy"/>
            </manifest>
        </jar>
        <echo>Building .jar file completed successfully!</echo>
    </target>

</project>

That code generates my engine.jar with the output classes + dependencies.jar. I want it to generate my classes and the dependencies extracted.

Can anyone help?

4

3 回答 3

1

我通过将依赖项提取到 tmp 目录然后制作两个目录(src & tmp)的 jar 来做到这一点

  <target name="compress"  depends="compile">
<delete file="output/engine.jar" />
    <mkdir dir="tmp" />
    <unzip dest="tmp">
        <fileset dir="lib"> 
            <include name="*.jar" />
        </fileset>
    </unzip>
            <delete dir="tmp/META-INF" />
        <jar destfile="output/engine.jar" update="true">
        <fileset dir="build/classes" />
        <fileset dir="tmp"/>
            <manifest>
              <attribute name="Created-By" value="vireton"/>
              <attribute name="Main-Class" value="HelloIvy"/>
            </manifest>         
    </jar>  
     </target>

我找到了一种更好的方法来使用

 <target name="compress"  depends="compile">
     <delete file="output/engine.jar" />
        <jar destfile="output/engine.jar" update="true">
        <zipgroupfileset dir="lib" includes="*.jar"/>
        <zipfileset dir="build/classes" />          
    </jar>  
   </target>
于 2013-06-02T08:59:54.643 回答
0

您的依赖项 jar 文件可用作路径,这要归功于

    <ivy:cachepath pathid="compile.path" />

因此将它们全部捆绑到最终 jar 中的最简单方法是(假设 Ant 1.8 或更高版本)

  <jar destfile="output/engine.jar" filesonly="true" update="true">
    <fileset dir="build/classes" />
    <archives>
      <zips>
        <path refid="compile.path"/>
      </zips>
    </archives>
    <manifest>
        <attribute name="Created-By" value="vireton"/>
        <attribute name="Main-Class" value="HelloIvy"/>
    </manifest>
  </jar>

这将直接从您的常春藤缓存中读取 jar,您不需要先将retrieve它们放入本地lib目录。

于 2013-06-02T10:18:35.270 回答
0

将 lib 文件夹的所有 jar 提取到某个临时文件夹(使用 unjar 任务),然后通过将此临时文件夹添加为文件集来创建 engine.jar。

就像你会用手做一样。

于 2013-06-01T17:43:02.607 回答