use of org.apache.maven.model.resolution.UnresolvableModelException in project bazel by bazelbuild.
the class DefaultModelResolver method getModelSource.
// TODO(kchodorow): make this work with local repositories.
private UrlModelSource getModelSource(String url, String groupId, String artifactId, String version) throws UnresolvableModelException {
try {
if (!url.endsWith("/")) {
url += "/";
}
URL urlUrl = new URL(url + groupId.replaceAll("\\.", "/") + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".pom");
if (pomFileExists(urlUrl)) {
UrlModelSource urlModelSource = new UrlModelSource(urlUrl);
ruleNameToModelSource.put(Rule.name(groupId, artifactId), urlModelSource);
return urlModelSource;
}
} catch (MalformedURLException e) {
throw new UnresolvableModelException("Bad URL " + url + ": " + e.getMessage(), groupId, artifactId, version, e);
}
return null;
}
use of org.apache.maven.model.resolution.UnresolvableModelException in project bazel by bazelbuild.
the class Resolver method resolveEffectiveModel.
/**
* Resolves all dependencies from a given "model source," which could be either a URL or a local
* file.
* @return the model.
*/
@Nullable
public Model resolveEffectiveModel(ModelSource modelSource, Set<String> exclusions, Rule parent) {
Model model = modelResolver.getEffectiveModel(modelSource, handler);
if (model == null) {
return null;
}
for (Repository repo : model.getRepositories()) {
modelResolver.addRepository(repo);
}
for (Dependency dependency : model.getDependencies()) {
if (!dependency.getScope().equals(COMPILE_SCOPE)) {
continue;
}
if (dependency.isOptional()) {
continue;
}
if (exclusions.contains(dependency.getGroupId() + ":" + dependency.getArtifactId())) {
continue;
}
try {
Rule artifactRule = new Rule(getArtifact(dependency), dependency.getExclusions());
HashSet<String> localDepExclusions = new HashSet<>(exclusions);
localDepExclusions.addAll(artifactRule.getExclusions());
boolean isNewDependency = addArtifact(artifactRule, model.toString());
if (isNewDependency) {
ModelSource depModelSource = modelResolver.resolveModel(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
if (depModelSource != null) {
artifactRule.setRepository(depModelSource.getLocation(), handler);
artifactRule.setSha1(downloadSha1(artifactRule));
resolveEffectiveModel(depModelSource, localDepExclusions, artifactRule);
} else {
handler.handle(Event.error("Could not get a model for " + dependency));
}
}
if (parent != null) {
parent.addDependency(artifactRule);
parent.getDependencies().addAll(artifactRule.getDependencies());
} else {
addArtifact(artifactRule, modelSource.getLocation());
}
} catch (UnresolvableModelException | InvalidArtifactCoordinateException e) {
handler.handle(Event.error("Could not resolve dependency " + dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getVersion() + ": " + e.getMessage()));
}
}
return model;
}
use of org.apache.maven.model.resolution.UnresolvableModelException in project bazel by bazelbuild.
the class WorkspaceResolver method resolveTransitiveDependencies.
/**
* Calculates transitive dependencies of the given //external package.
*/
public void resolveTransitiveDependencies(Package externalPackage) {
Location location = Location.fromFile(externalPackage.getFilename());
for (Target target : externalPackage.getTargets()) {
// Targets are //external:foo.
if (target.getTargetKind().startsWith("maven_jar ")) {
RepositoryName repositoryName;
try {
repositoryName = RepositoryName.create("@" + target.getName());
} catch (LabelSyntaxException e) {
handler.handle(Event.error(location, "Invalid repository name for " + target + ": " + e.getMessage()));
return;
}
com.google.devtools.build.lib.packages.Rule workspaceRule = externalPackage.getRule(repositoryName.strippedName());
DefaultModelResolver modelResolver = resolver.getModelResolver();
AttributeMap attributeMap = AggregatingAttributeMapper.of(workspaceRule);
Rule rule;
try {
rule = new Rule(Resolver.getArtifact(attributeMap.get("artifact", Type.STRING)));
} catch (InvalidArtifactCoordinateException e) {
handler.handle(Event.error(location, "Couldn't get attribute: " + e.getMessage()));
return;
}
if (attributeMap.isAttributeValueExplicitlySpecified("repository")) {
modelResolver.addUserRepository(attributeMap.get("repository", Type.STRING));
rule.setRepository(attributeMap.get("repository", Type.STRING), handler);
}
if (attributeMap.isAttributeValueExplicitlySpecified("sha1")) {
rule.setSha1(attributeMap.get("sha1", Type.STRING));
} else {
rule.setSha1(resolver.downloadSha1(rule));
}
ModelSource modelSource;
try {
modelSource = modelResolver.resolveModel(rule.groupId(), rule.artifactId(), rule.version());
} catch (UnresolvableModelException e) {
handler.handle(Event.error("Could not resolve model for " + target + ": " + e.getMessage()));
continue;
}
resolver.addArtifact(rule, modelSource.getLocation());
resolver.resolveEffectiveModel(modelSource, Sets.<String>newHashSet(), rule);
} else if (!target.getTargetKind().startsWith("bind") && !target.getTargetKind().startsWith("source ")) {
handler.handle(Event.warn(location, "Cannot fetch transitive dependencies for " + target + " yet, skipping"));
}
}
}
use of org.apache.maven.model.resolution.UnresolvableModelException in project bazel by bazelbuild.
the class DefaultModelResolver method resolveModel.
@Override
public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException {
String ruleName = Rule.name(groupId, artifactId);
if (ruleNameToModelSource.containsKey(ruleName)) {
return ruleNameToModelSource.get(ruleName);
}
for (Repository repository : repositories) {
UrlModelSource modelSource = getModelSource(repository.getUrl(), groupId, artifactId, version);
if (modelSource != null) {
return modelSource;
}
}
// TODO(kchodorow): use Java 8 features to make this a one-liner.
List<String> attemptedUrls = Lists.newArrayList();
for (Repository repository : repositories) {
attemptedUrls.add(repository.getUrl());
}
throw new UnresolvableModelException("Could not find any repositories that knew how to " + "resolve " + groupId + ":" + artifactId + ":" + version + " (checked " + Joiner.on(", ").join(attemptedUrls) + ")", groupId, artifactId, version);
}
use of org.apache.maven.model.resolution.UnresolvableModelException in project bazel by bazelbuild.
the class Resolver method resolveArtifact.
/**
* Resolves an artifact as a root of a dependency graph.
*/
public void resolveArtifact(String artifactCoord) {
Artifact artifact;
ModelSource modelSource;
try {
artifact = getArtifact(artifactCoord);
modelSource = modelResolver.resolveModel(artifact);
} catch (UnresolvableModelException | InvalidArtifactCoordinateException e) {
handler.handle(Event.error(e.getMessage()));
return;
}
Rule rule = new Rule(artifact);
// add the artifact rule to the workspace
deps.put(rule.name(), rule);
resolveEffectiveModel(modelSource, Sets.<String>newHashSet(), rule);
}
Aggregations