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);
}
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;
}
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());
}
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"));
}
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);
}
}
Aggregations