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