/** 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); } }
@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); }
@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 = ClassNameUtils.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(); if (!loaded.contains(pom)) { loaded.add(pom); } if (exists) { return new FileModelSource(pom); } return null; }
@Override public ModelSource2 resolveModel(final Parent parent) throws UnresolvableModelException { final String groupId = parent.getGroupId(); final String artifactId = parent.getArtifactId(); final String version = parent.getVersion(); final ModelSource2 model = resolveModel(groupId, artifactId, version); if (nonNull(model)) { return model; } String relativePath = parent.getRelativePath(); if (nonNull(relativePath) && !relativePath.isEmpty()) { File pom = new File(this.projectRoot, relativePath); if (!relativePath.endsWith("pom.xml")) { pom = new File(relativePath, "pom.xml"); } if (!loaded.contains(pom)) { loaded.add(pom); } if (pom.exists()) { return new FileModelSource(pom); } } return null; }
@Override public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { Map<String, String> dependency = new HashMap<String, String>(); dependency.put("group", groupId); dependency.put("module", artifactId); dependency.put("version", version); dependency.put("type", "pom"); try { return new UrlModelSource( Grape.getInstance().resolve(null, dependency)[0].toURL()); } catch (MalformedURLException e) { throw new UnresolvableModelException(e.getMessage(), groupId, artifactId, version); } }
@Override public ModelSource resolveModel(Parent parent) throws UnresolvableModelException { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Processing parent model {}", parent.getRelativePath()); } Artifact artifact = new DefaultArtifact(parent.getGroupId(), parent.getArtifactId(), StringUtils.EMPTY, POM, parent.getVersion()); VersionRangeRequest versionRangeRequest = new VersionRangeRequest(artifact, repositories, null); try { VersionRangeResult versionRangeResult = repositorySystem.resolveVersionRange(repositorySystemSession, versionRangeRequest); parent.setVersion(versionRangeResult.getHighestVersion().toString()); } catch (VersionRangeResolutionException e) { throw new UnresolvableModelException(e.getMessage(), parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), e); } return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion()); }
@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); }
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 ); } File pomFile = pomArtifact.getFile(); return new FileModelSource( pomFile ); }
private ModelProblem hasMissingParentPom( ProjectBuildingException e ) { if ( e.getCause() instanceof ModelBuildingException ) { ModelBuildingException mbe = (ModelBuildingException) e.getCause(); for ( ModelProblem problem : mbe.getProblems() ) { if ( problem.getException() instanceof UnresolvableModelException ) { return problem; } } } return null; }
@Override public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { LocalRepository localRepo = repositorySystemSession.getLocalRepository(); File localRepoDir = localRepo.getBasedir(); StringBuilder pomPath = new StringBuilder(); pomPath.append(groupId.replace('.', File.separatorChar)); pomPath.append(File.separatorChar); pomPath.append(artifactId); pomPath.append(File.separatorChar); pomPath.append(version); pomPath.append(File.separatorChar); pomPath.append(artifactId); pomPath.append('-'); pomPath.append(version); pomPath.append(".pom"); File pomFile = new File(localRepoDir, pomPath.toString()); if (pomFile.exists()) { return new FileModelSource(pomFile); } else { throw new UnresolvableModelException("POM does not exist in local repository: " + pomFile, groupId, artifactId, version); } }
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); } } }
@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); }
/** * 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 { Dependency parentDependency = new Dependency(); parentDependency.setGroupId(parent.getGroupId()); parentDependency.setArtifactId(parent.getArtifactId()); parentDependency.setVersion(parent.getVersion()); parentDependency.setClassifier(""); parentDependency.setType("pom"); Artifact parentArtifact = null; try { Iterable<ArtifactResult> artifactResults = depencencyResolver.resolveDependencies( projectBuildingRequest, singleton(parentDependency), null, null ); Iterator<ArtifactResult> iterator = artifactResults.iterator(); if (iterator.hasNext()) { parentArtifact = iterator.next().getArtifact(); } } catch (DependencyResolverException e) { throw new UnresolvableModelException( e.getMessage(), parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), e ); } return resolveModel( parentArtifact.getGroupId(), parentArtifact.getArtifactId(), parentArtifact.getVersion() ); }
@Override public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, StringUtils.EMPTY, POM, version); try { ArtifactRequest request = new ArtifactRequest(pomArtifact, repositories, null); pomArtifact = repositorySystem.resolveArtifact(repositorySystemSession, request).getArtifact(); } catch (ArtifactResolutionException e) { throw new UnresolvableModelException(e.getMessage(), groupId, artifactId, version, e); } return new FileModelSource(pomArtifact.getFile()); }
public ModelSource resolveModel( String groupId, String artifactId, String version ) throws UnresolvableModelException { File pomFile = null; if ( modelPool != null ) { pomFile = modelPool.get( groupId, artifactId, version ); } if ( pomFile == null ) { 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 ); } pomFile = pomArtifact.getFile(); } return new FileModelSource( pomFile ); }
@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; }
@Override public ModelSource resolveModel(Parent parent) throws UnresolvableModelException { return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion()); }
public ModelSource resolveModel(Artifact artifact) throws UnresolvableModelException { return resolveModel(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()); }
public ModelSource resolveModel(Parent parent) throws UnresolvableModelException { return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion()); }
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()); } }
@Override public ModelSource2 resolveModel(Dependency dependency) throws UnresolvableModelException { return null; }
public ModelSource resolveModel(Dependency dependency) throws UnresolvableModelException { return resolveModel( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() ); }
@Override public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { Artifact artifact = new DefaultArtifact(groupId + ":" + artifactId + ":" + version); return new FileModelSource(Maven.getMetadata(system, session, artifact).getFile()); }
public ModelSource resolveModel(Parent parent) throws UnresolvableModelException { // FIXME: Support version range. See DefaultModelResolver return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion()); }
private ModelData readParentExternally( Model childModel, ModelBuildingRequest request, 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(); if ( modelResolver == null ) { throw new IllegalArgumentException( "no model resolver provided, cannot resolve parent POM " + ModelProblemUtils.toId( groupId, artifactId, version ) + " for POM " + ModelProblemUtils.toSourceHint( childModel ) ); } ModelSource modelSource; try { modelSource = modelResolver.resolveModel( groupId, artifactId, version ); } catch ( UnresolvableModelException e ) { 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(); } ModelBuildingRequest lenientRequest = request; if ( request.getValidationLevel() > ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0 ) { lenientRequest = new FilterModelBuildingRequest( request ) { @Override public int getValidationLevel() { return ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0; } }; } Model parentModel = readModel( modelSource, null, lenientRequest, problems ); ModelData parentData = new ModelData( parentModel, groupId, artifactId, version ); return parentData; }