简体   繁体   English

蚂蚁脚本示例

[英]Ant Script Example

I would like to make a very simple ant script that does 1 thing, which is to bulid a jar file. 我想做一个非常简单的蚂蚁脚本,该脚本可以完成一件事情,那就是构建一个jar文件。 But when I try to use a very simple example, it fails due to dependancies on jars that my source depends on. 但是,当我尝试使用一个非常简单的示例时,由于对我的来源所依赖的jar的依赖性,它失败了。 So, How you you specify jars that there are jars that need to be in the class path when building an Ant target. 因此,在构建Ant目标时,如何指定jar使其需要在类路径中。

<project name="project" default="default">
<property name="src.dir"     value="src"/>
<property name="build.dir"   value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir"     value="${build.dir}/jar"/>
<property name="lib.dir"     value="//tomcat/common/lib"/>
<description> description </description>

<!-- ================================= 
      target: default              
     ================================= -->
<target name="default" depends="compile" description="description">
    <jar destfile="/path/to/dir/Library.jar">

    </jar>
</target>
  <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

</project>

Your question isn't entirely clear - I suspect you mean you want to compile your source (with the javac task) and then build a jar file from the results. 您的问题尚不完全清楚-我怀疑您的意思是您想编译源代码(使用javac任务), 然后根据结果​​构建一个jar文件。 If that's not the case, I don't see where your source dependencies come into it. 如果不是这种情况,那么我看不到您的源依赖性在哪里出现。 If that is the case, then the jar task is irrelevant. 如果是这样的话,那么jar的任务是无关紧要的。

In the javac task, use the classpath attribute to specify other jar dependencies. javac任务中,使用classpath属性指定其他jar依赖项。

Here's an ANT script generated by using the Eclipse Runnable JAR Export Wizard. 这是使用Eclipse Runnable JAR导出向导生成的ANT脚本。 This is a project that updates stats on a Google Spreadsheet for a small fantasy baseball league with some friends. 这是一个与一些朋友一起为小型梦幻棒球联赛更新Google Spreadsheet上的统计信息的项目。 It gets the stats by scraping ESPN.com player pages. 它通过抓取ESPN.com播放器页面来获取统计信息。

Class-Path attribute inside the manifest element is used to set the classpath used by the jar. manifest元素内的Class-Path属性用于设置jar使用的类路径。 This defaulted "." 默认为“。” but I had to add my src path explicitly so that log4j would pick up log4j.properties. 但是我必须显式添加src路径,以便log4j可以选择log4j.properties。

zipfileset elements are external jars used by my source that I wanted to be included with my jar. zipfileset元素是我的源中使用的外部jar,我想将其包含在jar中。 I suspect this might be what you're looking for. 我怀疑这可能是您要寻找的。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project cob_fantasy_baseball">
    <!--this file was created by Eclipse Runnable JAR Export Wizard-->
    <!--ANT 1.7 is required                                        -->
    <target name="create_run_jar">
        <jar destfile="C:/workspace/cob_fantasy_baseball/cob_fantasy_baseball.jar" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Built-By" value="${user.name}"/>
                <attribute name="Main-Class" value="com.me.cob_fantasy_baseball.UpdateCobStats"/>
                <attribute name="Class-Path" value=".;src/com/me/cob_fantasy_baseball"/>
            </manifest>
            <fileset dir="C:/workspace/cob_fantasy_baseball/classes"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/gdata/java/lib/gdata-core-1.0.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/gdata/java/lib/gdata-spreadsheet-2.0.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/jericho-html-2.6/lib/jericho-html-2.6.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/apache-log4j-1.2.15/log4j-1.2.15.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/jaf-1.1.1/activation.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/javamail-1.4.2/mail.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="C:/workspace/javamail-1.4.2/lib/smtp.jar"/>
            <fileset dir="C:/workspace/cob_fantasy_baseball/src/com/me/cob_fantasy_baseball"/>
        </jar>
    </target>
</project>

Also, here's a link to the Ant documentation for the jar task: http://ant.apache.org/manual/Tasks/jar.html 另外,这是jar任务的Ant文档链接: http : //ant.apache.org/manual/Tasks/jar.html

Based on your example you can just put libs inside javac: 根据您的示例,您可以将库放入javac中:

<javac srcdir="${src.dir}" destdir="${classes.dir}">
    <classpath>
        <pathelement location="${lib.dir}/lib1.jar"/>
        <pathelement location="${lib.dir}/lib2.jar"/>
    </classpath>
</javac>

Here is the ant file we use to build the Timeline opensource project. 这是我们用来构建Timeline开源项目的ant文件。 It is pretty straight forward. 这很简单。 It doesn't build a jar, but it does use libraries to minimize JS files. 它不会构建jar,但是会使用库来最小化JS文件。

http://simile-widgets.googlecode.com/svn/timeline/trunk/build.xml http://simile-widgets.googlecode.com/svn/timeline/trunk/build.xml

Larry 拉里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM