欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ant 打包java应用

程序员文章站 2022-05-23 09:06:23
...

最近在维护一个以前的项目,发现以前发布的时候居然都是手动发布,项目中用到第三方的Jar包,每次要解压、复制,麻烦而且容易出错。于是想起用Ant发布。上网查下资料,简单写了个Demo,里面只用到了Log4j.jar。

 

工程目录如图,其中dist和build目录是Ant构建过程中生成的:

Ant 打包java应用
            
    
    博客分类: 日积月累 AntjavaManifest 

 

下面是MainClass的代码:

/**
*@author DaiYanming
*@create date 2011-12-9
*/

package dc;

import org.apache.log4j.Logger;

public class MainClass {

	public static void main(String[] args) {
		Logger logger=Logger.getLogger(MainClass.class);
		logger.info("信息");
		logger.debug("调试");
		logger.error("出错啦");
	}

}

 

 Ant构建文件:

<?xml version="1.0" ?>
<project name="testPackage" default="jar">

	<property name="jar.name" value="testPackage.jar" />
	<property name="dist.home" value="dist" />
	<property name="build.home" value="build" />
	<property name="lib.home" value="lib" />

	<path id="lib_path">
		<fileset dir="${lib.home}">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<target name="init">
		<mkdir dir="build/classes" />
		<mkdir dir="dist" />
	</target>

	<target name="compile" depends="init">
		<javac srcdir="src" destdir="build/classes" encoding="utf-8">
			<classpath>
				<path refid="lib_path"/>
			</classpath>
		</javac>
	</target>

	<target name="prepare" depends="init">
		<copy todir="${build.home}/classes/lib">
			<fileset dir="${lib.home}">
				<include name="**/*.jar" />
			</fileset>
		</copy>
		<copy todir="${build.home}/classes">
			<fileset dir=".">
				<include name="**/*.properties" />
			</fileset>
		</copy>
	</target>
	
	<target name="jar" depends="compile,prepare" description="Create jar and MANIFEST.MF">
		<!-- create a property containing all .jar files, prefix lib/, and seperated with a space -->
		<pathconvert property="libs.project" pathsep=" ">
			<mapper>
				<chainedmapper>
					<!-- remove absolute path -->
					<flattenmapper />
					<!-- add lib/ prefix -->
					<globmapper from="*" to="../lib/*" />
				</chainedmapper>
			</mapper>

			<path>
				<!-- lib.home contains all jar files, in several subdirectories -->
				<fileset dir="${lib.home}">
					<include name="**/*.jar" />
				</fileset>
			</path>
		</pathconvert>

		<!-- create the jar -->
		<jar destfile="${dist.home}/${jar.name}" basedir="${build.home}/classes">
			<!-- define MANIFEST.MF -->
			<manifest>
				<attribute name="Built-By" value="${user.name}" />
				<attribute name="Main-Class" value="dc.MainClass" />
				<section name="common">
					<attribute name="Specification-Version" value="${component.version}" />
				</section>

				<!-- finally, use the magically generated libs path -->
				<attribute name="Class-Path" value="${libs.project}" />
			</manifest>
		</jar>
	</target>


	<target name="clean" depends="init">
		<delete dir="build" />
		<delete dir="dist" />
	</target>
</project>

 在命令行下切换到工程目录,输入ant jar 提示:

E:\javawork\testPackage>ant
Buildfile: build.xml

init:

compile:

prepare:
     [copy] Copying 1 file to E:\javawork\testPackage\build\classes

jar:
      [jar] Building jar: E:\javawork\testPackage\dist\testPackage.jar

BUILD SUCCESSFUL
Total time: 0 seconds

 

打包完毕。

 

  • Ant 打包java应用
            
    
    博客分类: 日积月累 AntjavaManifest 
  • 大小: 6.7 KB
相关标签: Ant java Manifest