git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@880590 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -150,9 +150,9 @@ project).</p> | |||||
| To create a relative resource you'd use something like:</p> | To create a relative resource you'd use something like:</p> | ||||
| <pre> | <pre> | ||||
| <url id="imported.basedir" url="${ant.file.imported}/."/> | |||||
| <loadproperties> | <loadproperties> | ||||
| <url url="${toString:imported.basedir}/imported.properties"/> | |||||
| <url baseUrl="${ant.file.imported}" | |||||
| relativePath="imported.properties"/> | |||||
| </loadproperties> | </loadproperties> | ||||
| </pre> | </pre> | ||||
| @@ -153,9 +153,9 @@ project).</p> | |||||
| To create a relative resource you'd use something like:</p> | To create a relative resource you'd use something like:</p> | ||||
| <pre> | <pre> | ||||
| <url id="included.basedir" url="${ant.file.included}/."/> | |||||
| <loadproperties> | <loadproperties> | ||||
| <url url="${toString:included.basedir}/included.properties"/> | |||||
| <url baseUrl="${ant.file.included}" | |||||
| relativePath="included.properties"/> | |||||
| </loadproperties> | </loadproperties> | ||||
| </pre> | </pre> | ||||
| @@ -254,6 +254,16 @@ element.</p> | |||||
| <td valign="top">file</td> | <td valign="top">file</td> | ||||
| <td valign="top">The file to expose as a file: url</td> | <td valign="top">The file to expose as a file: url</td> | ||||
| </tr> | </tr> | ||||
| <tr> | |||||
| <td valign="top">baseUrl</td> | |||||
| <td valign="top">The base URL which must be combined with relativePath</td> | |||||
| </tr> | |||||
| <tr> | |||||
| <td valign="top">relativePath</td> | |||||
| <td valign="top">Relative path that defines the url combined with | |||||
| baseUrl</td> | |||||
| <td align="center" valign="top">If using baseUrl</td> | |||||
| </tr> | |||||
| </table> | </table> | ||||
| <h4><a name="string">string</a></h4> | <h4><a name="string">string</a></h4> | ||||
| @@ -124,12 +124,16 @@ public class AntXMLContext { | |||||
| */ | */ | ||||
| public void setBuildFile(File buildFile) { | public void setBuildFile(File buildFile) { | ||||
| this.buildFile = buildFile; | this.buildFile = buildFile; | ||||
| this.buildFileParent = new File(buildFile.getParent()); | |||||
| implicitTarget.setLocation(new Location(buildFile.getAbsolutePath())); | |||||
| try { | |||||
| setBuildFile(FileUtils.getFileUtils().getFileURL(buildFile)); | |||||
| } catch (MalformedURLException ex) { | |||||
| throw new BuildException(ex); | |||||
| if (buildFile != null) { | |||||
| this.buildFileParent = new File(buildFile.getParent()); | |||||
| implicitTarget.setLocation(new Location(buildFile.getAbsolutePath())); | |||||
| try { | |||||
| setBuildFile(FileUtils.getFileUtils().getFileURL(buildFile)); | |||||
| } catch (MalformedURLException ex) { | |||||
| throw new BuildException(ex); | |||||
| } | |||||
| } else { | |||||
| this.buildFileParent = null; | |||||
| } | } | ||||
| } | } | ||||
| @@ -219,6 +219,7 @@ public class ProjectHelper2 extends ProjectHelper { | |||||
| buildFileName = buildFile.toString(); | buildFileName = buildFile.toString(); | ||||
| } else if (url != null) { | } else if (url != null) { | ||||
| try { | try { | ||||
| context.setBuildFile((File) null); | |||||
| context.setBuildFile(url); | context.setBuildFile(url); | ||||
| } catch (java.net.MalformedURLException ex) { | } catch (java.net.MalformedURLException ex) { | ||||
| throw new BuildException(ex); | throw new BuildException(ex); | ||||
| @@ -46,6 +46,8 @@ public class URLResource extends Resource implements URLProvider { | |||||
| private URL url; | private URL url; | ||||
| private URLConnection conn; | private URLConnection conn; | ||||
| private URL baseURL; | |||||
| private String relPath; | |||||
| /** | /** | ||||
| * Default constructor. | * Default constructor. | ||||
| @@ -107,6 +109,34 @@ public class URLResource extends Resource implements URLProvider { | |||||
| } | } | ||||
| } | } | ||||
| /** | |||||
| * Base URL which combined with the relativePath attribute defines | |||||
| * the URL. | |||||
| * @since Ant 1.8.0 | |||||
| */ | |||||
| public synchronized void setBaseURL(URL base) { | |||||
| checkAttributesAllowed(); | |||||
| if (url != null) { | |||||
| throw new BuildException("can't define URL and baseURL attribute"); | |||||
| } | |||||
| baseURL = base; | |||||
| } | |||||
| /** | |||||
| * Relative path which combined with the baseURL attribute defines | |||||
| * the URL. | |||||
| * @since Ant 1.8.0 | |||||
| */ | |||||
| public synchronized void setRelativePath(String r) { | |||||
| checkAttributesAllowed(); | |||||
| if (url != null) { | |||||
| throw new BuildException("can't define URL and relativePath" | |||||
| + " attribute"); | |||||
| } | |||||
| relPath = r; | |||||
| } | |||||
| /** | /** | ||||
| * Get the URL used by this URLResource. | * Get the URL used by this URLResource. | ||||
| * @return a URL object. | * @return a URL object. | ||||
| @@ -115,6 +145,19 @@ public class URLResource extends Resource implements URLProvider { | |||||
| if (isReference()) { | if (isReference()) { | ||||
| return ((URLResource) getCheckedRef()).getURL(); | return ((URLResource) getCheckedRef()).getURL(); | ||||
| } | } | ||||
| if (url == null) { | |||||
| if (baseURL != null) { | |||||
| if (relPath == null) { | |||||
| throw new BuildException("must provide relativePath" | |||||
| + " attribute when using baseURL."); | |||||
| } | |||||
| try { | |||||
| url = new URL(baseURL, relPath); | |||||
| } catch (MalformedURLException e) { | |||||
| throw new BuildException(e); | |||||
| } | |||||
| } | |||||
| } | |||||
| return url; | return url; | ||||
| } | } | ||||
| @@ -124,7 +167,7 @@ public class URLResource extends Resource implements URLProvider { | |||||
| */ | */ | ||||
| public synchronized void setRefid(Reference r) { | public synchronized void setRefid(Reference r) { | ||||
| //not using the accessor in this case to avoid side effects | //not using the accessor in this case to avoid side effects | ||||
| if (url != null) { | |||||
| if (url != null || baseURL != null || relPath != null) { | |||||
| throw tooManyAttributes(); | throw tooManyAttributes(); | ||||
| } | } | ||||
| super.setRefid(r); | super.setRefid(r); | ||||
| @@ -21,16 +21,25 @@ | |||||
| <mkdir dir="${input}/a/b"/> | <mkdir dir="${input}/a/b"/> | ||||
| <mkdir dir="${input}/a/c"/> | <mkdir dir="${input}/a/c"/> | ||||
| <echo file="${input}/a/b/outer.xml"><![CDATA[ | <echo file="${input}/a/b/outer.xml"><![CDATA[ | ||||
| <project> | |||||
| <project name="outer"> | |||||
| <import file="../c/inner.xml"/> | <import file="../c/inner.xml"/> | ||||
| </project> | </project> | ||||
| ]]></echo> | ]]></echo> | ||||
| <echo file="${input}/a/c/inner.xml"><![CDATA[ | <echo file="${input}/a/c/inner.xml"><![CDATA[ | ||||
| <project> | |||||
| <project name="inner"> | |||||
| <target name="foo"> | <target name="foo"> | ||||
| <echo>In inner</echo> | <echo>In inner</echo> | ||||
| <echo>ant.file.inner is ${ant.file.inner}</echo> | |||||
| <echo>type is ${ant.file.type.inner}</echo> | |||||
| <loadproperties> | |||||
| <url baseUrl="${ant.file.inner}" relativePath="test.properties"/> | |||||
| </loadproperties> | |||||
| <echo>foo is ${foo}</echo> | |||||
| </target> | </target> | ||||
| </project>]]></echo> | </project>]]></echo> | ||||
| <echo file="${input}/a/c/test.properties"><![CDATA[ | |||||
| foo=bar | |||||
| ]]></echo> | |||||
| <mkdir dir="${output}"/> | <mkdir dir="${output}"/> | ||||
| <jar destfile="${output}/test.jar"> | <jar destfile="${output}/test.jar"> | ||||
| <fileset dir="${input}"/> | <fileset dir="${input}"/> | ||||
| @@ -45,5 +54,7 @@ | |||||
| <target name="testImportOfNestedFile" depends="foo"> | <target name="testImportOfNestedFile" depends="foo"> | ||||
| <au:assertLogContains text="In inner"/> | <au:assertLogContains text="In inner"/> | ||||
| <au:assertLogContains text="type is url"/> | |||||
| <au:assertLogContains text="foo is bar"/> | |||||
| </target> | </target> | ||||
| </project> | </project> | ||||