使用ant编译并生成jar包

使用ant生成可执行jar文件,双击可以打开窗口。

1.写一个创建窗口的类

package com.anllin.ant.test;

 

import java.awt.Color;

import java.awt.Dimension;

 

import javax.swing.JButton;

import javax.swing.JFrame;

 

public class Window

{

    public static void main(String[] args)

    {

       JFrame frame = new JFrame();

      

       frame.setSize(new Dimension(200,300));

       frame.setBackground(new Color(200,200,200));

       frame.setAlwaysOnTop(true);

       frame.getContentPane().add(new JButton("Ant"));

       frame.setVisible(true);

      

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

 

 

2.使用ant编译并生成jar包

<?xml version="1.0" encoding="UTF-8"?>

<project name="myAntProject" basedir="." default="package">

    <property name="compile" value="compile"/>

    <property name="dist" value="dist"></property>

    <target name="init"></target>

    <target name="preprocess" depends="init">

       <mkdir dir="${compile}"/>

       <mkdir dir="${dist}"/>

    </target>

    <target name="compile" depends="init,preprocess"></target>

    <target name="package" depends="compile"></target>

    <target name="mycompile" depends="preprocess">

       <javac srcdir="src" destdir="${compile}"></javac>

    </target>

    <target name="dist" depends="mycompile">

       <jar destfile="${dist}/package.jar" basedir="${compile}">

           <manifest>

              <attribute name="Build-By" value="${user.name}"/>

              <attribute name="Main-Class" value="com.anllin.ant.test.Window"/>

           </manifest>

       </jar>

    </target>

</project>

posted @ 2012-02-19 10:33  张良  阅读(539)  评论(0编辑  收藏  举报