View Javadoc
1   package org.apache.maven.plugin.coreit;
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.factory.ArtifactFactory;
24  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.resolver.ArtifactCollector;
27  import org.apache.maven.model.Dependency;
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  
32  import java.util.Collections;
33  import java.util.LinkedHashSet;
34  import java.util.List;
35  import java.util.Set;
36  
37  /**
38   * Collects user-specified artifacts. This mimics in part the Maven Assembly Plugin.
39   *
40   * @author Benjamin Bentmann
41   *
42   * @goal collect
43   */
44  public class CollectMojo
45      extends AbstractMojo
46  {
47  
48      /**
49       * The local repository.
50       *
51       * @parameter default-value="${localRepository}"
52       * @readonly
53       * @required
54       */
55      private ArtifactRepository localRepository;
56  
57      /**
58       * The remote repositories of the current Maven project.
59       *
60       * @parameter default-value="${project.remoteArtifactRepositories}"
61       * @readonly
62       * @required
63       */
64      private List remoteRepositories;
65  
66      /**
67       * The artifact collector.
68       *
69       * @component
70       */
71      private ArtifactCollector collector;
72  
73      /**
74       * The artifact factory.
75       *
76       * @component
77       */
78      private ArtifactFactory factory;
79  
80      /**
81       * The metadata source.
82       *
83       * @component
84       */
85      private ArtifactMetadataSource metadataSource;
86  
87      /**
88       * The dependencies to resolve.
89       *
90       * @parameter
91       */
92      private Dependency[] dependencies;
93  
94      /**
95       * Runs this mojo.
96       *
97       * @throws MojoFailureException If the artifact file has not been set.
98       */
99      public void execute()
100         throws MojoExecutionException, MojoFailureException
101     {
102         getLog().info( "[MAVEN-CORE-IT-LOG] Collecting artifacts" );
103 
104         try
105         {
106             Artifact origin = factory.createArtifact( "it", "it", "0.1", null, "pom" );
107 
108             Set artifacts = new LinkedHashSet();
109 
110             if ( dependencies != null )
111             {
112                 for ( Dependency dependency : dependencies )
113                 {
114                     Artifact artifact =
115                         factory.createArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(),
116                                                               dependency.getVersion(), dependency.getType(),
117                                                               dependency.getClassifier() );
118 
119                     artifacts.add( artifact );
120 
121                     getLog().info( "[MAVEN-CORE-IT-LOG] Collecting " + artifact.getId() );
122                 }
123             }
124 
125             collector.collect( artifacts, origin, localRepository, remoteRepositories, metadataSource, null,
126                                Collections.EMPTY_LIST );
127         }
128         catch ( Exception e )
129         {
130             throw new MojoExecutionException( "Failed to collect artifacts: " + e.getMessage(), e );
131         }
132     }
133 
134 }