IMPROVEMENT: ivy:retrieve now accepts a nested Ant <mapper> type.

git-svn-id: https://svn.apache.org/repos/asf/ant/ivy/core/trunk@1075566 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Maarten Coene 2011-02-28 22:27:20 +00:00
parent f46912fffe
commit 9e7743b655
7 changed files with 146 additions and 21 deletions

View File

@ -122,6 +122,7 @@ for detailed view of each issue, please consult http://issues.apache.org/jira/br
- IMPROVEMENT: ivy:retrieve can now create a path or fileset containing the retrieved artifacts (IVY-1235)
- IMPROVEMENT: Improve diagnostics in ssh resolver (IVY-1267) (thanks to Stepan Koltsov)
- IMPROVEMENT: ivy:retrieve can now convert 'dotted'-organisation names into a directory tree.
- IMPROVEMENT: ivy:retrieve now accepts a nested mapper type.
- FIX: quiet="true" does not surpress download 'dots' on packager resolver (IVY-1269)
- FIX: Dynamic version resolution result can be incorrect when ivy metadata contains extra attributes (IVY-1236)

View File

@ -38,6 +38,7 @@ Example:
pattern: lib/[conf]/[artifact].[ext]
root: lib
<span class="since">since 2.3</span> A nested <a href="http://ant.apache.org/manual/Types/mapper.html">mapper</a> element can be used to specify more complex filename transformations of the retrieved files. See the examples below.
<table class="ant">
<thead>
@ -172,6 +173,16 @@ lib
<ivy:retrieve organisation="foo" module="bar" inline="true" pattern="${my.install.dir}/[artifact].[ext]"/>
</code>
Resolves and retrieve the latest version of the module bar and its dependencies in the directory pointed by ${my.install.dir}.
<hr/>
<code type="xml">
<ivy:retrieve pattern="lib/[artifact]-[revision].[ext]">
<firstmatchmapper>
<globmapper from="lib/*-SNAPSHOT.jar" to="lib/snapshots/*-SNAPSHOT.jar" />
<globmapper from="lib/*" to="lib/releases/*"/>
</firstmatchmapper>
</ivy:retrieve>
</code>
Retrieves all dependencies of the last resolve call to a lib directory. The jar files with a version equal to 'SNAPSHOT' are retrieved in a 'snapshots' directory. The other ones are retrieved in a 'releases' directory.
</textarea>
<script type="text/javascript">xooki.postProcess();</script>
</body>

View File

@ -28,8 +28,10 @@ import org.apache.ivy.core.retrieve.RetrieveReport;
import org.apache.ivy.util.filter.Filter;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Mapper;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.PatternSet;
import org.apache.tools.ant.util.FileNameMapper;
/**
* This task allow to retrieve dependencies from the cache to a local directory like a lib dir.
@ -60,6 +62,8 @@ public class IvyRetrieve extends IvyPostResolveTask {
private String pathId = null;
private String setId = null;
private Mapper mapper = null;
public String getPattern() {
return pattern;
@ -109,7 +113,8 @@ public class IvyRetrieve extends IvyPostResolveTask {
.setDirMode(getDirMode())
.setUseOrigin(isUseOrigin())
.setMakeSymlinks(symlink)
.setResolveId(getResolveId()));
.setResolveId(getResolveId())
.setMapper(mapper == null ? null : new MapperAdapter(mapper)));
int targetsCopied = report.getNbrArtifactsCopied();
boolean haveTargetsBeenCopied = targetsCopied > 0;
@ -193,6 +198,28 @@ public class IvyRetrieve extends IvyPostResolveTask {
public String getDirMode() {
return dirMode;
}
/**
* Add a mapper to convert the file names.
*
* @param mapper a <code>Mapper</code> value.
*/
public void addMapper(Mapper mapper) {
if (this.mapper != null) {
throw new IllegalArgumentException("Cannot define more than one mapper");
}
this.mapper = mapper;
}
/**
* Add a nested filenamemapper.
* @param fileNameMapper the mapper to add.
*/
public void add(FileNameMapper fileNameMapper) {
Mapper m = new Mapper(getProject());
m.add(fileNameMapper);
addMapper(m);
}
/**
* Returns the path of the file relative to the given base directory.

View File

@ -0,0 +1,42 @@
/*
* 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
*
* http://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.ivy.core.retrieve.FileNameMapper;
import org.apache.tools.ant.types.Mapper;
class MapperAdapter implements FileNameMapper {
private static final String[] EMPTY = new String[0];
private Mapper mapper;
MapperAdapter(Mapper mapper) {
this.mapper = mapper;
}
public String[] mapFileName(String fileName) {
String[] result = mapper.getImplementation().mapFileName(fileName);
if (result == null) {
result = EMPTY;
}
return result;
}
}

View File

@ -0,0 +1,24 @@
/*
* 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
*
* http://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.core.retrieve;
public interface FileNameMapper {
String[] mapFileName(String fileName);
}

View File

@ -307,7 +307,7 @@ public class RetrieveEngine {
ArtifactDownloadReport artifact = (ArtifactDownloadReport) iter.next();
String destPattern = "ivy".equals(artifact.getType()) ? destIvyPattern
: destFilePattern;
if (!"ivy".equals(artifact.getType())
&& !options.getArtifactFilter().accept(artifact.getArtifact())) {
continue; // skip this artifact, the filter didn't accept it!
@ -327,33 +327,41 @@ public class RetrieveEngine {
} else {
throw new IllegalArgumentException("Unsupported dirMode: " + options.getDirMode());
}
Set dest = (Set) artifactsToCopy.get(artifact);
if (dest == null) {
dest = new HashSet();
artifactsToCopy.put(artifact, dest);
}
String copyDest = settings.resolveFile(destFileName).getAbsolutePath();
dest.add(copyDest);
Set conflicts = (Set) conflictsMap.get(copyDest);
Set conflictsReports = (Set) conflictsReportsMap.get(copyDest);
Set conflictsConf = (Set) conflictsConfMap.get(copyDest);
if (conflicts == null) {
conflicts = new HashSet();
conflictsMap.put(copyDest, conflicts);
String[] destinations = new String[] {copyDest};
if (options.getMapper() != null) {
destinations = options.getMapper().mapFileName(copyDest);
}
if (conflictsReports == null) {
conflictsReports = new HashSet();
conflictsReportsMap.put(copyDest, conflictsReports);
}
if (conflictsConf == null) {
conflictsConf = new HashSet();
conflictsConfMap.put(copyDest, conflictsConf);
}
if (conflicts.add(artifact.getArtifact().getId())) {
conflictsReports.add(artifact);
conflictsConf.add(conf);
for (int j = 0; j < destinations.length; j++) {
dest.add(destinations[j]);
Set conflicts = (Set) conflictsMap.get(destinations[j]);
Set conflictsReports = (Set) conflictsReportsMap.get(destinations[j]);
Set conflictsConf = (Set) conflictsConfMap.get(destinations[j]);
if (conflicts == null) {
conflicts = new HashSet();
conflictsMap.put(destinations[j], conflicts);
}
if (conflictsReports == null) {
conflictsReports = new HashSet();
conflictsReportsMap.put(destinations[j], conflictsReports);
}
if (conflictsConf == null) {
conflictsConf = new HashSet();
conflictsConfMap.put(destinations[j], conflictsConf);
}
if (conflicts.add(artifact.getArtifact().getId())) {
conflictsReports.add(artifact);
conflictsConf.add(conf);
}
}
}
}

View File

@ -83,6 +83,8 @@ public class RetrieveOptions extends LogOptions {
* The id used to store the resolve information.
*/
private String resolveId;
private FileNameMapper mapper;
public RetrieveOptions() {
}
@ -99,6 +101,7 @@ public class RetrieveOptions extends LogOptions {
this.makeSymlinks = options.makeSymlinks;
this.dirMode = options.dirMode;
this.resolveId = options.resolveId;
this.mapper = options.mapper;
}
public String getDestArtifactPattern() {
@ -191,4 +194,13 @@ public class RetrieveOptions extends LogOptions {
return this;
}
public FileNameMapper getMapper() {
return mapper;
}
public RetrieveOptions setMapper(FileNameMapper mapper) {
this.mapper = mapper;
return this;
}
}