Search in sources :

Example 1 with ModelSource

use of org.apache.maven.model.building.ModelSource 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 2 with ModelSource

use of org.apache.maven.model.building.ModelSource in project maven-plugins by apache.

the class InstallFileMojo method createMavenProject.

/**
 * Creates a Maven project in-memory from the user-supplied groupId, artifactId and version. When a classifier is
 * supplied, the packaging must be POM because the project with only have attachments. This project serves as basis
 * to attach the artifacts to install to.
 *
 * @return The created Maven project, never <code>null</code>.
 * @throws MojoExecutionException When the model of the project could not be built.
 * @throws MojoFailureException When building the project failed.
 */
private MavenProject createMavenProject() throws MojoExecutionException, MojoFailureException {
    if (groupId == null || artifactId == null || version == null || packaging == null) {
        throw new MojoExecutionException("The artifact information is incomplete: 'groupId', 'artifactId', " + "'version' and 'packaging' are required.");
    }
    ModelSource modelSource = new StringModelSource("<project><modelVersion>4.0.0</modelVersion><groupId>" + groupId + "</groupId><artifactId>" + artifactId + "</artifactId><version>" + version + "</version><packaging>" + (classifier == null ? packaging : "pom") + "</packaging></project>");
    ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
    pbr.setProcessPlugins(false);
    try {
        return projectBuilder.build(modelSource, pbr).getProject();
    } catch (ProjectBuildingException e) {
        if (e.getCause() instanceof ModelBuildingException) {
            throw new MojoExecutionException("The artifact information is not valid:" + Os.LINE_SEP + e.getCause().getMessage());
        }
        throw new MojoFailureException("Unable to create the project.", e);
    }
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException) StringModelSource(org.apache.maven.model.building.StringModelSource) StringModelSource(org.apache.maven.model.building.StringModelSource) ModelSource(org.apache.maven.model.building.ModelSource) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 3 with ModelSource

use of org.apache.maven.model.building.ModelSource in project drools by kiegroup.

the class MavenEmbedder method readProject.

// ----------------------------------------------------------------------
// Project
// ----------------------------------------------------------------------
public MavenProject readProject(final InputStream mavenProjectStream) throws ProjectBuildingException, MavenEmbedderException {
    ModelSource modelSource = new ModelSource() {

        @Override
        public InputStream getInputStream() {
            return mavenProjectStream;
        }

        @Override
        public String getLocation() {
            return "";
        }
    };
    ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
    try {
        org.eclipse.aether.artifact.Artifact lastArtifact = null;
        do {
            Thread.currentThread().setContextClassLoader(componentProvider.getSystemClassLoader());
            ProjectBuilder projectBuilder = componentProvider.lookup(ProjectBuilder.class);
            // BZ-1007894: Check if added dependencies are resolvable.
            ProjectBuildingResult result = projectBuilder.build(modelSource, getProjectBuildingRequest());
            if (result != null && result.getDependencyResolutionResult() != null && !result.getDependencyResolutionResult().getCollectionErrors().isEmpty()) {
                // A dependency resolution error has been produced. It can contains some error. Throw the first one to the client, so the user will fix every one sequentially.
                final Exception depedencyResolutionException = result.getDependencyResolutionResult().getCollectionErrors().get(0);
                if (depedencyResolutionException instanceof ArtifactDescriptorException) {
                    final org.eclipse.aether.artifact.Artifact artifact = ((ArtifactDescriptorException) depedencyResolutionException).getResult().getArtifact();
                    if (!artifact.equals(lastArtifact)) {
                        tryRemoveLocalArtifact(artifact);
                        lastArtifact = artifact;
                        continue;
                    }
                }
                if (depedencyResolutionException != null) {
                    throw new MavenEmbedderException(depedencyResolutionException.getMessage(), depedencyResolutionException);
                }
            }
            return (result == null || result.getProject() == null) ? null : result.getProject();
        } while (true);
    } catch (ComponentLookupException e) {
        throw new MavenEmbedderException(e.getMessage(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(originalCl);
        try {
            mavenProjectStream.close();
        } catch (IOException e) {
        }
    }
}
Also used : ProjectBuilder(org.apache.maven.project.ProjectBuilder) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) Artifact(org.eclipse.aether.artifact.Artifact) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) IOException(java.io.IOException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) ModelSource(org.apache.maven.model.building.ModelSource) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) InvalidRepositoryException(org.apache.maven.artifact.InvalidRepositoryException) IOException(java.io.IOException) MavenExecutionRequestPopulationException(org.apache.maven.execution.MavenExecutionRequestPopulationException)

Example 4 with ModelSource

use of org.apache.maven.model.building.ModelSource 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 5 with ModelSource

use of org.apache.maven.model.building.ModelSource 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

ModelSource (org.apache.maven.model.building.ModelSource)6 UnresolvableModelException (org.apache.maven.model.resolution.UnresolvableModelException)3 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)3 FileModelSource (org.apache.maven.model.building.FileModelSource)2 ModelBuildingException (org.apache.maven.model.building.ModelBuildingException)2 StringModelSource (org.apache.maven.model.building.StringModelSource)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)2 Artifact (org.eclipse.aether.artifact.Artifact)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 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1