View Javadoc
1   package org.apache.maven.plugins.assembly.artifact;
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 javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
33  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
34  import org.apache.maven.plugins.assembly.archive.phase.ModuleSetAssemblyPhase;
35  import org.apache.maven.plugins.assembly.model.Assembly;
36  import org.apache.maven.plugins.assembly.model.DependencySet;
37  import org.apache.maven.plugins.assembly.model.ModuleBinaries;
38  import org.apache.maven.plugins.assembly.model.ModuleSet;
39  import org.apache.maven.project.MavenProject;
40  import org.apache.maven.repository.RepositorySystem;
41  import org.codehaus.plexus.util.StringUtils;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import static java.util.Objects.requireNonNull;
46  
47  /**
48   * @author jdcasey
49   *
50   */
51  @Singleton
52  @Named
53  public class DefaultDependencyResolver implements DependencyResolver
54  {
55      private static final Logger LOGGER = LoggerFactory.getLogger( DefaultDependencyResolver.class );
56  
57      private final RepositorySystem resolver;
58  
59      @Inject
60      public DefaultDependencyResolver( RepositorySystem resolver )
61      {
62          this.resolver = requireNonNull( resolver );
63      }
64  
65      @Override
66      public Map<DependencySet, Set<Artifact>> resolveDependencySets( final Assembly assembly, ModuleSet moduleSet,
67                                                                      final AssemblerConfigurationSource configSource,
68                                                                      List<DependencySet> dependencySets )
69          throws DependencyResolutionException
70      {
71          Map<DependencySet, Set<Artifact>> result = new LinkedHashMap<>();
72  
73          for ( DependencySet dependencySet : dependencySets )
74          {
75  
76              final MavenProject currentProject = configSource.getProject();
77  
78              final ResolutionManagementInfo info = new ResolutionManagementInfo();
79              updateDependencySetResolutionRequirements( dependencySet, info, currentProject );
80              updateModuleSetResolutionRequirements( moduleSet, dependencySet, info, configSource );
81  
82              result.put( dependencySet, info.getArtifacts() );
83  
84          }
85          return result;
86      }
87  
88      @Override
89      public Map<DependencySet, Set<Artifact>> resolveDependencySets( final Assembly assembly,
90                                                                      final AssemblerConfigurationSource configSource,
91                                                                      List<DependencySet> dependencySets )
92          throws DependencyResolutionException
93      {
94          Map<DependencySet, Set<Artifact>> result = new LinkedHashMap<>();
95  
96          for ( DependencySet dependencySet : dependencySets )
97          {
98  
99              final MavenProject currentProject = configSource.getProject();
100 
101             final ResolutionManagementInfo info = new ResolutionManagementInfo();
102             updateDependencySetResolutionRequirements( dependencySet, info, currentProject );
103 
104             result.put( dependencySet, info.getArtifacts() );
105 
106         }
107         return result;
108     }
109 
110     void updateModuleSetResolutionRequirements( ModuleSet set, DependencySet dependencySet,
111                                                 final ResolutionManagementInfo requirements,
112                                                 final AssemblerConfigurationSource configSource )
113         throws DependencyResolutionException
114     {
115         final ModuleBinaries binaries = set.getBinaries();
116         if ( binaries != null )
117         {
118             Set<MavenProject> projects;
119             try
120             {
121                 projects = ModuleSetAssemblyPhase.getModuleProjects( set, configSource, LOGGER );
122             }
123             catch ( final ArchiveCreationException e )
124             {
125                 throw new DependencyResolutionException( "Error determining project-set for moduleSet with binaries.",
126                                                          e );
127             }
128 
129             for ( final MavenProject p : projects )
130             {
131                 if ( p.getArtifact() == null )
132                 {
133                     // TODO: such a call in MavenMetadataSource too - packaging not really the intention of type
134                     final Artifact artifact =
135                         resolver.createArtifact( p.getGroupId(), p.getArtifactId(), p.getVersion(), p.getPackaging() );
136                     p.setArtifact( artifact );
137                 }
138             }
139 
140             if ( binaries.isIncludeDependencies() )
141             {
142                 updateDependencySetResolutionRequirements( dependencySet, requirements,
143                                                            projects.toArray( new MavenProject[0] ) );
144             }
145         }
146     }
147 
148     void updateDependencySetResolutionRequirements( final DependencySet set,
149                                                     final ResolutionManagementInfo requirements,
150                                                     final MavenProject... projects )
151         throws DependencyResolutionException
152     {
153         for ( final MavenProject project : projects )
154         {
155             if ( project == null )
156             {
157                 continue;
158             }
159 
160             Set<Artifact> dependencyArtifacts = null;
161             if ( set.isUseTransitiveDependencies() )
162             {
163                 dependencyArtifacts = project.getArtifacts();
164             }
165             else
166             {
167                 dependencyArtifacts = project.getDependencyArtifacts();
168             }
169 
170             requirements.addArtifacts( dependencyArtifacts );
171             LOGGER.debug( "Dependencies for project: " + project.getId() + " are:\n" + StringUtils.join(
172                 dependencyArtifacts.iterator(), "\n" ) );
173         }
174     }
175 
176 }