/** * Links the given bundle together with all bundle dependencies. * * <p>The returned repository will have the linked runtime representation of the given bundle as first element of * {@link RuntimeRepository#getBundles()}. */ private static RuntimeRepository link(MutableBundle bundle, JAXBContext jaxbContext, DependencyResolutionResult dependencyResolution, ClassLoader classLoader) throws JAXBException, LinkerException, XMLStreamException { List<MutableBundle> bundles = new ArrayList<>(); bundles.add(bundle); bundles.addAll(loadDependencyBundles(jaxbContext, getBundleDependencies(dependencyResolution))); LinkerOptions linkerOptions = new LinkerOptions.Builder() .setClassProvider(name -> Optional.of(Class.forName(name.getBinaryName().toString(), true, classLoader))) .setDeserializeSerializationTrees(true) .setUnmarshalClassLoader(classLoader) .setMarshalValues(true) .build(); return Linker.createRepository(bundles, linkerOptions); }
public Set<BundleArtifact> getBundleDependencies(String... scopes) throws MojoExecutionException { ScopeDependencyFilter scopeFilter = new ScopeDependencyFilter(Arrays.asList(scopes), null); DefaultDependencyResolutionRequest dependencyResolutionRequest = new DefaultDependencyResolutionRequest( project, repositorySystemSession); dependencyResolutionRequest.setResolutionFilter(scopeFilter); DependencyResolutionResult dependencyResolutionResult; try { dependencyResolutionResult = projectDependenciesResolver .resolve(dependencyResolutionRequest); } catch (DependencyResolutionException ex) { throw new MojoExecutionException(ex.getMessage(), ex); } DependencyNode dependencyGraph = dependencyResolutionResult.getDependencyGraph(); if (dependencyGraph != null) { checkBundleDependencies(dependencyGraph.getChildren()); return getBundleArtifacts(dependencyGraph.getChildren()); } else { return new HashSet<BundleArtifact>(); } }
protected void resolveDependencies() throws MojoExecutionException { DependencyResolutionResult depRes; try { DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(mavenSession.getRepositorySession()); ((DefaultRepositorySystemSession)repositorySession).setDependencySelector(newRootScopeDependencySelector(repositorySession.getDependencySelector(), 0)); DefaultDependencyResolutionRequest depReq = new DefaultDependencyResolutionRequest(project, repositorySession); depRes = depsResolver.resolve(depReq); } catch (DependencyResolutionException e) { throw new MojoExecutionException("Cannot resolve dependencies for "+project, e); } rootDependencyGraph = depRes.getDependencyGraph(); getLog().debug("Dependency graph with scopes "+includeDependencyScopes+":"); dump("", rootDependencyGraph); projectByIdCache.put(Coords.of(project).normal(), project); collectDeps(rootDependencyGraph, project, 0); }
/** * Returns a list of bundle artifacts that the current Maven project (transitively) depends on. */ private static List<Artifact> getBundleDependencies(DependencyResolutionResult dependencyResolutionResult) { List<Artifact> bundleArtifacts = new ArrayList<>(); for (Dependency dependency: dependencyResolutionResult.getDependencies()) { Artifact artifact = dependency.getArtifact(); if (artifact.getExtension().equals(Bundles.ARTIFACT_TYPE) && CLASSPATH_JAVA_SCOPES.contains(dependency.getScope())) { bundleArtifacts.add(artifact); } } return bundleArtifacts; }
/** * Returns a list of {@link URL}s that need to be in the classpath before executing any of the Java code in the * current project. */ private List<URL> getClasspaths(DependencyResolutionResult dependencyResolutionResult) throws MalformedURLException { List<URL> artifactPaths = new ArrayList<>(); artifactPaths.add(buildOutputDirectory.toURI().toURL()); for (Dependency dependency: dependencyResolutionResult.getDependencies()) { Artifact artifact = dependency.getArtifact(); if (artifact.getExtension().equals(JAR_ARTIFACT_TYPE) && CLASSPATH_JAVA_SCOPES.contains(dependency.getScope())) { artifactPaths.add(artifact.getFile().toURI().toURL()); } } return artifactPaths; }
public MavenExecutionResult(@Nullable MavenProject mavenProject, @Nullable DependencyResolutionResult dependencyResolutionResult, List<Exception> exceptions) { myMavenProject = mavenProject; myExceptions = exceptions == null ? new ArrayList<Exception>() : exceptions; myDependencyResolutionResult = dependencyResolutionResult; if(myDependencyResolutionResult != null && myDependencyResolutionResult.getCollectionErrors() != null) { myExceptions.addAll(myDependencyResolutionResult.getCollectionErrors()); } }
public MavenResult( MavenProject mavenProject, DependencyResolutionResult dependencyResolutionResult, List<Exception> exceptions) { this.mavenProject = mavenProject; this.dependencyResolutionResult = dependencyResolutionResult; this.exceptions = exceptions; }
private MavenServerResult createResult(File pom, MavenResult mavenResult) throws RemoteException { List<MavenProjectProblem> problems = new ArrayList<>(); Set<MavenKey> unresolvedArtifacts = new HashSet<>(); validate(pom, mavenResult.getExceptions(), problems); MavenProject project = mavenResult.getMavenProject(); if (project == null) { return new MavenServerResult(null, problems, unresolvedArtifacts); } MavenModel model = null; try { DependencyResolutionResult resolutionResult = mavenResult.getDependencyResolutionResult(); org.eclipse.aether.graph.DependencyNode dependencyNode = null; if (resolutionResult != null) { dependencyNode = resolutionResult.getDependencyGraph(); } List<org.eclipse.aether.graph.DependencyNode> dependencyNodes = null; if (dependencyNode != null) { dependencyNodes = dependencyNode.getChildren(); } model = MavenModelUtil.convertProjectToModel( project, dependencyNodes, new File(localRepo.getBasedir())); } catch (Exception e) { validate(project.getFile(), Collections.singletonList(e), problems); } List<String> activeProfiles = getActiveProfiles(project); MavenProjectInfo projectInfo = new MavenProjectInfo(model, null, activeProfiles); return new MavenServerResult(projectInfo, problems, unresolvedArtifacts); }
public DependencyResolutionResult resolve( DependencyResolutionRequest request ) throws DependencyResolutionException { return new DependencyResolutionResult() { public List<Dependency> getUnresolvedDependencies() { return Collections.emptyList(); } public List<Dependency> getResolvedDependencies() { return Collections.emptyList(); } public List<Exception> getResolutionErrors( Dependency dependency ) { return Collections.emptyList(); } public DependencyNode getDependencyGraph() { return new DefaultDependencyNode( (Dependency) null ); } public List<Dependency> getDependencies() { return Collections.emptyList(); } public List<Exception> getCollectionErrors() { return Collections.emptyList(); } }; }
public DependencyResolutionResult resolve( DependencyResolutionRequest request ) throws DependencyResolutionException { synchronized ( SynchronizedProjectDependenciesResolver.class ) { return super.resolve( request ); } }
@Override public void onEvent( Object event ) throws Exception { try { if ( event instanceof ExecutionEvent ) { executionEventHandler( (ExecutionEvent) event ); } else if ( event instanceof org.eclipse.aether.RepositoryEvent ) { repositoryEventHandler( (RepositoryEvent) event ); } else if ( event instanceof MavenExecutionRequest ) { executionRequestEventHandler( (MavenExecutionRequest) event ); } else if ( event instanceof MavenExecutionResult ) { executionResultEventHandler( (MavenExecutionResult) event ); } else if ( event instanceof DependencyResolutionRequest ) { dependencyResolutionRequest( (DependencyResolutionRequest) event ); } else if ( event instanceof DependencyResolutionResult ) { dependencyResolutionResult( (DependencyResolutionResult) event ); } // The following event type is available since Maven 3.3.1+ // else if ( event instanceof DefaultSettingsBuildingRequest) { // DefaultSettingsBuildingRequest r = null; // r.getGlobalSettingsFile(); // r.getGlobalSettingsSource(); // r.getSystemProperties(); // r.getUserSettingsFile(); // r.getUserSettingsSource(); // // r.setGlobalSettingsFile( globalSettingsFile ); // r.setGlobalSettingsSource( globalSettingsSource ); // r.setSystemProperties( systemProperties ); // r.setUserProperties( userProperties ); // r.setUserSettingsFile( userSettingsFile ); // r.setUserSettingsSource( userSettingsSource ); // } // The following event type is available since Maven 3.3.1+ // else if (event instanceof DefaultSettingsBuildingRequest) { // // DefaultSettingsBuildingRequest r = null; // r.getGlobalSettingsSource().getLocation() // } // The following event type is available since Maven 3.3.1+ // else if (event instanceof DefaultToolchainsBuildingRequest) { // DefaultToolchainsBuildingRequest r = null; // r.getGlobalToolchainsSource(). // } // The following event type is available since Maven 3.3.1+ // else if (event instanceof DefaultToolchainsBuildingResult) { // DefaultToolchainsBuildingResult r = null; // r.getEffectiveToolchains(); // r.getProblems(); // } else { // TODO: What kind of event we haven't considered? LOGGER.debug( "MBTP: Event {}", event.getClass().getCanonicalName() ); } } catch ( Exception e ) { LOGGER.error( "MBTP: Exception", e ); } }
private void dependencyResolutionResult( DependencyResolutionResult event ) { LOGGER.debug( "MBTP: dependencyResolutionResult() {}", event.getResolvedDependencies().size() ); }
@Override protected boolean _handle(DependencyResolutionResult result) { Xpp3Dom root = new Xpp3Dom("DependencyResolutionResult"); root.setAttribute("class", result.getClass().getName()); Xpp3Dom dependenciesElt = new Xpp3Dom("resolvedDependencies"); root.addChild(dependenciesElt); for (Dependency dependency : result.getResolvedDependencies()) { Artifact artifact = dependency.getArtifact(); if ( !includedScopes.contains(dependency.getScope())) { continue; } if (!includeSnapshots && artifact.isSnapshot()) { continue; } if(!includeReleases && !artifact.isSnapshot()) { continue; } Xpp3Dom dependencyElt = new Xpp3Dom("dependency"); dependencyElt.addChild(newElement("file", artifact.getFile().getAbsolutePath())); dependencyElt.setAttribute("name", artifact.getFile().getName()); dependencyElt.setAttribute("groupId", artifact.getGroupId()); dependencyElt.setAttribute("artifactId", artifact.getArtifactId()); dependencyElt.setAttribute("version", artifact.getVersion()); dependencyElt.setAttribute("baseVersion", artifact.getBaseVersion()); if (artifact.getClassifier() != null) { dependencyElt.setAttribute("classifier", artifact.getClassifier()); } dependencyElt.setAttribute("type", artifact.getExtension()); dependencyElt.setAttribute("id", artifact.getArtifactId()); dependencyElt.setAttribute("extension", artifact.getExtension()); dependencyElt.setAttribute("scope", dependency.getScope()); dependencyElt.setAttribute("optional", Boolean.toString(dependency.isOptional())); dependencyElt.setAttribute("snapshot", Boolean.toString(artifact.isSnapshot())); dependenciesElt.addChild(dependencyElt); } reporter.print(root); return true; }
@Nullable public DependencyResolutionResult getDependencyResolutionResult() { return myDependencyResolutionResult; }
public DependencyResolutionResult getDependencyResolutionResult() { return dependencyResolutionResult; }
@Override protected void execute(GitBranchType type, String gitBranch, String branchPattern) throws MojoExecutionException, MojoFailureException { getLog().debug("update-stage-dependencies setting up Repository session..."); DefaultRepositorySystemSession reresolveSession = new DefaultRepositorySystemSession(session); reresolveSession.setUpdatePolicy(org.eclipse.aether.repository.RepositoryPolicy.UPDATE_POLICY_ALWAYS); reresolveSession.setCache(new DefaultRepositoryCache()); LocalRepositoryManager localRepositoryManager = reresolveSession.getLocalRepositoryManager(); getLog().debug("configuring stage as the remote repository for artifact resolution requests..."); List<RemoteRepository> stageRepo = Arrays.asList(getRepository(stageDeploymentRepository)); boolean itemsPurged = false; try { DependencyResolutionResult depencencyResult = dependenciesResolver.resolve( new DefaultDependencyResolutionRequest(project, reresolveSession)); for (Dependency dependency : depencencyResult.getResolvedDependencies()) { if (!dependency.getArtifact().isSnapshot()) { // Find the artifact in the local repo, and if it came from the 'stageRepo', populate that info // as the 'repository' on the artifact. LocalArtifactResult localResult = localRepositoryManager.find(reresolveSession, new LocalArtifactRequest(dependency.getArtifact(), stageRepo, null)); // If the result has a file... and the getRepository() matched the stage repo id... if (localResult.getFile() != null && localResult.getRepository() != null) { getLog().info("Purging: " + dependency + " from remote repository: " + localResult.getRepository() + "."); File deleteTarget = new File(localRepositoryManager.getRepository().getBasedir(), localRepositoryManager.getPathForLocalArtifact(dependency.getArtifact())); if (deleteTarget.isDirectory()) { try { FileUtils.deleteDirectory(deleteTarget); } catch (IOException ioe) { getLog().warn("Failed to purge stage artifact from local repository: " + deleteTarget, ioe); } } else if (!deleteTarget.delete()) { getLog().warn("Failed to purge stage artifact from local repository: " + deleteTarget); } itemsPurged = true; } } } } catch (DependencyResolutionException dre) { throw new MojoExecutionException("Initial dependency resolution to resolve dependencies which may have been provided by the 'stage' repository failed.", dre); } if (itemsPurged) { try { getLog().info("Resolving purged dependencies..."); dependenciesResolver.resolve(new DefaultDependencyResolutionRequest(project, reresolveSession)); getLog().info("All stage dependencies purged and re-resolved."); } catch (DependencyResolutionException e) { throw new MojoExecutionException("Post-purge dependency resolution failed!", e); } } }
private Set<Artifact> getDependencies( MavenProject project, Collection<String> scopesToCollect, Collection<String> scopesToResolve, MavenSession session, boolean aggregating, Set<Artifact> projectArtifacts ) throws LifecycleExecutionException { if ( scopesToCollect == null ) { scopesToCollect = Collections.emptySet(); } if ( scopesToResolve == null ) { scopesToResolve = Collections.emptySet(); } if ( scopesToCollect.isEmpty() && scopesToResolve.isEmpty() ) { return new LinkedHashSet<Artifact>(); } scopesToCollect = new HashSet<String>( scopesToCollect ); scopesToCollect.addAll( scopesToResolve ); DependencyFilter collectionFilter = new ScopeDependencyFilter( null, negate( scopesToCollect ) ); DependencyFilter resolutionFilter = new ScopeDependencyFilter( null, negate( scopesToResolve ) ); resolutionFilter = AndDependencyFilter.newInstance( collectionFilter, resolutionFilter ); resolutionFilter = AndDependencyFilter.newInstance( resolutionFilter, new ReactorDependencyFilter( projectArtifacts ) ); DependencyResolutionResult result; try { DefaultDependencyResolutionRequest request = new DefaultDependencyResolutionRequest( project, session.getRepositorySession() ); request.setResolutionFilter( resolutionFilter ); eventSpyDispatcher.onEvent( request ); result = dependenciesResolver.resolve( request ); } catch ( DependencyResolutionException e ) { result = e.getResult(); /* * MNG-2277, the check below compensates for our bad plugin support where we ended up with aggregator * plugins that require dependency resolution although they usually run in phases of the build where project * artifacts haven't been assembled yet. The prime example of this is "mvn release:prepare". */ if ( aggregating && areAllDependenciesInReactor( session.getProjects(), result.getUnresolvedDependencies() ) ) { logger.warn( "The following dependencies could not be resolved at this point of the build" + " but seem to be part of the reactor:" ); for ( Dependency dependency : result.getUnresolvedDependencies() ) { logger.warn( "o " + dependency ); } logger.warn( "Try running the build up to the lifecycle phase \"package\"" ); } else { throw new LifecycleExecutionException( null, project, e ); } } eventSpyDispatcher.onEvent( result ); Set<Artifact> artifacts = new LinkedHashSet<Artifact>(); if ( result.getDependencyGraph() != null && !result.getDependencyGraph().getChildren().isEmpty() ) { RepositoryUtils.toArtifacts( artifacts, result.getDependencyGraph().getChildren(), Collections.singletonList( project.getArtifact().getId() ), collectionFilter ); } return artifacts; }
public MavenExecutionResult setDependencyResolutionResult( DependencyResolutionResult dependencyResolutionResult ) { this.dependencyResolutionResult = dependencyResolutionResult; return this; }
MavenExecutionResult setDependencyResolutionResult( DependencyResolutionResult result );
DependencyResolutionResult getDependencyResolutionResult();