View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.archive.phase;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.Collections;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
31  import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
32  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
33  import org.apache.maven.plugins.assembly.archive.task.AddDependencySetsTask;
34  import org.apache.maven.plugins.assembly.artifact.DependencyResolutionException;
35  import org.apache.maven.plugins.assembly.artifact.DependencyResolver;
36  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
37  import org.apache.maven.plugins.assembly.model.Assembly;
38  import org.apache.maven.plugins.assembly.model.DependencySet;
39  import org.apache.maven.project.ProjectBuilder;
40  import org.codehaus.plexus.archiver.Archiver;
41  
42  import static java.util.Objects.requireNonNull;
43  
44  /**
45   * Handles the top-level <dependencySets/> section of the assembly descriptor.
46   *
47   *
48   */
49  @Singleton
50  @Named("dependency-sets")
51  public class DependencySetAssemblyPhase implements AssemblyArchiverPhase, PhaseOrder {
52      private final ProjectBuilder projectBuilder;
53  
54      private final DependencyResolver dependencyResolver;
55  
56      /**
57       * Injected ctor.
58       */
59      @Inject
60      public DependencySetAssemblyPhase(
61              final ProjectBuilder projectBuilder, final DependencyResolver dependencyResolver) {
62          this.projectBuilder = requireNonNull(projectBuilder);
63          this.dependencyResolver = requireNonNull(dependencyResolver);
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public void execute(
71              final Assembly assembly, final Archiver archiver, final AssemblerConfigurationSource configSource)
72              throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException,
73                      DependencyResolutionException {
74  
75          Map<DependencySet, Set<Artifact>> resolved =
76                  dependencyResolver.resolveDependencySets(assembly, configSource, assembly.getDependencySets());
77          for (Map.Entry<DependencySet, Set<Artifact>> dependencySetSetEntry : resolved.entrySet()) {
78              final AddDependencySetsTask task = new AddDependencySetsTask(
79                      Collections.singletonList(dependencySetSetEntry.getKey()),
80                      dependencySetSetEntry.getValue(),
81                      configSource.getProject(),
82                      projectBuilder);
83  
84              task.execute(archiver, configSource);
85          }
86      }
87  
88      @Override
89      public int order() {
90          // CHECKSTYLE_OFF: MagicNumber
91          return 40;
92          // CHECKSTYLE_ON: MagicNumber
93      }
94  }