org.apache.maven.model.resolution.UnresolvableModelException Java Examples

The following examples show how to use org.apache.maven.model.resolution.UnresolvableModelException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VersionSubstitutingModelResolverTest.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testNewCopy() throws UnresolvableModelException {
  ImmutableMap<String, String> substitution =
      ImmutableMap.of("com.google.guava:guava", "25.1-jre");
  VersionSubstitutingModelResolver resolver =
      new VersionSubstitutingModelResolver(
          session,
          null,
          repositorySystem,
          remoteRepositoryManager,
          ImmutableList.of(CENTRAL), // Needed when parent pom is not locally available
          substitution);

  ModelResolver copiedResolver = resolver.newCopy();
  Truth.assertThat(copiedResolver).isInstanceOf(VersionSubstitutingModelResolver.class);

  // request for guava:20.0 is replaced with 25.1-jre
  Truth.assertThat(copiedResolver.resolveModel(
      "com.google.guava", "guava", "20.0").getLocation()).contains("guava-25.1-jre.pom");
}
 
Example #2
Source File: MavenModelResolver.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public ModelSource resolveModel(String groupId, String artifactId, String version)
         throws UnresolvableModelException
{
   Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "", "pom", version);
   try
   {
      final ArtifactRequest request = new ArtifactRequest(pomArtifact, repositories, null);
      pomArtifact = system.resolveArtifact(session, request).getArtifact();

   }
   catch (ArtifactResolutionException e)
   {
      throw new UnresolvableModelException("Failed to resolve POM for " + groupId + ":" + artifactId + ":"
               + version + " due to " + e.getMessage(), groupId, artifactId, version, e);
   }

   final File pomFile = pomArtifact.getFile();

   return new FileModelSource(pomFile);

}
 
Example #3
Source File: RepositoryModelResolver.java    From archiva with Apache License 2.0 6 votes vote down vote up
public ModelSource resolveModel(Dependency dependency) throws UnresolvableModelException {
    try {
        Artifact artifact = new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(), "", "pom", dependency.getVersion());
        VersionRangeRequest versionRangeRequest = new VersionRangeRequest(artifact, null, null);
        VersionRangeResult versionRangeResult = this.versionRangeResolver.resolveVersionRange(this.session, versionRangeRequest);
        if (versionRangeResult.getHighestVersion() == null) {
            throw new UnresolvableModelException(String.format("No versions matched the requested dependency version range '%s'", dependency.getVersion()), dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
        } else if (versionRangeResult.getVersionConstraint() != null && versionRangeResult.getVersionConstraint().getRange() != null && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null) {
            throw new UnresolvableModelException(String.format("The requested dependency version range '%s' does not specify an upper bound", dependency.getVersion()), dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
        } else {
            dependency.setVersion(versionRangeResult.getHighestVersion().toString());
            return this.resolveModel(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
        }
    } catch (VersionRangeResolutionException var5) {
        throw new UnresolvableModelException(var5.getMessage(), dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), var5);
    }
}
 
Example #4
Source File: FlattenModelResolver.java    From flatten-maven-plugin with Apache License 2.0 6 votes vote down vote up
private String resolveParentVersionRange( String groupId, String artifactId, String version )
    throws UnresolvableModelException
{

    Dependency parentDependency = new Dependency();
    parentDependency.setGroupId( groupId );
    parentDependency.setArtifactId( artifactId );
    parentDependency.setVersion( version );
    parentDependency.setClassifier( "" );
    parentDependency.setType( "pom" );

    try
    {
        Iterable<ArtifactResult> artifactResults = depencencyResolver.resolveDependencies(
            projectBuildingRequest, singleton( parentDependency ), null, null );
        return artifactResults.iterator().next().getArtifact().getVersion();
    }
    catch ( DependencyResolverException e )
    {
        throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e );
    }
}
 
Example #5
Source File: FlattenModelResolver.java    From flatten-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves the POM for the specified parent.
 *
 * @param parent the parent coordinates to resolve, must not be {@code null}
 * @return The source of the requested POM, never {@code null}
 * @since Apache-Maven-3.2.2 (MNG-5639)
 */
public ModelSource resolveModel( Parent parent )
    throws UnresolvableModelException
{

    String groupId = parent.getGroupId();
    String artifactId = parent.getArtifactId();
    String version = parent.getVersion();

    // resolve version range (if present)
    if ( isRestrictedVersionRange( version, groupId, artifactId ) )
    {
        version = resolveParentVersionRange( groupId, artifactId, version );
    }

    return resolveModel( groupId, artifactId, version );
}
 
Example #6
Source File: POMParser.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ModelSource2 resolveModel(
    final String groupId, final String artifactId, final String version)
    throws UnresolvableModelException {
  final Config config = Config.load();
  final String localRepository = config.getMavenLocalRepository();
  final String parent = StringUtils.replace(groupId, ".", File.separator);
  final String path =
      Joiner.on(File.separator).join(localRepository, parent, artifactId, version);
  final String file = artifactId + '-' + version + ".pom";
  final File pom = new File(path, file);
  final boolean exists = pom.exists();

  loaded.add(pom);
  if (exists) {
    return new FileModelSource(pom);
  }
  return null;
}
 
Example #7
Source File: SimpleModelResolver.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
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, null);
        pomArtifact = system.resolveArtifact(session, request).getArtifact();
    } catch (org.eclipse.aether.resolution.ArtifactResolutionException ex) {
        throw new UnresolvableModelException(ex.getMessage(), groupId, artifactId, version, ex);
    } 

    File pomFile = pomArtifact.getFile();

    return new FileModelSource(pomFile);
}
 
Example #8
Source File: Resolver.java    From migration-tooling with Apache License 2.0 6 votes vote down vote up
/** Resolves an artifact as a root of a dependency graph. */
public void resolveArtifact(String artifactCoord) {
  Artifact artifact;
  ModelSource modelSource;
  try {
    artifact = ArtifactBuilder.fromCoords(artifactCoord);
    modelSource = modelResolver.resolveModel(artifact);
  } catch (UnresolvableModelException | InvalidArtifactCoordinateException e) {
    logger.warning(e.getMessage());
    return;
  }

  Rule rule = new Rule(artifact);
  rule.setRepository(modelSource.getLocation());
  rule.setSha1(downloadSha1(rule));
  deps.put(rule.name(), rule); // add the artifact rule to the workspace
  Model model = modelResolver.getEffectiveModel(modelSource);
  if (model != null) {
    traverseDeps(model, Sets.newHashSet(), Sets.newHashSet(), rule);
  }
}
 
Example #9
Source File: MavenModelBuilder.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public ModelBuildingResult build(ModelBuildingRequest request) throws ModelBuildingException {
    if (workspaceResolver != null) {
        request.setWorkspaceModelResolver(workspaceResolver);
        final Model requestModel = getModel(request);
        try {
            if (requestModel != null && workspaceResolver.resolveRawModel(ModelUtils.getGroupId(requestModel),
                    requestModel.getArtifactId(), ModelUtils.getVersion(requestModel)) != null) {
                completeWorkspaceProjectBuildRequest(request);
            }
        } catch (UnresolvableModelException e) {
            // ignore
        }
    }
    return builder.build(request);
}
 
Example #10
Source File: VersionSubstitutingModelResolver.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a file model source for {@code groupId:artifactId:version}.
 * The version is substituted if {@link #versionSubstitution} contains the
 * versionless coordinates ({@code groupId:artifactId}) in its keys.
 */
@Override
public ModelSource2 resolveModel(String groupId, String artifactId, String version)
    throws UnresolvableModelException {
  String versionlessCoordinates = groupId + ":" + artifactId;
  String versionToResolve = versionSubstitution.getOrDefault(versionlessCoordinates, version);
  return (ModelSource2) super.resolveModel(groupId, artifactId, versionToResolve);
}
 
Example #11
Source File: StatusProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void runMavenValidation(final POMModel model, final List<ErrorDescription> err) {
    File pom = model.getModelSource().getLookup().lookup(File.class);
    if (pom == null) {
        return;
    }
    
    List<ModelProblem> problems = runMavenValidationImpl(pom);
    for (ModelProblem problem : problems) {
        if (!problem.getSource().equals(pom.getAbsolutePath())) {
            LOG.log(Level.FINE, "found problem not in {0}: {1}", new Object[] {pom, problem.getSource()});
            continue;
        }
        int line = problem.getLineNumber();
        if (line <= 0) { // probably from a parent POM
            /* probably more irritating than helpful:
            line = 1; // fallback
            Parent parent = model.getProject().getPomParent();
            if (parent != null) {
                Line l = NbEditorUtilities.getLine(model.getBaseDocument(), parent.findPosition(), false);
                if (l != null) {
                    line = l.getLineNumber() + 1;
                }
            }
            */
            continue;
        }
        if (problem.getException() instanceof UnresolvableModelException) {
            // If a <parent> reference cannot be followed because e.g. no projects are opened (so no repos registered), just ignore it.
            continue;
        }
        try {
            err.add(ErrorDescriptionFactory.createErrorDescription(problem.getSeverity() == ModelProblem.Severity.WARNING ? Severity.WARNING : Severity.ERROR, problem.getMessage(), model.getBaseDocument(), line));
        } catch (IndexOutOfBoundsException x) {
            LOG.log(Level.WARNING, "improper line number: {0}", problem);
        }
    }
    
}
 
Example #12
Source File: DefaultModelResolver.java    From migration-tooling with Apache License 2.0 5 votes vote down vote up
@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;
    }
  }

  List<String> attemptedUrls = repositories.stream().map(Repository::getUrl).collect(toList());
  throw new UnresolvableModelException(
      "Could not find any repositories that knew how to "
          + "resolve "
          + groupId
          + ":"
          + artifactId
          + ":"
          + version
          + " (checked "
          + Joiner.on(", ").join(attemptedUrls)
          + ")",
      groupId,
      artifactId,
      version);
}
 
Example #13
Source File: LocalWorkspace.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public Model resolveRawModel(String groupId, String artifactId, String versionConstraint)
        throws UnresolvableModelException {
    final LocalProject project = getProject(groupId, artifactId);
    return project != null
            && (versionConstraint == null
                    || versionConstraint.equals(ModelUtils.getVersion(project.getRawModel())))
                            ? project.getRawModel()
                            : null;
}
 
Example #14
Source File: FlattenModelResolver.java    From flatten-maven-plugin with Apache License 2.0 5 votes vote down vote up
private static boolean isRestrictedVersionRange( String version, String groupId, String artifactId )
    throws UnresolvableModelException
{
    try
    {
        return VersionRange.createFromVersionSpec( version ).hasRestrictions();
    }
    catch ( InvalidVersionSpecificationException e )
    {
        throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e );
    }
}
 
Example #15
Source File: RepositoryModelResolver.java    From repairnator with MIT License 5 votes vote down vote up
@Override
public ModelSource resolveModel(String groupId, String artifactId, String versionId)
        throws UnresolvableModelException {
    File pom = getLocalFile(groupId, artifactId, versionId);

    if (!pom.exists()) {
        try {
            download(pom);
        } catch (IOException e) {
            throw new UnresolvableModelException("Could not download POM", groupId, artifactId, versionId, e);
        }
    }

    return new FileModelSource(pom);
}
 
Example #16
Source File: POMParser.java    From meghanada-server with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ModelSource2 resolveModel(Dependency dependency) throws UnresolvableModelException {
  return null;
}
 
Example #17
Source File: MavenModelResolver.java    From furnace with Eclipse Public License 1.0 4 votes vote down vote up
@Override public ModelSource resolveModel(Dependency dependency) throws UnresolvableModelException
{
   return resolveModel(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
}
 
Example #18
Source File: MavenModelResolver.java    From furnace with Eclipse Public License 1.0 4 votes vote down vote up
public ModelSource resolveModel(Parent parent)
         throws UnresolvableModelException
{
   // FIXME: Support version range. See DefaultModelResolver
   return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
}
 
Example #19
Source File: FlattenModelResolver.java    From flatten-maven-plugin with Apache License 2.0 4 votes vote down vote up
public ModelSource resolveModel( Dependency dependency ) throws UnresolvableModelException
{
    return resolveModel( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
}
 
Example #20
Source File: LocalWorkspace.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public Model resolveEffectiveModel(String groupId, String artifactId, String versionConstraint)
        throws UnresolvableModelException {
    return null;
}
 
Example #21
Source File: RepositoryModelResolver.java    From repairnator with MIT License 4 votes vote down vote up
@Override
public ModelSource resolveModel(Parent parent) throws UnresolvableModelException {
    return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
}
 
Example #22
Source File: DefaultModelResolver.java    From migration-tooling with Apache License 2.0 4 votes vote down vote up
public ModelSource resolveModel(Parent parent) throws UnresolvableModelException {
  return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
}
 
Example #23
Source File: DefaultModelResolver.java    From migration-tooling with Apache License 2.0 4 votes vote down vote up
public ModelSource resolveModel(Artifact artifact) throws UnresolvableModelException {
  return resolveModel(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
}
 
Example #24
Source File: SimpleModelResolver.java    From gate-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
public ModelSource resolveModel(Dependency dependency)
    throws UnresolvableModelException {
  return resolveModel(dependency.getGroupId(), dependency.getArtifactId(),
      dependency.getVersion());
}
 
Example #25
Source File: SimpleModelResolver.java    From gate-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ModelSource resolveModel(Parent parent)
    throws UnresolvableModelException {
  return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
  
}
 
Example #26
Source File: NBRepositoryModelResolver.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public ModelSource resolveModel(Dependency dpndnc) throws UnresolvableModelException {
    return resolveModel(dpndnc.getGroupId(), dpndnc.getArtifactId(), dpndnc.getVersion());
}
 
Example #27
Source File: NBRepositoryModelResolver.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public ModelSource resolveModel(Parent parent) throws UnresolvableModelException {
    return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
}
 
Example #28
Source File: MavenModelProblemsProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@NbBundle.Messages({
    "TXT_Artifact_Resolution_problem=Artifact Resolution problem",
    "TXT_Artifact_Not_Found=Artifact Not Found",
    "TXT_Cannot_Load_Project=Unable to properly load project",
    "TXT_Cannot_read_model=Error reading project model",
    "TXT_NoMsg=Exception thrown while loading maven project at {0}. See messages.log for more information."
})
private Collection<ProjectProblem> reportExceptions(MavenExecutionResult res) {
    List<ProjectProblem> toRet = new ArrayList<ProjectProblem>();
    for (Throwable e : res.getExceptions()) {
        LOG.log(Level.FINE, "Error on loading project " + project.getProjectDirectory(), e);
        if (e instanceof ArtifactResolutionException) { // XXX when does this occur?
            toRet.add(ProjectProblem.createError(TXT_Artifact_Resolution_problem(), getDescriptionText(e)));
            problemReporter.addMissingArtifact(((ArtifactResolutionException) e).getArtifact());
            
        } else if (e instanceof ArtifactNotFoundException) { // XXX when does this occur?
            toRet.add(ProjectProblem.createError(TXT_Artifact_Not_Found(), getDescriptionText(e)));
            problemReporter.addMissingArtifact(((ArtifactNotFoundException) e).getArtifact());
        } else if (e instanceof ProjectBuildingException) {
            toRet.add(ProjectProblem.createError(TXT_Cannot_Load_Project(), getDescriptionText(e), new SanityBuildAction(project)));
            if (e.getCause() instanceof ModelBuildingException) {
                ModelBuildingException mbe = (ModelBuildingException) e.getCause();
                for (ModelProblem mp : mbe.getProblems()) {
                    LOG.log(Level.FINE, mp.toString(), mp.getException());
                    if (mp.getException() instanceof UnresolvableModelException) {
                        // Probably obsoleted by ProblemReporterImpl.checkParent, but just in case:
                        UnresolvableModelException ume = (UnresolvableModelException) mp.getException();
                        problemReporter.addMissingArtifact(EmbedderFactory.getProjectEmbedder().createProjectArtifact(ume.getGroupId(), ume.getArtifactId(), ume.getVersion()));
                    } else if (mp.getException() instanceof PluginResolutionException) {
                        Plugin plugin = ((PluginResolutionException) mp.getException()).getPlugin();
                        // XXX this is not actually accurate; should rather pick out the ArtifactResolutionException & ArtifactNotFoundException inside
                        problemReporter.addMissingArtifact(EmbedderFactory.getProjectEmbedder().createArtifact(plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(), "jar"));
                    } else if (mp.getException() instanceof PluginManagerException) {
                        PluginManagerException ex = (PluginManagerException) mp.getException();                            
                        problemReporter.addMissingArtifact(EmbedderFactory.getProjectEmbedder().createArtifact(ex.getPluginGroupId(), ex.getPluginArtifactId(), ex.getPluginVersion(), "jar"));
                    }
                }
            }
        } else {
            String msg = e.getMessage();
            if(msg != null) {
                LOG.log(Level.INFO, "Exception thrown while loading maven project at " + project.getProjectDirectory(), e); //NOI18N
                toRet.add(ProjectProblem.createError(TXT_Cannot_read_model(), msg));
            } else {
                String path = project.getProjectDirectory().getPath();
                toRet.add(ProjectProblem.createError(TXT_Cannot_read_model(), TXT_NoMsg(path)));
                LOG.log(Level.WARNING, "Exception thrown while loading maven project at " + path, e); //NOI18N
            }
        }
    }
    return toRet;
}
 
Example #29
Source File: Resolver.java    From migration-tooling with Apache License 2.0 4 votes vote down vote up
private void addDependency(
    Dependency dependency,
    Model model,
    Set<String> topLevelScopes,
    Set<String> exclusions,
    @Nullable Rule parent) {
  String scope = dependency.getScope();
  // DependencyManagement dependencies don't have scope.
  if (scope != null) {
    if (parent == null) {
      // Top-level scopes get pulled in based on the user-provided scopes.
      if (!topLevelScopes.contains(scope)) {
        return;
      }
    } else {
      // TODO (bazel-devel): Relabel the scope of transitive dependencies so that they match how
      // maven relabels them as described here:
      // https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
      if (!INHERITED_SCOPES.contains(scope)) {
        return;
      }
    }
  }
  if (dependency.isOptional()) {
    return;
  }
  if (exclusions.contains(unversionedCoordinate(dependency))) {
    return;
  }
  try {
    Rule artifactRule =
        new Rule(ArtifactBuilder.fromMavenDependency(dependency, versionResolver));
    HashSet<String> localDepExclusions = Sets.newHashSet(exclusions);
    dependency
        .getExclusions()
        .forEach(exclusion -> localDepExclusions.add(unversionedCoordinate(exclusion)));

    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());
        artifactRule.setSha1(downloadSha1(artifactRule));
        Model depModel = modelResolver.getEffectiveModel(depModelSource);
        if (depModel != null) {
          traverseDeps(depModel, topLevelScopes, localDepExclusions, artifactRule);
        }
      } else {
        logger.warning("Could not get a model for " + dependency);
      }
    }

    if (parent == null) {
      addArtifact(artifactRule, TOP_LEVEL_ARTIFACT);
    } else {
      parent.addDependency(artifactRule);
      parent.getDependencies().addAll(artifactRule.getDependencies());
    }
  } catch (UnresolvableModelException | InvalidArtifactCoordinateException e) {
    logger.warning(
        "Could not resolve dependency "
            + dependency.getGroupId()
            + ":"
            + dependency.getArtifactId()
            + ":"
            + dependency.getVersion()
            + ": "
            + e.getMessage());
  }
}