diff --git a/src/main/org/apache/tools/ant/helper/ProjectHelper2.java b/src/main/org/apache/tools/ant/helper/ProjectHelper2.java index 4eb007520..5feb9170c 100644 --- a/src/main/org/apache/tools/ant/helper/ProjectHelper2.java +++ b/src/main/org/apache/tools/ant/helper/ProjectHelper2.java @@ -28,6 +28,7 @@ import org.apache.tools.ant.TargetGroup; import org.apache.tools.ant.Task; import org.apache.tools.ant.UnknownElement; import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.resources.FileProvider; import org.apache.tools.ant.types.resources.URLProvider; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.JAXPUtils; @@ -197,14 +198,27 @@ public class ProjectHelper2 extends ProjectHelper { if (source instanceof File) { buildFile = (File) source; + } else if (source instanceof URL) { + url = (URL) source; + } else if (source instanceof Resource) { + FileProvider fp = + (FileProvider) ((Resource) source).as(FileProvider.class); + if (fp != null) { + buildFile = fp.getFile(); + } else { + URLProvider up = + (URLProvider) ((Resource) source).as(URLProvider.class); + if (up != null) { + url = up.getURL(); + } + } + } + if (buildFile != null) { buildFile = FILE_UTILS.normalize(buildFile.getAbsolutePath()); context.setBuildFile(buildFile); buildFileName = buildFile.toString(); -// } else if (source instanceof InputStream ) { - } else if (source instanceof URL) { - url = (URL) source; + } else if (url != null) { buildFileName = url.toString(); -// } else if (source instanceof InputSource ) { } else { throw new BuildException("Source " + source.getClass().getName() + " not supported by this plugin"); diff --git a/src/main/org/apache/tools/ant/taskdefs/ImportTask.java b/src/main/org/apache/tools/ant/taskdefs/ImportTask.java index 485307e1f..710b024e8 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ImportTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ImportTask.java @@ -22,6 +22,8 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.ProjectHelper; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.resources.FileResource; import org.apache.tools.ant.util.FileUtils; import java.io.File; @@ -57,6 +59,7 @@ public class ImportTask extends Task { private boolean optional; private String targetPrefix; private String prefixSeparator = "."; + private Resource resource = null; private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); /** @@ -99,9 +102,19 @@ public class ImportTask extends Task { prefixSeparator = s; } + /** + * The resource to import. + * + * @since Ant 1.8.0 + */ + public void add(Resource r) { + resource = r; + } + public void execute() { - if (file == null) { - throw new BuildException("import requires file attribute"); + if (file == null && resource == null) { + throw new BuildException("import requires file attribute or" + + " nested resource"); } if (getOwningTarget() == null || !"".equals(getOwningTarget().getName())) { @@ -129,21 +142,27 @@ public class ImportTask extends Task { throw new BuildException("Unable to get location of import task"); } + Resource importedResource = resource; + File importedFile = null; + if (resource == null) { + File buildFile = new File(getLocation().getFileName()).getAbsoluteFile(); // Paths are relative to the build file they're imported from, // *not* the current directory (same as entity includes). File buildFileParent = new File(buildFile.getParent()); - File importedFile = FILE_UTILS.resolveFile(buildFileParent, file); + importedFile = FILE_UTILS.resolveFile(buildFileParent, file); + importedResource = new FileResource(importedFile); + } - getProject().log("Importing file " + importedFile + " from " - + buildFile.getAbsolutePath(), Project.MSG_VERBOSE); + getProject().log("Importing file " + importedResource + " from " + + getLocation().getFileName(), Project.MSG_VERBOSE); - if (!importedFile.exists()) { + if (!importedResource.isExists()) { String message = - "Cannot find " + file + " imported from " - + buildFile.getAbsolutePath(); + "Cannot find " + importedResource + " imported from " + + getLocation().getFileName(); if (optional) { getProject().log(message, Project.MSG_VERBOSE); return; @@ -152,10 +171,14 @@ public class ImportTask extends Task { } } - if (!isInIncludeMode() && importStack.contains(importedFile)) { + if (!isInIncludeMode() && + (importStack.contains(importedResource) + || (importedFile != null && importStack.contains(importedFile)) + ) + ) { getProject().log( "Skipped already imported file:\n " - + importedFile + "\n", Project.MSG_VERBOSE); + + importedResource + "\n", Project.MSG_VERBOSE); return; } @@ -173,7 +196,7 @@ public class ImportTask extends Task { setProjectHelperProps(prefix, prefixSeparator, isInIncludeMode()); - helper.parse(getProject(), importedFile); + helper.parse(getProject(), importedResource); } catch (BuildException ex) { throw ProjectHelper.addLocationToBuildException( ex, getLocation()); diff --git a/src/tests/antunit/taskdefs/import-test.xml b/src/tests/antunit/taskdefs/import-test.xml index 2bca71872..f9e4b055e 100644 --- a/src/tests/antunit/taskdefs/import-test.xml +++ b/src/tests/antunit/taskdefs/import-test.xml @@ -18,7 +18,9 @@ - + + + @@ -37,7 +39,11 @@ - + + + + + diff --git a/src/tests/antunit/taskdefs/include-test.xml b/src/tests/antunit/taskdefs/include-test.xml index b906254c8..74da94e73 100644 --- a/src/tests/antunit/taskdefs/include-test.xml +++ b/src/tests/antunit/taskdefs/include-test.xml @@ -18,7 +18,9 @@ - + + + @@ -39,7 +41,11 @@ - + + + + +