`
rensanning
  • 浏览: 3513375 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:37464
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:604269
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:677956
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:87221
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:399787
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69055
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:90447
社区版块
存档分类
最新评论

Java构建工具之Ant

    博客分类:
  • Java
阅读更多
Ant是一种基于Java的build工具。理论上来说,它有些类似于(Unix)C中的make ,但没有make的缺陷。

http://ant.apache.org/

版本:apache-ant-1.8.4


1、输出信息
<!-- 输出信息 -->
<echo>ANT_HOME:${ant.home}</echo>
<echo message="ANT_VERSION:${ant.version}"/>


2、时间戳
<!-- 时间戳 -->
<tstamp />
<echo message="${DSTAMP}"/>


3、使用properties文件
<property file="build.properties" />

<!-- 使用build.properties -->
<echo message="${test.key1}"/>


4、定义变量
<!-- property定义 -->
<property name="test.property1" value="propertyA"/>

<echo message="${test.property1}"/>


5、导入其他Build文件
<include file="included.xml"/>

<!-- included.xml -->
<echo message="${included}"/>

......

<import file="imported.xml"/>

<!-- imported.xml -->
<echo message="${imported}"/>


6、自定义Task
package com.rensanning.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class MyTask extends Task { 
	
	private String name; 
	
	private String country;

	@Override  
    public void execute() throws BuildException {  
		System.out.println(name + " from " + country);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}
	
}

<taskdef name="mt" classname="com.rensanning.ant.MyTask" classpath="bin"/>

<!-- 自定义Task -->
<mt name="rensanning" country="china"/>


7、创建文件夹
<!-- 创建文件夹 -->
<mkdir dir="C:\ant\test11\10" />
<mkdir dir="C:\ant\test11\11" />


8、删除文件夹
<!-- 删除文件夹 -->
<delete dir="C:\ant\test11\11" />


9、移动文件夹
<!-- 移动文件夹 -->
<mkdir dir="C:\ant\folder\move" />
<move todir="C:\ant\folder\move">
   <fileset dir="test/folder/move"/>
</move>


10、拷贝文件夹
<!-- 拷贝文件夹 -->
<mkdir dir="C:\ant\folder\copy" />
<copy todir="C:\ant\folder\copy">
	<fileset dir="test/folder/copy">
		<include name="**/*.java" />
		<exclude name="**/*Test.java" />
	</fileset>
</copy>


11、删除文件
<!-- 删除文件 -->
<delete file="test/file/delete/deletefile.txt" />


12、拷贝文件
<!-- 拷贝文件 -->
<mkdir dir="C:\ant\file\copy" />
<copy todir="C:\ant\file\copy">
	<fileset dir="">
		<include name="**/*.java" />
		<exclude name="**/*.class" />
	</fileset>
</copy>


13、文件重命名
<!-- 文件重命名 -->
<move file="test/file/copy/C2.java" tofile="C:\ant\file\copy\RC2.java"/>


14、修改文件中的某个值
<!-- 修改文件中的某个值 -->
<replace file="C:\ant\file\copy\RC2.java" token="args" value="param"/>


15、压缩文件
<!-- 压缩文件 -->
<mkdir dir="C:\ant\file\zip" />
<zip destfile="C:\ant\file\zip\manual.zip"
     basedir="test/folder"
     includes="**/*.java"
     excludes="**/*Test.java"/>


16、解压文件
<!-- 解压文件 -->
<mkdir dir="C:\ant\file\unzip" />
<unzip src="C:\ant\file\zip\manual.zip" dest="C:\ant\file\unzip"/>


17、移动文件
<!-- 移动文件 -->
<mkdir dir="C:\ant\file\move" />
<move file="test/file/move/movefile.txt" todir="C:\ant\file\move"/>


18、设置classpath
<!-- 设置classpath -->
<path id="cp">
    <pathelement path="${java.class.path}"/>
    <fileset dir="./lib">
       <include name="**/*.jar"/>
    </fileset>
</path>


19、编译类
<!-- 编译类javac -->
<javac destdir="./bin" 
	encoding="UTF-8" 
	deprecation="on" 
	debug="off" 
	fork="true" 
	memoryMaximumSize="256m">
    <src path="src"/>
    <src path="src2"/>
	<classpath refid="cp" />
</javac>


20、运行类
<!-- 运行类java -->
<java classname="com.rensanning.ant.T" classpath="bin"/>


21、打包jar
<!-- 打包jar -->
<mkdir dir="C:\ant\jar" />
<jar jarfile="C:\ant\jar\test.jar">
    <fileset dir="./bin">
        <include name="**/*" />
    </fileset>
</jar>


22、打包war
<!-- 打包war -->
<mkdir dir="C:\ant\war" />
<war destfile="C:\ant\war\myapp.war" webxml="test/war/web.xml">
  <fileset dir="test/war/html"/>
  <fileset dir="test/war/jsp"/>
  <lib dir="./lib">
    <exclude name="servlet-api.jar"/>
  </lib>
  <classes dir="./bin"/>
  <zipfileset dir="test/war/images" prefix="images"/>
</war>


23、CVS相关
<!-- CVS相关 -->
<cvspass cvsroot="${cvsroot}" password="${cvs.password}" passfile="${cvs.passfile}"/>
<cvs cvsroot="${cvsroot}" command="checkout" cvsrsh="ssh" package="myproject" dest="${basedir}" passfile="${cvs.passfile}"/>

<cvs cvsRoot=":pserver:${cvs.user}:${cvs.password}@${cvs.server}:${cvs.reppath}" 
	package="${cvs.module}" 
	tag="${tag60}" 
	dest="${checkoutdir}" />


24、执行外部文件
<!-- 执行外部文件 -->
<exec executable="cmd">
    <arg value="/c"/>
    <arg value="ant.bat"/>
    <arg value="-p"/>
</exec>


25、SSH
<!-- SSH -->
<sshexec host="" username="" password="" trust="true" command=""/>


26、SCP
<!-- SCP -->
<scp todir="root:123456@192.168.0.2:/usr/local/tomcat/webapps/" trust="true">
	<fileset dir="dir" id="id">
	    <include name="include"/>
	    <exclude name="exclude"/>
	</fileset>
</scp>


27、文件同步
<!-- 文件同步 -->
<sync todir="site">
  <fileset dir="generated-site"/>
</sync>


28、转换文件格式
<!-- 转换文件格式 -->
<native2ascii encoding="EUCJIS" src="srcdir" dest="srcdir" includes="**/*.eucjis" ext=".java"/>


29、发送邮件
<!-- 发送邮件 -->
<mail mailhost="smtp.myisp.com" mailport="1025" subject="Test build">
	<from address="me@myisp.com"/>
	<to address="all@xyz.com"/>
	<message>The {buildname} nightly build has completed</message>
	<fileset dir="dist">
	<includes name="**/*.zip"/>
	</fileset>
</mail>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics