一个常用的ANT打包脚本

suppose folder structure is like:
   MyProject
                 ---src
                 ---lib
                 ---target
                 ---build.xml

And we want to generate the Jar which will contain the MANIFEST.MF having classpath and main class.
Here we will use ant to build the jar. All you need to do is to change the project name in 1st line and Main-Class value in 55th line  for build.xml, then type ant in command line. (You need to download the ant and include installation folder of ant in PATH system environment variable)

build.xml:
<project name="MyJar" default="package" basedir=".">

<!-- set global properties for this build -->
<property name="src" value="src"/>
<property name="lib" value="lib"/>
<property name="target" value="target"/>
<property name="build" value="target/classes"/>    
<property name="dist" value="target/dist"/>
   
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
 
    
<path id="compile.class.path">
    <!--pathelement location="lib"/-->
    <fileset dir="${lib}">
    <include name="**/*.jar"/>
    </fileset>
</path>

<target name="compile" depends="init">
<!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}" >
        <classpath refid="compile.class.path"/>
    </javac>
</target>
    
    
<target name="package" depends="compile">
    <pathconvert  property ="libs.project"  pathsep =" " >  
               <mapper>
                <chainedmapper>
               <!--  remove absolute path  -->
                  <flattenmapper/>
                   <!--  add lib/ prefix  -->
                   <globmapper  from ="*"  to ="${lib}/*"   />
                </chainedmapper >
              </mapper >
              <path >
                <!--  lib/buildhome contains all jar files, in several subdirectories  -->
                <fileset  dir ="${lib}" >
                  <include  name ="**/*.jar"   />
                </fileset >
              </path >
   </pathconvert >
<!-- Create the jar -->

<!-- Put everything in ${build} into the jar file -->
 <jar jarfile="HadoopExec.jar" basedir="${build}">
       <manifest >
           <attribute  name ="Main-Class"  value ="xxx.XxxxYyyy"  />
          <!--  finally, use the magically generated libs path  -->
         <attribute  name ="Class-Path"  value ="${libs.project}"   />
       </manifest>
 </jar>
</target>

<target name="clean">
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>

</project>

 
执行jar:

java -jar MyJar

这样省去了设置classpath和指定main class的麻烦

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值