2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.ivy.ant;
21
import java.util.Iterator;
22
import java.util.LinkedList;
23
import java.util.List;
25
import org.apache.ivy.core.report.ArtifactDownloadReport;
26
import org.apache.tools.ant.BuildException;
27
import org.apache.tools.ant.types.FileSet;
28
import org.apache.tools.ant.types.PatternSet.NameEntry;
31
* Creates an ant fileset consisting in all artifacts found during a resolve. Note that this task is
32
* not compatible with the useOrigin mode.
34
public class IvyCacheFileset extends IvyCacheTask {
37
public String getSetid() {
41
public void setSetid(String id) {
45
public void setUseOrigin(boolean useOrigin) {
47
throw new UnsupportedOperationException(
48
"the cachefileset task does not support the useOrigin mode, since filesets "
49
+ "require to have only one root directory. Please use the the cachepath "
54
public void doExecute() throws BuildException {
57
throw new BuildException("setid is required in ivy cachefileset");
60
FileSet fileset = new FileSet();
61
fileset.setProject(getProject());
62
getProject().addReference(setid, fileset);
64
List paths = getArtifactReports();
66
for (Iterator iter = paths.iterator(); iter.hasNext();) {
67
ArtifactDownloadReport a = (ArtifactDownloadReport) iter.next();
68
if (a.getLocalFile() != null) {
69
base = getBaseDir(base, a.getLocalFile());
73
fileset.setDir(new File("."));
74
NameEntry ne = fileset.createExclude();
78
for (Iterator iter = paths.iterator(); iter.hasNext();) {
79
ArtifactDownloadReport a = (ArtifactDownloadReport) iter.next();
80
if (a.getLocalFile() != null) {
81
NameEntry ne = fileset.createInclude();
82
ne.setName(getPath(base, a.getLocalFile()));
86
} catch (Exception ex) {
87
throw new BuildException("impossible to build ivy cache fileset: " + ex, ex);
92
* Returns the path of the file relative to the given base directory.
94
* @param base the parent directory to which the file must be evaluated.
95
* @param file the file for which the path should be returned
96
* @return the path of the file relative to the given base directory.
98
private String getPath(File base, File file) {
99
String absoluteBasePath = base.getAbsolutePath();
101
int beginIndex = absoluteBasePath.length();
103
// checks if the basePath ends with the file separator (which can for instance
104
// happen if the basePath is the root on unix)
105
if (!absoluteBasePath.endsWith(File.separator)) {
106
beginIndex++; // skip the seperator char as well
109
return file.getAbsolutePath().substring(beginIndex);
113
* Returns the common base directory between a current base directory and a given file.
115
* The returned base directory must be a parent of both the current base and the given file.
119
* the current base directory, may be null.
121
* the file for which the new base directory should be returned.
122
* @return the common base directory between a current base directory and a given file.
124
File getBaseDir(File base, File file) {
126
return file.getParentFile().getAbsoluteFile();
128
Iterator bases = getParents(base).iterator();
129
Iterator fileParents = getParents(file.getAbsoluteFile()).iterator();
131
while (bases.hasNext() && fileParents.hasNext()) {
132
File next = (File) bases.next();
133
if (next.equals(fileParents.next())) {
144
* @return a list of files, starting with the root and ending with the file itself
146
private LinkedList/*<File>*/ getParents(File file) {
147
LinkedList r = new LinkedList();
148
while (file != null) {
150
file = file.getParentFile();