diff --git a/asciidoc/release-notes.adoc b/asciidoc/release-notes.adoc index b7cbd829..ff434e86 100644 --- a/asciidoc/release-notes.adoc +++ b/asciidoc/release-notes.adoc @@ -49,7 +49,7 @@ For details about the following changes, check our JIRA install at link:https:// - FIX: ResolveEngine resets dictator resolver to null in the global configuration (jira:IVY-1618[]) - FIX: ConcurrentModificationException in MessageLoggerHelper.sumupProblems (jira:IVY-1628[]) - FIX: useOrigin="true" fails with file-based ibiblio (jira:IVY-1616[]) - +- FIX: ivy:retrieve Ant task didn't create an empty fileset when no files were retrieved to a non-empty directory (jira:IVY-1631[]) - IMPROVEMENT: Ivy command now accepts a URL for the -settings option (jira:IVY-1615[]) diff --git a/src/java/org/apache/ivy/ant/EmptyFileSet.java b/src/java/org/apache/ivy/ant/EmptyFileSet.java new file mode 100644 index 00000000..0d72d5a1 --- /dev/null +++ b/src/java/org/apache/ivy/ant/EmptyFileSet.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ivy.ant; + +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.types.Resource; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +class EmptyFileSet extends FileSet { + + private DirectoryScanner ds = new EmptyDirectoryScanner(); + + public Iterator iterator() { + return new EmptyIterator<>(); + } + + public Object clone() { + return new EmptyFileSet(); + } + + public int size() { + return 0; + } + + public DirectoryScanner getDirectoryScanner(Project project) { + return ds; + } + + private static class EmptyIterator implements Iterator { + + public boolean hasNext() { + return false; + } + + public T next() { + throw new NoSuchElementException("EmptyFileSet Iterator"); + } + + public void remove() { + throw new IllegalStateException("EmptyFileSet Iterator"); + } + + } + + private static class EmptyDirectoryScanner extends DirectoryScanner { + + public String[] getIncludedFiles() { + return new String[0]; + } + + } +} diff --git a/src/java/org/apache/ivy/ant/IvyCacheFileset.java b/src/java/org/apache/ivy/ant/IvyCacheFileset.java index 39d35ccf..a5bd5fa2 100644 --- a/src/java/org/apache/ivy/ant/IvyCacheFileset.java +++ b/src/java/org/apache/ivy/ant/IvyCacheFileset.java @@ -21,15 +21,11 @@ import java.io.File; import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.NoSuchElementException; import org.apache.ivy.core.report.ArtifactDownloadReport; import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.Project; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.PatternSet.NameEntry; -import org.apache.tools.ant.types.Resource; /** * Creates an ant fileset consisting in all artifacts found during a resolve. Note that this task @@ -194,49 +190,4 @@ public class IvyCacheFileset extends IvyCacheTask { } return r; } - - private static class EmptyFileSet extends FileSet { - - private DirectoryScanner ds = new EmptyDirectoryScanner(); - - public Iterator iterator() { - return new EmptyIterator<>(); - } - - public Object clone() { - return new EmptyFileSet(); - } - - public int size() { - return 0; - } - - public DirectoryScanner getDirectoryScanner(Project project) { - return ds; - } - } - - private static class EmptyIterator implements Iterator { - - public boolean hasNext() { - return false; - } - - public T next() { - throw new NoSuchElementException("EmptyFileSet Iterator"); - } - - public void remove() { - throw new IllegalStateException("EmptyFileSet Iterator"); - } - - } - - private static class EmptyDirectoryScanner extends DirectoryScanner { - - public String[] getIncludedFiles() { - return new String[0]; - } - - } } diff --git a/src/java/org/apache/ivy/ant/IvyRetrieve.java b/src/java/org/apache/ivy/ant/IvyRetrieve.java index 4da3e8e3..2e0d6c9c 100644 --- a/src/java/org/apache/ivy/ant/IvyRetrieve.java +++ b/src/java/org/apache/ivy/ant/IvyRetrieve.java @@ -126,16 +126,23 @@ public class IvyRetrieve extends IvyPostResolveTask { } if (getSetId() != null) { - FileSet fileset = new FileSet(); + FileSet fileset; + + Collection retrievedFiles = report.getRetrievedFiles(); + if (retrievedFiles.isEmpty()) { + fileset = new EmptyFileSet(); + } else { + fileset = new FileSet(); + fileset.setDir(report.getRetrieveRoot()); + + for (File file : retrievedFiles) { + PatternSet.NameEntry ne = fileset.createInclude(); + ne.setName(getPath(report.getRetrieveRoot(), file)); + } + } + fileset.setProject(getProject()); getProject().addReference(getSetId(), fileset); - - fileset.setDir(report.getRetrieveRoot()); - - for (File file : report.getRetrievedFiles()) { - PatternSet.NameEntry ne = fileset.createInclude(); - ne.setName(getPath(report.getRetrieveRoot(), file)); - } } } catch (Exception ex) { throw new BuildException("impossible to ivy retrieve: " + ex, ex); diff --git a/test/java/org/apache/ivy/ant/IvyRetrieveTest.java b/test/java/org/apache/ivy/ant/IvyRetrieveTest.java index 0d0ea82d..fca4f78f 100644 --- a/test/java/org/apache/ivy/ant/IvyRetrieveTest.java +++ b/test/java/org/apache/ivy/ant/IvyRetrieveTest.java @@ -22,17 +22,20 @@ import java.io.IOException; import org.apache.ivy.TestHelper; import org.apache.ivy.core.IvyPatternHelper; +import org.apache.ivy.plugins.matcher.GlobPatternMatcher; import org.apache.ivy.util.CacheCleaner; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; +import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.types.Mapper; +import org.apache.tools.ant.types.Resource; import org.junit.After; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; public class IvyRetrieveTest { private static final String IVY_RETRIEVE_PATTERN = "build/test/lib/[organisation]/[module]/ivy-[revision].xml"; @@ -77,6 +80,37 @@ public class IvyRetrieveTest { "mod1.2", "jar", "jar")).exists()); } + @Test + public void testRetrieveFileSetToNonEmptyDirectory() throws IOException { + new File("build/test/lib").mkdirs(); + new File("build/test/lib/foo.txt").createNewFile(); // make sure the retrieve dir is not empty + + project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-simple.xml"); + + retrieve.setSetId("testId"); + retrieve.execute(); + + FileSet fileSet = project.getReference("testId"); + assertNotNull(fileSet); + assertEquals(1, fileSet.size()); + } + + @Test + public void testRetrieveEmptyFileSetToNonEmptyDirectory() throws IOException { + new File("build/test/lib").mkdirs(); + new File("build/test/lib/foo.txt").createNewFile(); // make sure the retrieve dir is not empty + + project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-simple.xml"); + + retrieve.setType("foo"); // make sure we don't retrieve anything + retrieve.setSetId("testId"); + retrieve.execute(); + + FileSet fileSet = project.getReference("testId"); + assertNotNull(fileSet); + assertEquals(0, fileSet.size()); + } + @Test public void testRetrievePrivateWithWildcard() { project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-381.xml");