IVY-1631: when the ivy:retrieve Ant task retrieves the jars to a non-empty directory, but the retrieve task itself didn't retrieve any files, the created fileset was not empty but contained the existing files in that directory.

This commit is contained in:
Maarten Coene 2021-11-23 17:26:55 +01:00
parent 8e2bdfb302
commit f760b390e5
5 changed files with 123 additions and 60 deletions

View File

@ -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[])

View File

@ -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<Resource> 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<T> implements Iterator<T> {
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];
}
}
}

View File

@ -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<Resource> 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<T> implements Iterator<T> {
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];
}
}
}

View File

@ -126,16 +126,23 @@ public class IvyRetrieve extends IvyPostResolveTask {
}
if (getSetId() != null) {
FileSet fileset = new FileSet();
FileSet fileset;
Collection<File> 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);

View File

@ -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");