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

Ant项目示例

程序员文章站 2022-05-02 14:31:36
...
4. ant示例

4.1 新建目录编译文件

<?xml version="1.0" encoding="utf-8"?>
<project name="myAntProject" basedir="." default="package">
    <property name="compile" value="compile"></property>
    <target name="init"></target>
    <target name="preprocess" depends="init">
        <mkdir dir="${compile}"/>
    </target>
    <target name="complie" depends="init,preprocess"></target>
    <target name="package" depends="complie"></target>
    <target name="myCompile" depends="preprocess">
        <javac srcdir="src" destdir="${compile}"></javac>
    </target>
</project>


4.2 打包文件

<?xml version="1.0" encoding="utf-8"?>
<project name="myAntProject" basedir="." default="package">
    <property name="compile" value="compile"></property>
    <property name="dist" value="dist"></property>
    <target name="init"></target>
    <target name="preprocess" depends="init">
        <mkdir dir="${compile}"/>
        <mkdir dir="${dist}"/>
    </target>
    <target name="complie" depends="init,preprocess"></target>
    <target name="package" depends="complie"></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}"></jar>
    </target>
</project>

jar包中会附带生成jar文件的ant的信息META-INF/MANIFEST.MF
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.6
Created-By: 1.8.0_101-b13 (Oracle Corporation)


4.3 生成图形界面的jar文件

<?xml version="1.0" encoding="utf-8"?>
<project name="myAntProject" basedir="." default="package">
    <property name="compile" value="compile"></property>
    <property name="dist" value="dist"></property>
    <target name="init"></target>
    <target name="preprocess" depends="init">
        <mkdir dir="${compile}"/>
        <mkdir dir="${dist}"/>
    </target>
    <target name="complie" depends="init,preprocess"></target>
    <target name="package" depends="complie"></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="Built-by" value="${user.name}"/>
                <attribute name="Main-Class" value="com.test.Test01"/>
            </manifest>
        </jar>
    </target>
</project>

通过mainfest定义构建内容
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.6
Created-By: 1.8.0_101-b13 (Oracle Corporation)
Built-by: chenchen
Main-Class: com.test.Test01


4.4 编译定义时间戳

<target name="dist" depends="myCompile">
    <tstamp></tstamp>
    <jar destfile="${dist}/package-${DSTAMP}.jar" basedir="${compile}">
        <manifest>
            <attribute name="Built-by" value="${user.name}"/>
            <attribute name="Main-Class" value="com.test.Test01"/>
        </manifest>
    </jar>
</target>



4.5 删除文件

<target name="deleteFile">
    <delete file="${dist}/package-20181201.jar"></delete>
</target>



4.6 拷贝文件

<target name="copyFile">
    <copy file="src/com/test/Test01.java" tofile="C:/Users/chenchen/Desktop/Test.java"></copy>
</target>


4.7 移动文件

<target name="moveFile">
    <move file="C:/Users/chenchen/Desktop/Test.java" todir="d:/"></move>
</target>


4.8 压缩文件

<target name="compress" depends="myCompile">
    <zip destfile="${dist}/package.zip" basedir="${compile}"></zip>
</target>



4.9 解压缩文件

<target name="uncompress" depends="compress">
    <unzip dest="${dist}" src="${dist}/package.zip"></unzip>
</target>



4.10 ant集成cvs

<property name="cvsRoot" value=":pserver:username:password@localhost:c:\cvs"></property>
<property name="destLocation" value="C:/Users/chenchen/Desktop/"></property>
<target name="preprocess" depends="init">
    <mkdir dir="${destLocation}"/>
</target>
<target name="cvs" depends="preprocess">
        <cvs cvsroot="${cvsRoot}" package="project01" command="checkout" dest="${destLocation}"></cvs>
</target>


4.11 替换内容

summary="on"属性显示替换信息

<target name="replaceOperation">
<replace file="input.txt" token="how" value="what" summary="on"></replace>
</target>


4.12 拷贝文件

<include name="*.java"/>单层目录文件
<include name="**/*.java"/>所有目录文件

<property name="src" value="src"></property>
<target name="copy2">
    <copy todir="${dist}">
        <fileset dir="${src}">
            <include name="**/*.java"/>
        </fileset>
    </copy>
</target>


5. 自定义ant

1. 导入ant.jar

2. 编写自定义文件,实现Task接口
package com.tool;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
public class FileSorter extends Task {
    private File srcFile;
    private File destFile;
    public File getSrcFile() {
        return srcFile;
    }
    public void setSrcFile(File srcFile) {
        this.srcFile = srcFile;
    }
    public File getDestFile() {
        return destFile;
    }
    public void setDestFile(File destFile) {
        this.destFile = destFile;
    }
    @Override
    public void execute() throws BuildException {
        try {
            BufferedReader fromFile = new BufferedReader(new FileReader(srcFile));
            BufferedWriter toFile = new BufferedWriter(new FileWriter(destFile));
            List<String> list = new ArrayList<>();
            String line = fromFile.readLine();
            while (line != null) {
                list.add(line);
                line = fromFile.readLine();
            }
            Collections.sort(list);
            for (ListIterator<String> li = list.listIterator(); li.hasNext();) {
                String str = li.next();
                toFile.write(str);
                toFile.newLine();
            }
            fromFile.close();
            toFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3. input.txt文件
hello world
what are you
what old are you
what do you do
abc
test

4. build.xml文件
<taskdef name="myFileSorterExample" classname="com.tool.FileSorter" classpath="bin">
</taskdef>
<target name="myFileSorter">
    <myFileSorterExample srcFile="input.txt" destFile="output.txt"></myFileSorterExample>
</target>

5. 运行结果output.txt
abc
hello world
test
what are you
what do you do
what old are you
相关标签: ant