View Javadoc
1   package org.apache.maven.cli.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Set;
27  
28  import javax.inject.Inject;
29  import javax.inject.Named;
30  
31  import org.apache.maven.RepositoryUtils;
32  import org.apache.maven.cli.internal.extension.model.CoreExtension;
33  import org.apache.maven.execution.MavenExecutionRequest;
34  import org.apache.maven.extension.internal.CoreExtensionEntry;
35  import org.apache.maven.internal.aether.DefaultRepositorySystemSessionFactory;
36  import org.apache.maven.model.Plugin;
37  import org.apache.maven.plugin.PluginResolutionException;
38  import org.apache.maven.plugin.internal.DefaultPluginDependenciesResolver;
39  import org.codehaus.plexus.DefaultPlexusContainer;
40  import org.codehaus.plexus.PlexusContainer;
41  import org.codehaus.plexus.classworlds.ClassWorld;
42  import org.codehaus.plexus.classworlds.realm.ClassRealm;
43  import org.codehaus.plexus.logging.Logger;
44  import org.eclipse.aether.RepositorySystemSession;
45  import org.eclipse.aether.artifact.Artifact;
46  import org.eclipse.aether.graph.DependencyFilter;
47  import org.eclipse.aether.graph.DependencyNode;
48  import org.eclipse.aether.repository.RemoteRepository;
49  import org.eclipse.aether.util.filter.ExclusionsDependencyFilter;
50  import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator;
51  
52  @Named
53  public class BootstrapCoreExtensionManager
54  {
55      private final Logger log;
56  
57      private final DefaultPluginDependenciesResolver pluginDependenciesResolver;
58  
59      private final DefaultRepositorySystemSessionFactory repositorySystemSessionFactory;
60  
61      private final ClassWorld classWorld;
62  
63      private final ClassRealm parentRealm;
64  
65      @Inject
66      public BootstrapCoreExtensionManager( Logger log, DefaultPluginDependenciesResolver pluginDependenciesResolver,
67                                            DefaultRepositorySystemSessionFactory repositorySystemSessionFactory,
68                                            PlexusContainer container )
69      {
70          this.log = log;
71          this.pluginDependenciesResolver = pluginDependenciesResolver;
72          this.repositorySystemSessionFactory = repositorySystemSessionFactory;
73          this.classWorld = ( (DefaultPlexusContainer) container ).getClassWorld();
74          this.parentRealm = container.getContainerRealm();
75      }
76  
77      public List<CoreExtensionEntry> loadCoreExtensions( MavenExecutionRequest request, Set<String> providedArtifacts,
78                                                          List<CoreExtension> extensions )
79          throws Exception
80      {
81          RepositorySystemSession repoSession = repositorySystemSessionFactory.newRepositorySession( request );
82          List<RemoteRepository> repositories = RepositoryUtils.toRepos( request.getPluginArtifactRepositories() );
83  
84          return resolveCoreExtensions( repoSession, repositories, providedArtifacts, extensions );
85      }
86  
87      private List<CoreExtensionEntry> resolveCoreExtensions( RepositorySystemSession repoSession,
88                                                              List<RemoteRepository> repositories,
89                                                              Set<String> providedArtifacts,
90                                                              List<CoreExtension> configuration )
91          throws Exception
92      {
93          List<CoreExtensionEntry> extensions = new ArrayList<>();
94  
95          DependencyFilter dependencyFilter = new ExclusionsDependencyFilter( providedArtifacts );
96  
97          for ( CoreExtension extension : configuration )
98          {
99              List<Artifact> artifacts = resolveExtension( extension, repoSession, repositories, dependencyFilter );
100             if ( !artifacts.isEmpty() )
101             {
102                 extensions.add( createExtension( extension, artifacts ) );
103             }
104         }
105 
106         return Collections.unmodifiableList( extensions );
107     }
108 
109     private CoreExtensionEntry createExtension( CoreExtension extension, List<Artifact> artifacts )
110         throws Exception
111     {
112         String realmId =
113             "coreExtension>" + extension.getGroupId() + ":" + extension.getArtifactId() + ":" + extension.getVersion();
114         ClassRealm realm = classWorld.newRealm( realmId, null );
115         log.debug( "Populating class realm " + realm.getId() );
116         realm.setParentRealm( parentRealm );
117         for ( Artifact artifact : artifacts )
118         {
119             File file = artifact.getFile();
120             log.debug( "  Included " + file );
121             realm.addURL( file.toURI().toURL() );
122         }
123         return CoreExtensionEntry.discoverFrom( realm, Collections.singleton( artifacts.get( 0 ).getFile() ) );
124     }
125 
126     private List<Artifact> resolveExtension( CoreExtension extension, RepositorySystemSession repoSession,
127                                              List<RemoteRepository> repositories, DependencyFilter dependencyFilter )
128         throws PluginResolutionException
129     {
130         Plugin plugin = new Plugin();
131         plugin.setGroupId( extension.getGroupId() );
132         plugin.setArtifactId( extension.getArtifactId() );
133         plugin.setVersion( extension.getVersion() );
134 
135         DependencyNode root =
136             pluginDependenciesResolver.resolveCoreExtension( plugin, dependencyFilter, repositories, repoSession );
137         PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
138         root.accept( nlg );
139         List<Artifact> artifacts = nlg.getArtifacts( false );
140 
141         return artifacts;
142     }
143 }