package aQute.library.util;
import aQute.lib.struct.*;
import aQute.maven.*;
import aQute.maven.Pom.Dependency;
import aQute.maven.Pom.PomRef;
import aQute.maven.ns.*;
import aQute.service.library.*;
import aQute.service.library.Library.Revision;
public class PomUtil {
public static void merge(Revision r, Pom pom) throws Exception {
StructUtil.merge(pom, r);
if (r.groupId.equals(Library.OSGI_GROUP) || r.groupId.equals(Library.SHA_GROUP)) {
r.groupId = pom.groupId;
r.artifactId = pom.artifactId;
r.version = pom.version;
}
// The basic scanner assigns 0.0.0 if no version is set in the bundle
if (r.version == null || r.version.equals("0.0.0")) {
if (pom.version != null)
r.version = pom.version;
}
// fixup: The pom date (from the last modified time of the
// server seems more reliable)
if (pom.created != 0) {
r.created = pom.created;
}
// fixup, set the sha of the pom into the
// rev.
r.pom = pom.sha;
// Fixup, see if the pom is relocated
if (pom.distributionManagement != null) {
PomRef ref = pom.distributionManagement.relocation;
if (ref != null) {
Library.Relocation reloc = new Library.Relocation();
if (ref.groupId != null)
reloc.groupId = ref.groupId;
else
reloc.groupId = pom.groupId;
if (ref.artifactId != null)
reloc.artifactId = ref.artifactId;
else
reloc.artifactId = pom.artifactId;
if (ref.version != null)
reloc.version = ref.version;
else
reloc.version = pom.version;
reloc.message = ref.message;
r.relocation = reloc;
}
}
Library.Capability cap = new Library.Capability();
cap.ns = "x-maven";
cap.ps.put("g", pom.groupId);
cap.ps.put("a", pom.artifactId);
cap.ps.put("v", pom.version);
if (pom.classifier != null)
cap.ps.put("classifier", pom.classifier);
if (pom.packaging != null)
cap.ps.put("p", pom.packaging);
else
cap.ps.put("p", "jar");
r.capabilities.add(cap);
if (pom.dependencies != null) {
int n = 0;
for (Dependency dep : pom.dependencies) {
if (dep.groupId == null) {
r.errors.add("No group ID set on dependency " + n);
dep.groupId = "not set";
}
if (dep.artifactId == null) {
r.errors.add("No artifactId set on dependency " + n);
dep.artifactId = "not set";
}
if (dep.version == null) {
r.errors.add("No artifactId set on dependency " + n);
dep.version = "not set";
}
Library.Requirement rq = new Library.Requirement();
rq.ns = MavenNamespace.NAME;
StringBuilder sb = new StringBuilder("(&(g=");
sb.append(dep.groupId).append(")(a=").append(dep.artifactId).append(")(v=").append(dep.version);
if (dep.classifier == null) {
sb.append(")(!(c=*))");
} else {
sb.append(")(c=").append(dep.classifier);
}
sb.append("))");
rq.ps.put("filter:", sb.toString());
// TODO do we need another effective time?
rq.ps.put("effective:", dep.scope == null ? "compile" : dep.scope);
rq.name = MavenRepository.getCoordinates(dep.groupId, dep.artifactId, dep.classifier, dep.version);
r.requirements.add(rq);
n++;
}
}
}
}