-
Notifications
You must be signed in to change notification settings - Fork 146
[MRESOLVER-262] Provide contextual data in trace during collect #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
82c08b9
600468f
2dde984
4a6631c
bf96c99
d5a3847
e4d67a1
b4c56d8
bbbc03c
2288823
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package org.eclipse.aether.collection; | ||
|
|
||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.eclipse.aether.graph.Dependency; | ||
| import org.eclipse.aether.graph.DependencyNode; | ||
|
|
||
| /** | ||
| * A trace data object revealing collect step (while executing {@link CollectRequest}. | ||
| * | ||
| * @see org.eclipse.aether.RequestTrace | ||
| * @since 1.8.1 | ||
| */ | ||
| public interface CollectStepData | ||
| { | ||
| /** | ||
| * Returns the context of collection. Never {@code null}. | ||
| */ | ||
| String getContext(); | ||
|
|
||
| /** | ||
| * Returns the path of dependency nodes that led collector to current node returned by {@link #getNode()}. | ||
| * Never {@code null}. | ||
| */ | ||
| List<DependencyNode> getPath(); | ||
|
|
||
| /** | ||
| * Returns the current node being collected. Never {@code null}. | ||
| */ | ||
| Dependency getNode(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package org.apache.maven.resolver.examples; | ||
|
|
||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import org.apache.maven.resolver.examples.util.Booter; | ||
| import org.apache.maven.resolver.examples.util.ReverseTreeRepositoryListener; | ||
| import org.eclipse.aether.DefaultRepositorySystemSession; | ||
| import org.eclipse.aether.RepositorySystem; | ||
| import org.eclipse.aether.artifact.Artifact; | ||
| import org.eclipse.aether.artifact.DefaultArtifact; | ||
| import org.eclipse.aether.collection.CollectRequest; | ||
| import org.eclipse.aether.resolution.ArtifactDescriptorRequest; | ||
| import org.eclipse.aether.resolution.ArtifactDescriptorResult; | ||
| import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; | ||
| import org.eclipse.aether.util.graph.transformer.ConflictResolver; | ||
| import org.eclipse.aether.util.listener.ChainedRepositoryListener; | ||
|
|
||
| /** | ||
| * Example of building reverse dependency tree using custom {@link ReverseTreeRepositoryListener}. | ||
| */ | ||
| public class ReverseDependencyTree | ||
| { | ||
|
|
||
| /** | ||
| * Main. | ||
| * @param args | ||
| * @throws Exception | ||
| */ | ||
| public static void main( String[] args ) | ||
| throws Exception | ||
| { | ||
| System.out.println( "------------------------------------------------------------" ); | ||
| System.out.println( ReverseDependencyTree.class.getSimpleName() ); | ||
|
|
||
| RepositorySystem system = Booter.newRepositorySystem( Booter.selectFactory( args ) ); | ||
|
|
||
| DefaultRepositorySystemSession session = Booter.newRepositorySystemSession( system ); | ||
|
|
||
| // install the listener into session | ||
| session.setRepositoryListener( new ChainedRepositoryListener( session.getRepositoryListener(), | ||
| new ReverseTreeRepositoryListener() ) ); | ||
|
|
||
| session.setConfigProperty( ConflictResolver.CONFIG_PROP_VERBOSE, true ); | ||
| session.setConfigProperty( DependencyManagerUtils.CONFIG_PROP_VERBOSE, true ); | ||
|
|
||
| Artifact artifact = new DefaultArtifact( "org.apache.maven:maven-resolver-provider:3.6.1" ); | ||
|
|
||
| ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest(); | ||
| descriptorRequest.setArtifact( artifact ); | ||
| descriptorRequest.setRepositories( Booter.newRepositories( system, session ) ); | ||
| ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor( session, descriptorRequest ); | ||
|
|
||
| CollectRequest collectRequest = new CollectRequest(); | ||
| collectRequest.setRequestContext( "demo" ); | ||
| collectRequest.setRootArtifact( descriptorResult.getArtifact() ); | ||
| collectRequest.setDependencies( descriptorResult.getDependencies() ); | ||
| collectRequest.setManagedDependencies( descriptorResult.getManagedDependencies() ); | ||
| collectRequest.setRepositories( descriptorRequest.getRepositories() ); | ||
|
|
||
| system.collectDependencies( session, collectRequest ); | ||
|
|
||
| // in this demo we are not interested in collect result, | ||
| // as all the "demo work" is done by installed ReverseTreeRepositoryListener | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package org.apache.maven.resolver.examples.util; | ||
|
|
||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ListIterator; | ||
| import java.util.Objects; | ||
|
|
||
| import org.eclipse.aether.AbstractRepositoryListener; | ||
| import org.eclipse.aether.RepositoryEvent; | ||
| import org.eclipse.aether.RequestTrace; | ||
| import org.eclipse.aether.artifact.Artifact; | ||
| import org.eclipse.aether.collection.CollectStepData; | ||
| import org.eclipse.aether.graph.Dependency; | ||
| import org.eclipse.aether.graph.DependencyNode; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * A demo class building reverse tree using {@link CollectStepData} trace data provided in {@link RepositoryEvent} | ||
| * events fired during collection. | ||
| */ | ||
| public class ReverseTreeRepositoryListener | ||
| extends AbstractRepositoryListener | ||
| { | ||
| private static final String EOL = System.lineSeparator(); | ||
|
|
||
| @Override | ||
| public void artifactResolved( RepositoryEvent event ) | ||
| { | ||
| requireNonNull( event, "event cannot be null" ); | ||
|
|
||
| RequestTrace trace = event.getTrace(); | ||
| CollectStepData collectStepTrace = null; | ||
| while ( trace != null ) | ||
| { | ||
| if ( trace.getData() instanceof CollectStepData ) | ||
| { | ||
| collectStepTrace = (CollectStepData) trace.getData(); | ||
| break; | ||
| } | ||
| trace = trace.getParent(); | ||
| } | ||
|
|
||
| if ( collectStepTrace == null ) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| Artifact resolvedArtifact = event.getArtifact(); | ||
| Artifact nodeArtifact = collectStepTrace.getNode().getArtifact(); | ||
|
|
||
| if ( isInScope( resolvedArtifact, nodeArtifact ) ) | ||
| { | ||
| Dependency node = collectStepTrace.getNode(); | ||
| String trackingData = node + " (" + collectStepTrace.getContext() + ")" + EOL; | ||
| String indent = ""; | ||
| ListIterator<DependencyNode> iter = collectStepTrace.getPath() | ||
| .listIterator( collectStepTrace.getPath().size() ); | ||
| while ( iter.hasPrevious() ) | ||
| { | ||
| DependencyNode curr = iter.previous(); | ||
| indent += " "; | ||
| trackingData += indent + curr + " (" + collectStepTrace.getContext() + ")" + EOL; | ||
| } | ||
| try | ||
| { | ||
| Path trackingDir = resolvedArtifact.getFile().getParentFile().toPath().resolve( ".tracking" ); | ||
| Files.createDirectories( trackingDir ); | ||
| Path trackingFile = trackingDir.resolve( collectStepTrace.getPath().get( 0 ) | ||
| .getArtifact().toString().replace( ":", "_" ) ); | ||
| Files.write( trackingFile, trackingData.getBytes( StandardCharsets.UTF_8 ) ); | ||
| System.out.println( trackingData ); | ||
| } | ||
| catch ( IOException e ) | ||
| { | ||
| throw new UncheckedIOException( e ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The event "artifact resolved" if fired WHENEVER an artifact is resolved, BUT it happens also when an artifact | ||
| * descriptor (model, the POM) is being built, and parent (and parent of parent...) is being asked for. Hence, this | ||
| * method "filters" out in WHICH artifact are we interested in, but it intentionally neglects extension as | ||
| * ArtifactDescriptorReader modifies extension to "pom" during collect. So all we have to rely on is GAV only. | ||
| */ | ||
| private boolean isInScope( Artifact artifact, Artifact nodeArtifact ) | ||
| { | ||
| return Objects.equals( artifact.getGroupId(), nodeArtifact.getGroupId() ) | ||
| && Objects.equals( artifact.getArtifactId(), nodeArtifact.getArtifactId() ) | ||
| && Objects.equals( artifact.getVersion(), nodeArtifact.getVersion() ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package org.eclipse.aether.internal.impl.collect; | ||
|
|
||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.eclipse.aether.collection.CollectStepData; | ||
| import org.eclipse.aether.graph.Dependency; | ||
| import org.eclipse.aether.graph.DependencyNode; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * Trace objects for dependency collection. | ||
| * | ||
| * @since 1.8.1 | ||
| */ | ||
| public final class CollectStepDataImpl implements CollectStepData | ||
| { | ||
| private final String context; | ||
|
|
||
| private final List<DependencyNode> path; | ||
|
|
||
| private final Dependency node; | ||
|
|
||
| public CollectStepDataImpl( final String context, final List<DependencyNode> path, final Dependency node ) | ||
| { | ||
| this.context = requireNonNull( context ); | ||
| this.path = requireNonNull( path ); | ||
| this.node = requireNonNull( node ); | ||
| } | ||
|
|
||
| @Override | ||
| public String getContext() | ||
| { | ||
| return context; | ||
| } | ||
|
|
||
| @Override | ||
| public List<DependencyNode> getPath() | ||
| { | ||
| return path; | ||
| } | ||
|
|
||
| @Override | ||
| public Dependency getNode() | ||
| { | ||
| return node; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -290,6 +290,33 @@ public final CollectResult collectDependencies( RepositorySystemSession session, | |
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Creates child {@link RequestTrace} instance from passed in {@link RequestTrace} and parameters by creating | ||
| * {@link CollectStepDataImpl} instance out of passed in data. Caller must ensure that passed in parameters are | ||
| * NOT affected by threading (or that there is no multi threading involved). In other words, the passed in values | ||
| * should be immutable. | ||
| * | ||
| * @param trace The current trace instance. | ||
| * @param context The context from {@link CollectRequest#getRequestContext()}, never {@code null}. | ||
| * @param path List representing the path of dependency nodes, never {@code null}. Caller must ensure, that this | ||
| * list does not change during the lifetime of the requested {@link RequestTrace} instance. If it may | ||
| * change, simplest is to pass here a copy of used list. | ||
| * @param node Currently collected node, that collector came by following the passed in path. | ||
| * @return A child request trance instance, never {@code null}. | ||
| */ | ||
| protected RequestTrace collectStepTrace( RequestTrace trace, String context, List<DependencyNode> path, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am least certain about this method: it does "too little" IMHO. For example, it could just take a copy the passed in "path" parameter list, but OTOH, am unsure how big penalty (heap, GC wise) would this introduce. Hence, am leaving this decision (to copy or not copy the list) to subclass, as subclass should know does it requires a copy or not, or in other words, is it multi threaded or not. DF for sure does not need to copy it (reason more why not to copy it here). BF once goes to "multi threaded POM download" it may, but as I see, each thread will have it's own context, and context carries the list, so again, I see no need for list copy. |
||
| Dependency node ) | ||
| { | ||
| return RequestTrace.newChild( | ||
| trace, | ||
| new CollectStepDataImpl( | ||
| context, | ||
| path, | ||
| node | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @SuppressWarnings( "checkstyle:parameternumber" ) | ||
| protected abstract void doCollectDependencies( RepositorySystemSession session, RequestTrace trace, DataPool pool, | ||
| DefaultDependencyCollectionContext context, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Classifier and type don't matter?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it does not, as one "collect" step may produce several resolved events, as parent POMs are being resolver, and per definition parent POM GAV must be different (otherwise you have a cycle). Also, this is "demo snippet", and example here only wants to be clear that the event fired does not correspond 1:1 to "collect step" (again, several events are fired per one collect step).
The trace request is added ONLY when collecting, and these events are fired as side effects of artifact descriptor read requests, and those are going to model builder, that in turn resolve (so use cached or fetches from remote) the POM and all POMs (like parents) needed to build artifact descriptor.... Hence, unrelated to actual dependency, artifact descriptor reader effectively goes for their POMs. Also, if you think about it, any classified artifact and main one too uses same one POM.