diff options
author | mattinger <[email protected]> | 2006-08-24 05:09:26 +0000 |
---|---|---|
committer | mattinger <[email protected]> | 2006-08-24 05:09:26 +0000 |
commit | fe78b4b2f629e06d852b3558b33b07905c4b86cd (patch) | |
tree | a3074cef1090f93016e8d29924d7b4912d9c8c75 | |
parent | c5c928bd8b28b4b36ba047f7ef58fda06e6e617c (diff) |
Reworked importUrl task to use Ivy to do it's bidding.
git-svn-id: file:///home/sven/projects/JOGL/temp/ant-contrib/svn/ant-contrib-code/trunk/ant-contrib@41 32d7a393-a5a9-423c-abd3-5d954feb1f2f
-rwxr-xr-x | docs/manual/tasks/importurl.html | 99 | ||||
-rwxr-xr-x | src/java/net/sf/antcontrib/net/URLImportTask.java | 168 |
2 files changed, 195 insertions, 72 deletions
diff --git a/docs/manual/tasks/importurl.html b/docs/manual/tasks/importurl.html index 76d40b3..d302df5 100755 --- a/docs/manual/tasks/importurl.html +++ b/docs/manual/tasks/importurl.html @@ -9,23 +9,38 @@ <p>The Importurl task will download a file, and import it's contents into the
current project. The file can be in the form of a standard ant .xml project
- file, or a .jar/.zip file. In the case of a .jar/.zip file, the file is downloaded
- and then decompressed. After decompression, the file 'build.xml' at the root
- level of the jar is imported.</p>
- <p>
- By importing a .jar/.zip file, one can package additional resources along with
+ file, or a .jar/.zip file.
+ </p>
+ <p>In the case of an .xml file, the file is downloaded, and imported as
+ is. In this case, the file itself is the only thing downloaded (ie.
+ no corresponding .properties files, or other build files are downloaded).
+ </p>
+ <p>In the case of a .jar/.zip file, the file is downloaded and then
+ decompressed. After decompression, the file 'build.xml' at the root
+ level of the jar is imported. By importing a .jar/.zip file, one can package additional resources along with
the build.xml file. However, you must be careful how you refer to these resources.
The build.xml file must follow the same rules as any other file imported with the
<import> task, in that references relative to the build.xml file must be
made with the property:
ant.file.<projectname>
where <projectname> is the name of the project being imported, as specified
- in the project tag.
- </p> + in the project tag. Example:
+ </p>
+
+ <pre>
+ <code>
+ <project name="common">
+ <basedir property="ant.dir.common" file="${ant.file.common}" />
+ <property file="${ant.dir.common}/build.properties" />
+ </project>
+ </code>
+ </pre> <p>
This task should be compatible with older versions of ant, but has only been
- tested with Ant 1.6.x
+ tested with Ant 1.6.x. The underlying implementation is done using the
+ <a href="http://jayasoft.org/ivy">Ivy</a> dependency resolver software,
+ and thus, it needs available to the same classloader that loads this task.
</p> <h2>Parameters</h2> <table border="1" cellpadding="2" cellspacing="0"> @@ -35,11 +50,53 @@ <th>Required</th> </tr> <tr> - <td valign="top">url</td> - <td valign="top">The URL of the file to import. This can be any URL which
- is recognized by the <get> task.</td> - <td align="center" valign="top">Yes</td> + <td valign="top">org</td> + <td valign="top">The organization that publishes the script.</td> + <td align="center" valign="top">Yes.</td> </tr> + <tr>
+ <td valign="top">module</td>
+ <td valign="top">The name of the module which is to be fetched.</td>
+ <td align="center" valign="top">Yes.</td>
+ </tr>
+ <tr>
+ <td valign="top">ref</td>
+ <td valign="top">The revision of the module to be fetched.</td>
+ <td align="center" valign="top">No. Defaults to "latest.integration".
+ See the ivy details for more information on the possible wildcarding
+ that can be used for this value.</td>
+ </tr>
+ <tr>
+ <td valign="top">type</td>
+ <td valign="top">The type of file to be downloaded</td>
+ <td align="center" valign="top">No. Defaults to 'jar'. Can be
+ any file extension. However, unless the type is 'xml', the
+ file is assumed to be a compressed file, expandable by ant's
+ <expand> task (which is aliased to unjar, unzip).</td>
+ </tr>
+ <tr>
+ <td valign="top">repositoryUrl</td>
+ <td valign="top">The URL base of the repository to use. This
+ results in using Ivy's URLResolver to resolve the requested
+ resource.</td>
+ <td align="center" valign="top">No. Defaults to the IvyRepResolver.</td>
+ </tr>
+ <tr>
+ <td valign="top">repositoryDir</td>
+ <td valign="top">The file base of the repository to use. This
+ results in using Ivy's FileSystemResolver to resolve the requested
+ resource.</td>
+ <td align="center" valign="top">No. Defaults to the IvyRepResolver.</td>
+ </tr>
+ <tr>
+ <td valign="top">artifactPattern</td>
+ <td valign="top">The pattern used to find artifacts in the repository.</td>
+ <td align="center" valign="top">No. If repositoryUrl or repositoryDir
+ are specified, this defaults to the standard repository pattern:
+ "/[org]/[module]/[ext]s/[module]-[revision].[ext]". Please see the
+ ivy documentation for more information on the contents of this pattern.
+ </td>
+ </tr>
</table> @@ -48,19 +105,28 @@ <pre>
<code>
- <antcontrib:importurl url="file:/path/to/build.xml" /> + <antcontrib:importurl org="antcontrib"
+ module="common"
+ rev="3.2" />
</code>
</pre>
<pre>
<code>
- <antcontrib:importurl url="http://host/path/to/build.xml" />
+ <antcontrib:importurl org="antcontrib"
+ module="common"
+ rev="3.2"
+ type="xml" />
</code>
</pre>
<pre>
<code>
- <antcontrib:importurl url="http://host/path/to/common.jar" />
+ <antcontrib:importurl repositoryUrl="http://www.antcontrib.org/ivyrep"
+ org="antcontrib"
+ module="common"
+ rev="3.2"
+ type="xml" />
</code>
</pre>
@@ -70,7 +136,8 @@ <pre>
<code>
<project name="common">
- <property file="${ant.file.common}/build.properties" />
+ <basedir property="ant.dir.common" file="${ant.file.common}" />
+ <property file="${ant.dir.common}/build.properties" />
</project>
</code>
</pre>
diff --git a/src/java/net/sf/antcontrib/net/URLImportTask.java b/src/java/net/sf/antcontrib/net/URLImportTask.java index 4477820..4d4663e 100755 --- a/src/java/net/sf/antcontrib/net/URLImportTask.java +++ b/src/java/net/sf/antcontrib/net/URLImportTask.java @@ -16,16 +16,29 @@ package net.sf.antcontrib.net;
import java.io.File;
-import java.io.IOException;
-import java.net.URL;
+import java.util.Date;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Expand;
-import org.apache.tools.ant.taskdefs.Get;
import org.apache.tools.ant.taskdefs.ImportTask;
+import fr.jayasoft.ivy.Artifact;
+import fr.jayasoft.ivy.DefaultModuleDescriptor;
+import fr.jayasoft.ivy.DependencyResolver;
+import fr.jayasoft.ivy.Ivy;
+import fr.jayasoft.ivy.MDArtifact;
+import fr.jayasoft.ivy.ModuleDescriptor;
+import fr.jayasoft.ivy.ModuleId;
+import fr.jayasoft.ivy.ModuleRevisionId;
+import fr.jayasoft.ivy.report.ArtifactDownloadReport;
+import fr.jayasoft.ivy.report.DownloadStatus;
+import fr.jayasoft.ivy.repository.Repository;
+import fr.jayasoft.ivy.resolver.FileSystemResolver;
+import fr.jayasoft.ivy.resolver.IvyRepResolver;
+import fr.jayasoft.ivy.resolver.URLResolver;
+
/***
* Task to import a build file from a url. The build file can be a build.xml,
* or a .zip/.jar, in which case we download and extract the entire archive, and
@@ -36,68 +49,111 @@ import org.apache.tools.ant.taskdefs.ImportTask; public class URLImportTask
extends Task {
- private URL url;
-
+ private String org;
+ private String module;
+ private String rev = "latest.integration";
+ private String type = "jar";
+ private String repositoryUrl;
+ private String repositoryDir;
+ private String artifactPattern = "/[org]/[module]/[ext]s/[module]-[revision].[ext]";
+ private String ivyPattern = "/[org]/[module]/ivy-[revision].xml";
+
+ public void setModule(String module) {
+ this.module = module;
+ }
- public void setUrl(URL url) {
- this.url = url;
+ public void setOrg(String org) {
+ this.org = org;
}
+ public void setRev(String rev) {
+ this.rev = rev;
+ }
public void execute()
throws BuildException {
- if (url == null) {
- throw new BuildException("Missing url.");
+ Ivy ivy = new Ivy();
+ DependencyResolver resolver = null;
+ Repository rep = null;
+
+ if (repositoryUrl != null) {
+ resolver = new URLResolver();
+ ((URLResolver)resolver).addArtifactPattern(
+ repositoryUrl + "/" + artifactPattern
+ );
+ ((URLResolver)resolver).addIvyPattern(
+ repositoryUrl + "/" + ivyPattern
+ );
+ resolver.setName("default");
}
-
- try {
- File tempDir = File.createTempFile("urlimport", "");
- log("Creating directory: " + tempDir.getAbsolutePath(), Project.MSG_INFO);
- tempDir.delete();
- tempDir.mkdirs();
-
- log("Downloading file: " + url.toExternalForm(), Project.MSG_INFO);
-
- File destFile = new File(tempDir, new File(url.getPath()).getName());
- Get get = (Get) getProject().createTask("get");
- get.setSrc(url);
- get.setDest(destFile);
- get.perform();
-
- log("File: " + url.toExternalForm() + " downloaded to " + destFile.getAbsolutePath(),
- Project.MSG_INFO);
-
- File extractedBuildFile = null;
- if (destFile.getName().endsWith(".jar") ||
- destFile.getName().endsWith(".zip")) {
- log("Extracting compressed file: " + destFile.getAbsolutePath(), Project.MSG_INFO);
- Expand expand = (Expand) getProject().createTask("unjar");
- expand.setSrc(destFile);
- expand.setDest(tempDir);
- expand.perform();
- log("Compressed file extracted", Project.MSG_INFO);
- extractedBuildFile = new File(tempDir, "build.xml");
- if (! extractedBuildFile.exists()) {
- log("No 'build.xml' exists in the extracted file.", Project.MSG_ERR);
- throw new BuildException("Downloaded file does not contain a 'build.xml' file");
- }
- }
- else {
- extractedBuildFile = destFile;
- }
-
- log("Importing file: " + extractedBuildFile.getAbsolutePath(), Project.MSG_INFO);
- ImportTask importTask = new ImportTask();
- importTask.setProject(getProject());
- importTask.setOwningTarget(getOwningTarget());
- importTask.setLocation(getLocation());
- importTask.setFile(extractedBuildFile.getAbsolutePath());
- importTask.perform();
- log("Import complete.", Project.MSG_INFO);
+ else if (repositoryDir != null) {
+ resolver = new FileSystemResolver();
+ ((FileSystemResolver)resolver).addArtifactPattern(
+ repositoryDir + "/" + artifactPattern
+ );
+ ((FileSystemResolver)resolver).addIvyPattern(
+ repositoryDir + "/" + ivyPattern
+ );
+ }
+ else {
+ resolver = new IvyRepResolver();
}
- catch (IOException e) {
- throw new BuildException(e);
+ resolver.setName("default");
+ ivy.addResolver(resolver);
+ ivy.setDefaultResolver(resolver.getName());
+
+ ModuleId moduleId =
+ new ModuleId(org, module);
+ ModuleRevisionId revId =
+ new ModuleRevisionId(moduleId, rev);
+ ModuleDescriptor md =
+ new DefaultModuleDescriptor(revId, "integration", new Date());
+ Artifact artifact =
+ new MDArtifact(md, module, type, type);
+
+ ArtifactDownloadReport report =
+ ivy.download(artifact, null);
+
+ DownloadStatus status = report.getDownloadStatus();
+ if (status == DownloadStatus.FAILED) {
+ throw new BuildException("Could not resolve resource.");
}
+
+ String path = ivy.getArchivePathInCache(artifact);
+
+ File file = new File(ivy.getDefaultCache(), path);
+
+ File importFile = null;
+
+ if ("xml".equalsIgnoreCase(type)) {
+ importFile = file;
+ }
+ else if ("jar".equalsIgnoreCase(type)) {
+ File dir = new File(file.getParentFile(),
+ file.getName() + ".extracted");
+ dir.mkdir();
+ Expand expand = (Expand)getProject().createTask("unjar");
+ expand.setSrc(file);
+ expand.setDest(dir);
+ expand.perform();
+ importFile = new File(dir, "build.xml");
+ if (! importFile.exists()) {
+ throw new BuildException("Cannot find a 'build.xml' file in " +
+ file.getName());
+ }
+ }
+ else {
+ throw new BuildException("Don't know what to do with type: " + type);
+ }
+
+ File buildFile = null;
+ ImportTask importTask = new ImportTask();
+ importTask.setProject(getProject());
+ importTask.setOwningTarget(getOwningTarget());
+ importTask.setLocation(getLocation());
+ importTask.setFile(buildFile.getAbsolutePath());
+ importTask.perform();
+ log("Import complete.", Project.MSG_INFO);
}
}
|