Ant项目示例

阅读更多
4. ant示例

4.1 新建目录编译文件



    
    
    
        
    
    
    
    
        
    



4.2 打包文件



    
    
    
    
        
        
    
    
    
    
        
    
    
        
    


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文件



    
    
    
    
        
        
    
    
    
    
        
    
    
        
            
                
                
            
        
    


通过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 编译定义时间戳


    
    
        
            
            
        
    




4.5 删除文件


    




4.6 拷贝文件


    



4.7 移动文件


    



4.8 压缩文件


    




4.9 解压缩文件


    




4.10 ant集成cvs




    


        



4.11 替换内容

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






4.12 拷贝文件

单层目录文件
所有目录文件



    
        
            
        
    



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 list = new ArrayList<>();
            String line = fromFile.readLine();
            while (line != null) {
                list.add(line);
                line = fromFile.readLine();
            }
            Collections.sort(list);
            for (ListIterator 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文件



    


5. 运行结果output.txt
abc
hello world
test
what are you
what do you do
what old are you

你可能感兴趣的:(ant)