git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@832990 13f79535-47bb-0310-9956-ffa450edef68master
@@ -28,6 +28,7 @@ import org.apache.tools.ant.TargetGroup; | |||||
import org.apache.tools.ant.Task; | import org.apache.tools.ant.Task; | ||||
import org.apache.tools.ant.UnknownElement; | import org.apache.tools.ant.UnknownElement; | ||||
import org.apache.tools.ant.types.Resource; | 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.types.resources.URLProvider; | ||||
import org.apache.tools.ant.util.FileUtils; | import org.apache.tools.ant.util.FileUtils; | ||||
import org.apache.tools.ant.util.JAXPUtils; | import org.apache.tools.ant.util.JAXPUtils; | ||||
@@ -197,14 +198,27 @@ public class ProjectHelper2 extends ProjectHelper { | |||||
if (source instanceof File) { | if (source instanceof File) { | ||||
buildFile = (File) source; | 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()); | buildFile = FILE_UTILS.normalize(buildFile.getAbsolutePath()); | ||||
context.setBuildFile(buildFile); | context.setBuildFile(buildFile); | ||||
buildFileName = buildFile.toString(); | buildFileName = buildFile.toString(); | ||||
// } else if (source instanceof InputStream ) { | |||||
} else if (source instanceof URL) { | |||||
url = (URL) source; | |||||
} else if (url != null) { | |||||
buildFileName = url.toString(); | buildFileName = url.toString(); | ||||
// } else if (source instanceof InputSource ) { | |||||
} else { | } else { | ||||
throw new BuildException("Source " + source.getClass().getName() | throw new BuildException("Source " + source.getClass().getName() | ||||
+ " not supported by this plugin"); | + " not supported by this plugin"); | ||||
@@ -22,6 +22,8 @@ import org.apache.tools.ant.BuildException; | |||||
import org.apache.tools.ant.ProjectHelper; | import org.apache.tools.ant.ProjectHelper; | ||||
import org.apache.tools.ant.Project; | import org.apache.tools.ant.Project; | ||||
import org.apache.tools.ant.Task; | 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 org.apache.tools.ant.util.FileUtils; | ||||
import java.io.File; | import java.io.File; | ||||
@@ -57,6 +59,7 @@ public class ImportTask extends Task { | |||||
private boolean optional; | private boolean optional; | ||||
private String targetPrefix; | private String targetPrefix; | ||||
private String prefixSeparator = "."; | private String prefixSeparator = "."; | ||||
private Resource resource = null; | |||||
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); | private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); | ||||
/** | /** | ||||
@@ -99,9 +102,19 @@ public class ImportTask extends Task { | |||||
prefixSeparator = s; | prefixSeparator = s; | ||||
} | } | ||||
/** | |||||
* The resource to import. | |||||
* | |||||
* @since Ant 1.8.0 | |||||
*/ | |||||
public void add(Resource r) { | |||||
resource = r; | |||||
} | |||||
public void execute() { | 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 | if (getOwningTarget() == null | ||||
|| !"".equals(getOwningTarget().getName())) { | || !"".equals(getOwningTarget().getName())) { | ||||
@@ -129,21 +142,27 @@ public class ImportTask extends Task { | |||||
throw new BuildException("Unable to get location of import 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(); | File buildFile = new File(getLocation().getFileName()).getAbsoluteFile(); | ||||
// Paths are relative to the build file they're imported from, | // Paths are relative to the build file they're imported from, | ||||
// *not* the current directory (same as entity includes). | // *not* the current directory (same as entity includes). | ||||
File buildFileParent = new File(buildFile.getParent()); | 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 = | String message = | ||||
"Cannot find " + file + " imported from " | |||||
+ buildFile.getAbsolutePath(); | |||||
"Cannot find " + importedResource + " imported from " | |||||
+ getLocation().getFileName(); | |||||
if (optional) { | if (optional) { | ||||
getProject().log(message, Project.MSG_VERBOSE); | getProject().log(message, Project.MSG_VERBOSE); | ||||
return; | 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( | getProject().log( | ||||
"Skipped already imported file:\n " | "Skipped already imported file:\n " | ||||
+ importedFile + "\n", Project.MSG_VERBOSE); | |||||
+ importedResource + "\n", Project.MSG_VERBOSE); | |||||
return; | return; | ||||
} | } | ||||
@@ -173,7 +196,7 @@ public class ImportTask extends Task { | |||||
setProjectHelperProps(prefix, prefixSeparator, | setProjectHelperProps(prefix, prefixSeparator, | ||||
isInIncludeMode()); | isInIncludeMode()); | ||||
helper.parse(getProject(), importedFile); | |||||
helper.parse(getProject(), importedResource); | |||||
} catch (BuildException ex) { | } catch (BuildException ex) { | ||||
throw ProjectHelper.addLocationToBuildException( | throw ProjectHelper.addLocationToBuildException( | ||||
ex, getLocation()); | ex, getLocation()); | ||||
@@ -18,7 +18,9 @@ | |||||
<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit"> | <project default="antunit" xmlns:au="antlib:org.apache.ant.antunit"> | ||||
<import file="../antunit-base.xml" /> | <import file="../antunit-base.xml" /> | ||||
<import file="importtests/a.xml"/> | |||||
<import> | |||||
<file file="importtests/a.xml"/> | |||||
</import> | |||||
<import file="importtests/b.xml" as="c"/> | <import file="importtests/b.xml" as="c"/> | ||||
<target name="testNoExplicitPrefix" depends="a.a"> | <target name="testNoExplicitPrefix" depends="a.a"> | ||||
@@ -37,7 +39,11 @@ | |||||
<au:assertEquals expected="baz" actual="${foo}"/> | <au:assertEquals expected="baz" actual="${foo}"/> | ||||
</target> | </target> | ||||
<import file="importtests/override.xml"/> | |||||
<import> | |||||
<javaresource name="override.xml"> | |||||
<classpath location="importtests"/> | |||||
</javaresource> | |||||
</import> | |||||
<target name="setProperty"> | <target name="setProperty"> | ||||
<property name="prop" value="in including/importing"/> | <property name="prop" value="in including/importing"/> | ||||
@@ -18,7 +18,9 @@ | |||||
<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit"> | <project default="antunit" xmlns:au="antlib:org.apache.ant.antunit"> | ||||
<import file="../antunit-base.xml" /> | <import file="../antunit-base.xml" /> | ||||
<include file="importtests/a.xml"/> | |||||
<include> | |||||
<file file="importtests/a.xml"/> | |||||
</include> | |||||
<include file="importtests/b.xml" as="c"/> | <include file="importtests/b.xml" as="c"/> | ||||
<target name="testNoExplicitPrefix" depends="a.a"> | <target name="testNoExplicitPrefix" depends="a.a"> | ||||
@@ -39,7 +41,11 @@ | |||||
<au:assertEquals expected="in included/imported" actual="${prop}"/> | <au:assertEquals expected="in included/imported" actual="${prop}"/> | ||||
</target> | </target> | ||||
<include file="importtests/nested.xml" as="nested"/> | |||||
<include as="nested"> | |||||
<javaresource name="nested.xml"> | |||||
<classpath location="importtests"/> | |||||
</javaresource> | |||||
</include> | |||||
<!-- really only tests that the targets have the expected names by | <!-- really only tests that the targets have the expected names by | ||||
forcing an exception if the dependencies don't exist --> | forcing an exception if the dependencies don't exist --> | ||||