ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Rettig <mike.ret...@gmail.com>
Subject Importing and caching build files from a URL
Date Tue, 29 Nov 2005 01:55:22 GMT
All,

I'd like to be able to import files from URL's. AFAIK, only file imports are
supported. This is limiting when trying to share build files between
multiple projects. I'd like to package build files in jars or distribute
them on a website.

I've brought this discussion up once before for jar imports.

http://marc.theaimsgroup.com/?l=ant-dev&m=111539890504619&w=2

Http Example:

<project>

  <import file="ivy-dependencies.xml"/>
  <urlimport url="http://www.antbuilds.com/1.1/war-build.xml" cache="true"
/>

</project>

This makes distributing and reusing builds quite easy. Apache or whomever
could host standard build files that could be downloaded
and imported directly into ant scripts.  The files could be cached in a
simple repository in the user's home directory (~/.antcache).

I've written some basic code that extends the ImportTask, but probably some
refactoring needs to be done on the ImportTask to make it a bit cleaner.

Thoughts?

package org.apache.tools.ant.taskdefs;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import org.apache.tools.ant.BuildException;

/**
 * @ant.task category="control"
 */
public class UrlImportTask extends ImportTask {

    private String url = null;

    private boolean cache = true;

    public void setUrl(String url) {
        this.url = url;
    }

    public void setCache(boolean shouldCache) {
        this.cache = shouldCache;
    }

    private File getOutputDir() {
        String tempDir = System.getProperty("user.home");
        File f = new File(tempDir, ".antimport");
        if (!f.exists()) {
            f.mkdirs();
        }
        return f;
    }

    public void execute() {
        if (url == null) {
            throw new BuildException("import requires url attribute");
        }

        File output = new File(getOutputDir(), url);
        boolean fileExists = output.exists();
        if (!fileExists) {
            copyFileToCacheDir(output);
        }
        setFile(output.getAbsolutePath());
        try {
            super.execute();
        } finally {
            if (!fileExists && !cache) {
                output.delete();
            }
        }
    }

    private void copyFileToCacheDir(File output) {
        InputStream input = openInputStream();

        OutputStream outputStream = null;
        try {
            outputStream = new BufferedOutputStream(
                    new FileOutputStream(output));
            for (int i = input.read(); i >= 0; i = input.read()) {
                outputStream.write(i);
            }
            outputStream.flush();
        } catch (Exception failed) {
            throw new BuildException("Import Failed:" + failed.getMessage(),
                    failed);
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                log(e.getMessage());
            }
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                log(e.getMessage());
            }
        }
    }

    private InputStream openInputStream() {
        try {
            URL location = new URL(url);
            InputStream input = new BufferedInputStream(location.openStream
());
            return input;
        } catch (Exception failed) {
            throw new BuildException("Import Failed: " + failed.getMessage
(),
                    failed);
        }
    }
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message