NEW: find modules in repository (IVY-275)

git-svn-id: https://svn.apache.org/repos/asf/incubator/ivy/trunk@484465 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Xavier Hanin 2006-08-22 23:56:38 +00:00
parent ccf05d5760
commit be3027f8fe
7 changed files with 428 additions and 4 deletions

View File

@ -19,6 +19,7 @@ Incompatibility with previous versions:
Changes:
--------
* thanks to Jayasoft:
- NEW: find modules in repository (IVY-275)
- NEW: explicitly specify artifact download URL (IVY-271)
- NEW: introduce branch management (IVY-269)
- NEW: resolve dependencies directly without using an ivy file (IVY-268)

View File

@ -962,10 +962,25 @@ public class Ivy implements TransferListener {
}
}
/////////////////////////////////////////////////////////////////////////
public ResolvedModuleRevision findModule(ModuleRevisionId id) {
DependencyResolver r = getResolver(id.getModuleId());
if (r == null) {
throw new IllegalStateException("no resolver found for "+id.getModuleId());
}
DefaultModuleDescriptor md = genCallerMD(id, new String[] {"*"}, false, false);
try {
return r.getDependency(new DefaultDependencyDescriptor(id, true), new ResolveData(this, getDefaultCache(), null, new ConfigurationResolveReport(this, md, "default", null, getDefaultCache()), false));
} catch (ParseException e) {
throw new RuntimeException("problem whle parsing repository module descriptor for "+id+": "+e, e);
}
}
/////////////////////////////////////////////////////////////////////////
// RESOLVE
/////////////////////////////////////////////////////////////////////////
public ResolveReport resolve(File ivySource) throws ParseException, IOException {
return resolve(ivySource.toURL());
}
@ -997,14 +1012,19 @@ public class Ivy implements TransferListener {
* Resolves the module identified by the given mrid with its dependencies if transitive is set to true.
*/
public ResolveReport resolve(ModuleRevisionId mrid, String[] confs, boolean transitive, boolean changing, File cache, Date date, boolean validate, boolean useCacheOnly, Filter artifactFilter) throws ParseException, IOException {
DefaultModuleDescriptor md = DefaultModuleDescriptor.newDefaultInstance(ModuleRevisionId.newInstance(mrid.getOrganisation(), mrid.getName()+"-caller", "working"));
DefaultModuleDescriptor md = genCallerMD(mrid, confs, transitive, changing);
return resolve(md, new String[] {"default"}, cache, date, validate, useCacheOnly, artifactFilter);
}
private DefaultModuleDescriptor genCallerMD(ModuleRevisionId mrid, String[] confs, boolean transitive, boolean changing) {
DefaultModuleDescriptor md = DefaultModuleDescriptor.newDefaultInstance(ModuleRevisionId.newInstance(mrid.getOrganisation(), mrid.getName()+"-caller", "working"));
DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, mrid, false, changing, transitive);
for (int i = 0; i < confs.length; i++) {
dd.addDependencyConfiguration("default", confs[i]);
}
md.addDependency(dd);
return resolve(md, new String[] {"default"}, cache, date, validate, useCacheOnly, artifactFilter);
return md;
}
public ResolveReport resolve(URL ivySource, String revision, String[] confs, File cache, Date date, boolean validate, boolean useCacheOnly, Filter artifactFilter) throws ParseException, IOException {
@ -2378,6 +2398,13 @@ public class Ivy implements TransferListener {
}
/**
* Returns an empty array when no token values are found.
*
* @param token
* @param otherTokenValues
* @return
*/
public String[] listTokenValues(String token, Map otherTokenValues) {
List r = new ArrayList();
for (Iterator iter = _resolversMap.values().iterator(); iter.hasNext();) {
@ -2789,4 +2816,49 @@ public class Ivy implements TransferListener {
return _interrupted;
}
/**
* List module revision ids of the module accessible through the current resolvers
* matching the given mrid criteria according to the given matcher.
*
* @param criteria
* @param matcher
* @return
*/
public ModuleRevisionId[] listModules(ModuleRevisionId criteria, PatternMatcher matcher) {
List ret = new ArrayList();
Matcher orgMatcher = matcher.getMatcher(criteria.getOrganisation());
Matcher modMatcher = matcher.getMatcher(criteria.getName());
Matcher branchMatcher = matcher.getMatcher(criteria.getBranch());
Matcher revMatcher = matcher.getMatcher(criteria.getRevision());
Map tokenValues = new HashMap();
String[] orgs = listTokenValues(IvyPatternHelper.ORGANISATION_KEY, tokenValues);
for (int i = 0; i < orgs.length; i++) {
if (orgMatcher.matches(orgs[i])) {
tokenValues.put(IvyPatternHelper.ORGANISATION_KEY, orgs[i]);
String[] mods = listTokenValues(IvyPatternHelper.MODULE_KEY, tokenValues);
for (int j = 0; j < mods.length; j++) {
if (modMatcher.matches(mods[j])) {
tokenValues.put(IvyPatternHelper.MODULE_KEY, mods[j]);
String[] branches = listTokenValues(IvyPatternHelper.BRANCH_KEY, tokenValues);
if (branches == null || branches.length == 0) {
branches = new String[] {getDefaultBranch(new ModuleId(orgs[i], mods[j]))};
}
for (int k = 0; k < branches.length; k++) {
if (branchMatcher.matches(branches[k])) {
tokenValues.put(IvyPatternHelper.BRANCH_KEY, tokenValues);
String[] revs = listTokenValues(IvyPatternHelper.REVISION_KEY, tokenValues);
for (int l = 0; l < revs.length; l++) {
if (revMatcher.matches(revs[l])) {
ret.add(ModuleRevisionId.newInstance(orgs[i], mods[j], branches[k], revs[l]));
}
}
}
}
}
}
}
}
return (ModuleRevisionId[]) ret.toArray(new ModuleRevisionId[ret.size()]);
}
}

View File

@ -0,0 +1,101 @@
package fr.jayasoft.ivy.ant;
import java.util.Iterator;
import java.util.Map;
import org.apache.tools.ant.BuildException;
import fr.jayasoft.ivy.Ivy;
import fr.jayasoft.ivy.ModuleId;
import fr.jayasoft.ivy.ModuleRevisionId;
import fr.jayasoft.ivy.ResolvedModuleRevision;
/**
* Look for the latest module in the repository matching the given criteria,
* and sets a set of properties according to what was found.
*
* @author Xavier Hanin
*/
public class IvyFindModule extends IvyTask {
private String _organisation;
private String _module;
private String _branch;
private String _revision;
private String _prefix = "ivy.";
protected String getModule() {
return _module;
}
protected void setModule(String module) {
_module = module;
}
protected String getOrganisation() {
return _organisation;
}
protected void setOrganisation(String organisation) {
_organisation = organisation;
}
protected String getRevision() {
return _revision;
}
protected void setRevision(String revision) {
_revision = revision;
}
protected String getBranch() {
return _branch;
}
protected void setBranch(String branch) {
_branch = branch;
}
protected String getPrefix() {
return _prefix;
}
protected void setPrefix(String prefix) {
_prefix = prefix;
}
public void execute() throws BuildException {
if (_organisation == null) {
throw new BuildException("no organisation provided for ivy findmodules");
}
if (_module == null) {
throw new BuildException("no module name provided for ivy findmodules");
}
if (_revision == null) {
throw new BuildException("no revision provided for ivy findmodules");
}
Ivy ivy = getIvyInstance();
if (_branch == null) {
ivy.getDefaultBranch(new ModuleId(_organisation, _module));
}
if (_prefix == null) {
_prefix = "";
} else if (!_prefix.endsWith(".") && _prefix.length() != 0) {
_prefix = _prefix+".";
}
ResolvedModuleRevision rmr = ivy.findModule(ModuleRevisionId.newInstance(_organisation, _module, _branch, _revision));
if (rmr != null) {
Map attributes = rmr.getId().getAttributes();
for (Iterator iter = attributes.keySet().iterator(); iter.hasNext();) {
String token = (String) iter.next();
String value = (String) attributes.get(token);
if (value != null) {
getProject().setProperty(_prefix+token, value);
}
}
}
}
}

View File

@ -0,0 +1,106 @@
package fr.jayasoft.ivy.ant;
import org.apache.tools.ant.BuildException;
import fr.jayasoft.ivy.Ivy;
import fr.jayasoft.ivy.ModuleRevisionId;
import fr.jayasoft.ivy.matcher.PatternMatcher;
import fr.jayasoft.ivy.util.IvyPatternHelper;
/**
* Look for modules in the repository matching the given criteria, and sets a set of properties
* according to what was found.
*
* @author Xavier Hanin
*/
public class IvyListModules extends IvyTask {
private String _organisation;
private String _module;
private String _branch = PatternMatcher.ANY_EXPRESSION;
private String _revision;
private String _matcher = PatternMatcher.EXACT_OR_REGEXP;
private String _property;
private String _value;
protected String getMatcher() {
return _matcher;
}
protected void setMatcher(String matcher) {
_matcher = matcher;
}
protected String getModule() {
return _module;
}
protected void setModule(String module) {
_module = module;
}
protected String getProperty() {
return _property;
}
protected void setProperty(String name) {
_property = name;
}
protected String getOrganisation() {
return _organisation;
}
protected void setOrganisation(String organisation) {
_organisation = organisation;
}
protected String getRevision() {
return _revision;
}
protected void setRevision(String revision) {
_revision = revision;
}
protected String getValue() {
return _value;
}
protected void setValue(String value) {
_value = value;
}
public void execute() throws BuildException {
if (_organisation == null) {
throw new BuildException("no organisation provided for ivy findmodules");
}
if (_module == null) {
throw new BuildException("no module name provided for ivy findmodules");
}
if (_revision == null) {
throw new BuildException("no revision provided for ivy findmodules");
}
if (_property == null) {
throw new BuildException("no property provided for ivy findmodules");
}
if (_value == null) {
throw new BuildException("no value provided for ivy findmodules");
}
Ivy ivy = getIvyInstance();
ModuleRevisionId[] mrids = ivy.listModules(ModuleRevisionId.newInstance(_organisation, _module, _branch, _revision), ivy.getMatcher(_matcher));
for (int i = 0; i < mrids.length; i++) {
String name = IvyPatternHelper.substitute(ivy.substitute(_property), mrids[i]);
String value = IvyPatternHelper.substitute(ivy.substitute(_value), mrids[i]);
getProject().setProperty(name, value);
}
}
protected String getBranch() {
return _branch;
}
protected void setBranch(String branch) {
_branch = branch;
}
}

View File

@ -18,4 +18,6 @@
<taskdef name="artifactreport" classname="fr.jayasoft.ivy.ant.IvyArtifactReport"/>
<taskdef name="info" classname="fr.jayasoft.ivy.ant.IvyInfo"/>
<taskdef name="addpath" classname="fr.jayasoft.ivy.ant.AddPathTask"/>
<taskdef name="listmodules" classname="fr.jayasoft.ivy.ant.IvyListModules"/>
<taskdef name="findmodule" classname="fr.jayasoft.ivy.ant.IvyFindModule"/>
</antlib>

View File

@ -0,0 +1,76 @@
/*
* This file is subject to the license found in LICENCE.TXT in the root directory of the project.
*
* #SNAPSHOT#
*/
package fr.jayasoft.ivy.ant;
import java.io.File;
import junit.framework.TestCase;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Delete;
public class IvyFindModuleTest extends TestCase {
private File _cache;
private IvyFindModule _findModule;
protected void setUp() throws Exception {
createCache();
Project project = new Project();
project.setProperty("ivy.conf.file", "test/repositories/ivyconf.xml");
_findModule = new IvyFindModule();
_findModule.setProject(project);
}
private void createCache() {
_cache = new File("build/cache");
_cache.mkdirs();
}
protected void tearDown() throws Exception {
cleanCache();
}
private void cleanCache() {
Delete del = new Delete();
del.setProject(new Project());
del.setDir(_cache);
del.execute();
}
public void testPrefix() throws Exception {
_findModule.setOrganisation("org1");
_findModule.setModule("mod1.1");
_findModule.setRevision("1.0");
_findModule.setPrefix("test");
_findModule.execute();
assertEquals("org1", _findModule.getProject().getProperty("test.organisation"));
assertEquals("mod1.1", _findModule.getProject().getProperty("test.module"));
assertEquals("1.0", _findModule.getProject().getProperty("test.revision"));
}
public void testLatest() throws Exception {
_findModule.setOrganisation("org1");
_findModule.setModule("mod1.1");
_findModule.setRevision("latest.integration");
_findModule.execute();
assertEquals("org1", _findModule.getProject().getProperty("ivy.organisation"));
assertEquals("mod1.1", _findModule.getProject().getProperty("ivy.module"));
assertEquals("2.0", _findModule.getProject().getProperty("ivy.revision"));
}
public void testLatestSubversion() throws Exception {
_findModule.setOrganisation("org1");
_findModule.setModule("mod1.1");
_findModule.setRevision("1.0+");
_findModule.setPrefix("");
_findModule.execute();
assertEquals("org1", _findModule.getProject().getProperty("organisation"));
assertEquals("mod1.1", _findModule.getProject().getProperty("module"));
assertEquals("1.0.1", _findModule.getProject().getProperty("revision"));
}
}

View File

@ -0,0 +1,66 @@
/*
* This file is subject to the license found in LICENCE.TXT in the root directory of the project.
*
* #SNAPSHOT#
*/
package fr.jayasoft.ivy.ant;
import java.io.File;
import junit.framework.TestCase;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Delete;
public class IvyListModulesTest extends TestCase {
private File _cache;
private IvyListModules _findModules;
protected void setUp() throws Exception {
createCache();
Project project = new Project();
project.setProperty("ivy.conf.file", "test/repositories/ivyconf.xml");
_findModules = new IvyListModules();
_findModules.setProject(project);
}
private void createCache() {
_cache = new File("build/cache");
_cache.mkdirs();
}
protected void tearDown() throws Exception {
cleanCache();
}
private void cleanCache() {
Delete del = new Delete();
del.setProject(new Project());
del.setDir(_cache);
del.execute();
}
public void testExact() throws Exception {
_findModules.setOrganisation("org1");
_findModules.setModule("mod1.1");
_findModules.setRevision("1.0");
_findModules.setProperty("found");
_findModules.setValue("[organisation]/[module]/[revision]");
_findModules.execute();
assertEquals("org1/mod1.1/1.0", _findModules.getProject().getProperty("found"));
}
public void testAllRevs() throws Exception {
_findModules.setOrganisation("org1");
_findModules.setModule("mod1.1");
_findModules.setRevision("*");
_findModules.setProperty("found.[revision]");
_findModules.setValue("true");
_findModules.execute();
assertEquals("true", _findModules.getProject().getProperty("found.1.0"));
assertEquals("true", _findModules.getProject().getProperty("found.1.1"));
assertEquals("true", _findModules.getProject().getProperty("found.2.0"));
}
}