Search in sources :

Example 6 with UnresolvableModelException

use of org.apache.maven.model.resolution.UnresolvableModelException in project maven by apache.

the class ProjectModelResolver method resolveModel.

public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException {
    Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "", "pom", version);
    try {
        ArtifactRequest request = new ArtifactRequest(pomArtifact, repositories, context);
        request.setTrace(trace);
        pomArtifact = resolver.resolveArtifact(session, request).getArtifact();
    } catch (ArtifactResolutionException e) {
        throw new UnresolvableModelException(e.getMessage(), groupId, artifactId, version, e);
    }
    return new ArtifactModelSource(pomArtifact.getFile(), groupId, artifactId, version);
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) ArtifactModelSource(org.apache.maven.model.building.ArtifactModelSource) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 7 with UnresolvableModelException

use of org.apache.maven.model.resolution.UnresolvableModelException in project maven by apache.

the class DefaultModelBuilder method doLoadDependencyManagement.

@SuppressWarnings("checkstyle:parameternumber")
private DependencyManagement doLoadDependencyManagement(Model model, ModelBuildingRequest request, DefaultModelProblemCollector problems, Dependency dependency, String groupId, String artifactId, String version, Collection<String> importIds) {
    DependencyManagement importMgmt;
    final WorkspaceModelResolver workspaceResolver = request.getWorkspaceModelResolver();
    final ModelResolver modelResolver = request.getModelResolver();
    if (workspaceResolver == null && modelResolver == null) {
        throw new NullPointerException(String.format("request.workspaceModelResolver and request.modelResolver cannot be null (parent POM %s and POM %s)", ModelProblemUtils.toId(groupId, artifactId, version), ModelProblemUtils.toSourceHint(model)));
    }
    Model importModel = null;
    if (workspaceResolver != null) {
        try {
            importModel = workspaceResolver.resolveEffectiveModel(groupId, artifactId, version);
        } catch (UnresolvableModelException e) {
            problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.BASE).setMessage(e.getMessage()).setException(e));
            return null;
        }
    }
    // no workspace resolver or workspace resolver returned null (i.e. model not in workspace)
    if (importModel == null) {
        final ModelSource importSource;
        try {
            importSource = modelResolver.resolveModel(dependency);
        } catch (UnresolvableModelException e) {
            StringBuilder buffer = new StringBuilder(256);
            buffer.append("Non-resolvable import POM");
            if (!containsCoordinates(e.getMessage(), groupId, artifactId, version)) {
                buffer.append(' ').append(ModelProblemUtils.toId(groupId, artifactId, version));
            }
            buffer.append(": ").append(e.getMessage());
            problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE).setMessage(buffer.toString()).setLocation(dependency.getLocation("")).setException(e));
            return null;
        }
        final ModelBuildingResult importResult;
        try {
            ModelBuildingRequest importRequest = new DefaultModelBuildingRequest();
            importRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
            importRequest.setModelCache(request.getModelCache());
            importRequest.setSystemProperties(request.getSystemProperties());
            importRequest.setUserProperties(request.getUserProperties());
            importRequest.setLocationTracking(request.isLocationTracking());
            importRequest.setModelSource(importSource);
            importRequest.setModelResolver(modelResolver.newCopy());
            importResult = build(importRequest, importIds);
        } catch (ModelBuildingException e) {
            problems.addAll(e.getProblems());
            return null;
        }
        problems.addAll(importResult.getProblems());
        importModel = importResult.getEffectiveModel();
    }
    importMgmt = importModel.getDependencyManagement();
    if (importMgmt == null) {
        importMgmt = new DependencyManagement();
    }
    return importMgmt;
}
Also used : ModelResolver(org.apache.maven.model.resolution.ModelResolver) WorkspaceModelResolver(org.apache.maven.model.resolution.WorkspaceModelResolver) Model(org.apache.maven.model.Model) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) WorkspaceModelResolver(org.apache.maven.model.resolution.WorkspaceModelResolver) DependencyManagement(org.apache.maven.model.DependencyManagement)

Example 8 with UnresolvableModelException

use of org.apache.maven.model.resolution.UnresolvableModelException in project maven by apache.

the class DefaultModelBuilder method readParentExternally.

private ModelData readParentExternally(Model childModel, ModelBuildingRequest request, ModelBuildingResult result, DefaultModelProblemCollector problems) throws ModelBuildingException {
    problems.setSource(childModel);
    Parent parent = childModel.getParent();
    String groupId = parent.getGroupId();
    String artifactId = parent.getArtifactId();
    String version = parent.getVersion();
    ModelResolver modelResolver = request.getModelResolver();
    Objects.requireNonNull(modelResolver, String.format("request.modelResolver cannot be null (parent POM %s and POM %s)", ModelProblemUtils.toId(groupId, artifactId, version), ModelProblemUtils.toSourceHint(childModel)));
    ModelSource modelSource;
    try {
        modelSource = modelResolver.resolveModel(parent);
    } catch (UnresolvableModelException e) {
        // Message below is checked for in the MNG-2199 core IT.
        StringBuilder buffer = new StringBuilder(256);
        buffer.append("Non-resolvable parent POM");
        if (!containsCoordinates(e.getMessage(), groupId, artifactId, version)) {
            buffer.append(' ').append(ModelProblemUtils.toId(groupId, artifactId, version));
        }
        if (childModel != problems.getRootModel()) {
            buffer.append(" for ").append(ModelProblemUtils.toId(childModel));
        }
        buffer.append(": ").append(e.getMessage());
        if (childModel.getProjectDirectory() != null) {
            if (parent.getRelativePath() == null || parent.getRelativePath().length() <= 0) {
                buffer.append(" and 'parent.relativePath' points at no local POM");
            } else {
                buffer.append(" and 'parent.relativePath' points at wrong local POM");
            }
        }
        problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.BASE).setMessage(buffer.toString()).setLocation(parent.getLocation("")).setException(e));
        throw problems.newModelBuildingException();
    }
    int validationLevel = Math.min(request.getValidationLevel(), ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0);
    ModelBuildingRequest lenientRequest = new DefaultModelBuildingRequest(request).setValidationLevel(validationLevel).setFileModel(null).setModelSource(modelSource);
    Model parentModel = readRawModel(lenientRequest, problems);
    if (!parent.getVersion().equals(version)) {
        String rawChildModelVersion = childModel.getVersion();
        if (rawChildModelVersion == null) {
            // Message below is checked for in the MNG-2199 core IT.
            problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.V31).setMessage("Version must be a constant").setLocation(childModel.getLocation("")));
        } else {
            if (rawChildVersionReferencesParent(rawChildModelVersion)) {
                // Message below is checked for in the MNG-2199 core IT.
                problems.add(new ModelProblemCollectorRequest(Severity.FATAL, Version.V31).setMessage("Version must be a constant").setLocation(childModel.getLocation("version")));
            }
        }
    // MNG-2199: What else to check here ?
    }
    return new ModelData(modelSource, parentModel, parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
}
Also used : Parent(org.apache.maven.model.Parent) Model(org.apache.maven.model.Model) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) ModelResolver(org.apache.maven.model.resolution.ModelResolver) WorkspaceModelResolver(org.apache.maven.model.resolution.WorkspaceModelResolver)

Example 9 with UnresolvableModelException

use of org.apache.maven.model.resolution.UnresolvableModelException in project maven by apache.

the class ProjectModelResolverTest method testResolveDependencyThrowsUnresolvableModelExceptionWhenNotFound.

@Test
public void testResolveDependencyThrowsUnresolvableModelExceptionWhenNotFound() throws Exception {
    final Dependency dependency = new Dependency();
    dependency.setGroupId("org.apache");
    dependency.setArtifactId("apache");
    dependency.setVersion("0");
    UnresolvableModelException e = assertThrows(UnresolvableModelException.class, () -> newModelResolver().resolveModel(dependency), "Expected 'UnresolvableModelException' not thrown.");
    assertNotNull(e.getMessage());
    assertThat(e.getMessage(), startsWith("Could not find artifact org.apache:apache:pom:0 in central"));
}
Also used : UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) Dependency(org.apache.maven.model.Dependency) Test(org.junit.jupiter.api.Test)

Example 10 with UnresolvableModelException

use of org.apache.maven.model.resolution.UnresolvableModelException in project maven by apache.

the class DefaultModelResolver method resolveModel.

@Override
public ModelSource resolveModel(final Parent parent) throws UnresolvableModelException {
    try {
        final Artifact artifact = new DefaultArtifact(parent.getGroupId(), parent.getArtifactId(), "", "pom", parent.getVersion());
        final VersionRangeRequest versionRangeRequest = new VersionRangeRequest(artifact, repositories, context);
        versionRangeRequest.setTrace(trace);
        final VersionRangeResult versionRangeResult = versionRangeResolver.resolveVersionRange(session, versionRangeRequest);
        if (versionRangeResult.getHighestVersion() == null) {
            throw new UnresolvableModelException(String.format("No versions matched the requested parent version range '%s'", parent.getVersion()), parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
        }
        if (versionRangeResult.getVersionConstraint() != null && versionRangeResult.getVersionConstraint().getRange() != null && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null) {
            // Message below is checked for in the MNG-2199 core IT.
            throw new UnresolvableModelException(String.format("The requested parent version range '%s' does not specify an upper bound", parent.getVersion()), parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
        }
        parent.setVersion(versionRangeResult.getHighestVersion().toString());
        return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
    } catch (final VersionRangeResolutionException e) {
        throw new UnresolvableModelException(e.getMessage(), parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), e);
    }
}
Also used : VersionRangeResult(org.eclipse.aether.resolution.VersionRangeResult) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) VersionRangeRequest(org.eclipse.aether.resolution.VersionRangeRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException)

Aggregations

UnresolvableModelException (org.apache.maven.model.resolution.UnresolvableModelException)39 Artifact (org.eclipse.aether.artifact.Artifact)17 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)16 Test (org.junit.jupiter.api.Test)12 FileModelSource (org.apache.maven.model.building.FileModelSource)9 VersionRangeResolutionException (org.eclipse.aether.resolution.VersionRangeResolutionException)9 Dependency (org.apache.maven.model.Dependency)8 Parent (org.apache.maven.model.Parent)8 VersionRangeRequest (org.eclipse.aether.resolution.VersionRangeRequest)8 VersionRangeResult (org.eclipse.aether.resolution.VersionRangeResult)8 Model (org.apache.maven.model.Model)6 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)5 File (java.io.File)4 InvalidRepositoryException (org.apache.maven.model.resolution.InvalidRepositoryException)4 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)4 ArtifactModelSource (org.apache.maven.model.building.ArtifactModelSource)3 ModelSource (org.apache.maven.model.building.ModelSource)3 WorkspaceModelResolver (org.apache.maven.model.resolution.WorkspaceModelResolver)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2