Search in sources :

Example 1 with UnresolvableModelException

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;
}
Also used : MalformedURLException(java.net.MalformedURLException) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) UrlModelSource(org.apache.maven.model.building.UrlModelSource) URL(java.net.URL)

Example 2 with UnresolvableModelException

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;
}
Also used : Repository(org.apache.maven.model.Repository) Model(org.apache.maven.model.Model) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) Dependency(org.apache.maven.model.Dependency) FileModelSource(org.apache.maven.model.building.FileModelSource) ModelSource(org.apache.maven.model.building.ModelSource) HashSet(java.util.HashSet) Nullable(javax.annotation.Nullable)

Example 3 with UnresolvableModelException

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"));
        }
    }
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) DefaultModelResolver(com.google.devtools.build.workspace.maven.DefaultModelResolver) RepositoryName(com.google.devtools.build.lib.cmdline.RepositoryName) ModelSource(org.apache.maven.model.building.ModelSource) InvalidArtifactCoordinateException(com.google.devtools.build.workspace.maven.Resolver.InvalidArtifactCoordinateException) Target(com.google.devtools.build.lib.packages.Target) AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) Rule(com.google.devtools.build.workspace.maven.Rule) Location(com.google.devtools.build.lib.events.Location)

Example 4 with UnresolvableModelException

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);
}
Also used : Repository(org.apache.maven.model.Repository) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) UrlModelSource(org.apache.maven.model.building.UrlModelSource)

Example 5 with UnresolvableModelException

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);
}
Also used : UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) FileModelSource(org.apache.maven.model.building.FileModelSource) ModelSource(org.apache.maven.model.building.ModelSource)

Aggregations

UnresolvableModelException (org.apache.maven.model.resolution.UnresolvableModelException)7 FileModelSource (org.apache.maven.model.building.FileModelSource)4 ModelSource (org.apache.maven.model.building.ModelSource)3 Repository (org.apache.maven.model.Repository)2 UrlModelSource (org.apache.maven.model.building.UrlModelSource)2 Artifact (org.eclipse.aether.artifact.Artifact)2 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)2 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)1 Location (com.google.devtools.build.lib.events.Location)1 AttributeMap (com.google.devtools.build.lib.packages.AttributeMap)1 Target (com.google.devtools.build.lib.packages.Target)1 DefaultModelResolver (com.google.devtools.build.workspace.maven.DefaultModelResolver)1 InvalidArtifactCoordinateException (com.google.devtools.build.workspace.maven.Resolver.InvalidArtifactCoordinateException)1 Rule (com.google.devtools.build.workspace.maven.Rule)1 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1