View Javadoc
1   package org.apache.maven.plugins.assembly.archive.phase;
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 org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
28  import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
29  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
30  import org.apache.maven.plugins.assembly.archive.task.AddDependencySetsTask;
31  import org.apache.maven.plugins.assembly.artifact.DependencyResolutionException;
32  import org.apache.maven.plugins.assembly.artifact.DependencyResolver;
33  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
34  import org.apache.maven.plugins.assembly.model.Assembly;
35  import org.apache.maven.plugins.assembly.model.DependencySet;
36  import org.apache.maven.project.ProjectBuilder;
37  import org.codehaus.plexus.archiver.Archiver;
38  
39  import java.util.Collections;
40  import java.util.Map;
41  import java.util.Set;
42  
43  import static java.util.Objects.requireNonNull;
44  
45  /**
46   * Handles the top-level <dependencySets/> section of the assembly descriptor.
47   *
48   *
49   */
50  @Singleton
51  @Named( "dependency-sets" )
52  public class DependencySetAssemblyPhase implements AssemblyArchiverPhase, PhaseOrder
53  {
54      private final ProjectBuilder projectBuilder;
55  
56      private final DependencyResolver dependencyResolver;
57  
58      /**
59       * Injected ctor.
60       */
61      @Inject
62      public DependencySetAssemblyPhase( final ProjectBuilder projectBuilder,
63                                         final DependencyResolver dependencyResolver )
64      {
65          this.projectBuilder = requireNonNull( projectBuilder );
66          this.dependencyResolver = requireNonNull( dependencyResolver );
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public void execute( final Assembly assembly, final Archiver archiver,
74                           final AssemblerConfigurationSource configSource )
75          throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException,
76          DependencyResolutionException
77      {
78  
79          Map<DependencySet, Set<Artifact>> resolved =
80              dependencyResolver.resolveDependencySets( assembly, configSource, assembly.getDependencySets() );
81          for ( Map.Entry<DependencySet, Set<Artifact>> dependencySetSetEntry : resolved.entrySet() )
82          {
83              final AddDependencySetsTask task =
84                  new AddDependencySetsTask( Collections.singletonList( dependencySetSetEntry.getKey() ),
85                          dependencySetSetEntry.getValue(), configSource.getProject(),
86                          projectBuilder );
87  
88              task.execute( archiver, configSource );
89          }
90      }
91  
92      @Override
93      public int order()
94      {
95          // CHECKSTYLE_OFF: MagicNumber
96          return 40;
97          // CHECKSTYLE_ON: MagicNumber
98      }
99  }