URL location = getURL(artifact);
OpenResult result = open(artifact, location);
if (result != null) {
return result;
}
Version version = artifact.getVersion();
// Snapshot artifacts can have a special filename in an online maven repo.
// The version number is replaced with a timestmap and build number.
// The maven-metadata file contains this extra information.
if (version.toString().indexOf("SNAPSHOT") >= 0 && !(version instanceof SnapshotVersion)) {
// base path for the artifact version in a maven repo
URI basePath = base.resolve(artifact.getGroupId().replace('.', '/') + "/" + artifact.getArtifactId() + "/" + version + "/");
// get the maven-metadata file
Document metadata = getMavenMetadata(basePath);
// determine the snapshot qualifier from the maven-metadata file
if (metadata != null) {
NodeList snapshots = metadata.getDocumentElement().getElementsByTagName("snapshot");
if (snapshots.getLength() >= 1) {
Element snapshot = (Element) snapshots.item(0);
List<String> timestamp = getChildrenText(snapshot, "timestamp");
List<String> buildNumber = getChildrenText(snapshot, "buildNumber");
if (timestamp.size() >= 1 && buildNumber.size() >= 1) {
try {
// recurse back into this method using a SnapshotVersion
SnapshotVersion snapshotVersion = new SnapshotVersion(version);
snapshotVersion.setBuildNumber(Integer.parseInt(buildNumber.get(0)));
snapshotVersion.setTimestamp(timestamp.get(0));
Artifact newQuery = new Artifact(artifact.getGroupId(), artifact.getArtifactId(), snapshotVersion, artifact.getType());
location = getURL(newQuery);
return open(artifact, location);
} catch (NumberFormatException nfe) {
// log.error("Could not create snapshot version for " + artifact, nfe);
}
} else {
// log.error("Could not create snapshot version for " + artifact);
}
}
}
}
return null;
}
// Version is not resolved. Look in maven-metadata.xml and maven-metadata-local.xml for
// the available version numbers. If found then recurse into the enclosing method with
// a resolved version number
else {
// base path for the artifact version in a maven repo
URI basePath = base.resolve(artifact.getGroupId().replace('.', '/') + "/" + artifact.getArtifactId() + "/");
// get the maven-metadata file
Document metadata = getMavenMetadata(basePath);
// determine the available versions from the maven-metadata file
if (metadata != null) {
Element root = metadata.getDocumentElement();
NodeList list = root.getElementsByTagName("versions");
list = ((Element) list.item(0)).getElementsByTagName("version");
Version[] available = new Version[list.getLength()];
for (int i = 0; i < available.length; i++) {
available[i] = new Version(getText(list.item(i)));
}
// desc sort
Arrays.sort(available, new Comparator<Version>() {
public int compare(Version o1, Version o2) {
return o2.toString().compareTo(o1.toString());