View Javadoc

1   package org.apache.maven.plugin.dependency;
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.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.factory.ArtifactFactory;
29  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
32  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
33  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
34  import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
35  import org.apache.maven.artifact.resolver.ArtifactResolver;
36  import org.apache.maven.plugin.AbstractMojo;
37  import org.apache.maven.plugin.MojoExecutionException;
38  import org.apache.maven.plugin.MojoFailureException;
39  import org.codehaus.plexus.util.StringUtils;
40  
41  /**
42   * Downloads a single artifact transitively from a specified remote repository.
43   *
44   * @goal get
45   * @requiresProject false
46   *
47   */
48  public class GetMojo
49      extends AbstractMojo
50  {
51  
52      /**
53       * @component
54       * @readonly
55       */
56      private ArtifactFactory artifactFactory;
57  
58      /**
59       * @component
60       * @readonly
61       */
62      private ArtifactResolver artifactResolver;
63  
64      /**
65       * @component
66       * @readonly
67       */
68      private ArtifactRepositoryFactory artifactRepositoryFactory;
69  
70      /**
71       * @component roleHint="default"
72       */
73      private ArtifactRepositoryLayout repositoryLayout;
74  
75      /**
76       * @component
77       * @readonly
78       */
79      private ArtifactMetadataSource source;
80  
81      /**
82       *
83       * @parameter expression="${localRepository}"
84       * @readonly
85       */
86      private ArtifactRepository localRepository;
87  
88      /**
89       * The groupId of the artifact to download
90       * @parameter expression="${groupId}"
91       */
92      private String groupId;
93  
94      /**
95       * The artifactId of the artifact to download
96       * @parameter expression="${artifactId}"
97       */
98      private String artifactId;
99  
100     /**
101      * The version of the artifact to download
102      * @parameter expression="${version}"
103      */
104     private String version;
105 
106     /**
107      * The classifier of the artifact to download. Ignored if {@link #artifact} is used.
108      * @parameter expression="${classifier}"
109      * @since 2.3
110      */
111     private String classifier;
112 
113     /**
114      * The packaging of the artifact to download
115      * @parameter expression="${packaging}" default-value="jar"
116      */
117     private String packaging = "jar";
118 
119     /**
120      * The id of the repository from which we'll download the artifact
121      * @parameter expression="${repoId}" default-value="temp"
122      */
123     private String repositoryId = "temp";
124 
125     /**
126      * The url of the repository from which we'll download the artifact
127      * @parameter expression="${repoUrl}"
128      * @required
129      */
130     private String repositoryUrl;
131 
132     /**
133      * @parameter expression="${remoteRepositories}"
134      * @readonly
135      */
136     private String remoteRepositories;
137 
138     /**
139      * A string of the form groupId:artifactId:version[:packaging][:classifier].
140      * @parameter expression="${artifact}"
141      */
142     private String artifact;
143 
144     /**
145      *
146      * @parameter expression="${project.remoteArtifactRepositories}"
147      * @required
148      * @readonly
149      */
150     private List pomRemoteRepositories;
151 
152     /**
153      * Download transitively, retrieving the specified artifact and all of its dependencies.
154      * @parameter expression="{$transitive}" default-value=true
155      */
156     private boolean transitive = true;
157 
158     public void execute()
159         throws MojoExecutionException, MojoFailureException
160     {
161 
162         if ( artifactId == null && artifact == null )
163         {
164             throw new MojoFailureException( "You must specify an artifact, "
165                 + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0" );
166         }
167         if ( artifactId == null )
168         {
169             String[] tokens = StringUtils.split( artifact, ":" );
170             if ( tokens.length < 3 && tokens.length > 5 )
171             {
172                 throw new MojoFailureException(
173                     "Invalid artifact, you must specify groupId:artifactId:version[:packaging][:classifier] "
174                         + artifact );
175             }
176             groupId = tokens[0];
177             artifactId = tokens[1];
178             version = tokens[2];
179             if ( tokens.length >= 4 )
180             {
181                 packaging = tokens[3];
182             }
183             if ( tokens.length == 5 )
184             {
185                 classifier = tokens[4];
186             }
187             else
188             {
189                 classifier = null;
190             }
191         }
192 
193         Artifact toDownload = classifier == null
194             ? artifactFactory.createBuildArtifact( groupId, artifactId, version, packaging )
195             : artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier );
196         Artifact dummyOriginatingArtifact =
197             artifactFactory.createBuildArtifact( "org.apache.maven.plugins", "maven-downloader-plugin", "1.0", "jar" );
198 
199         ArtifactRepositoryPolicy always =
200             new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
201                                           ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
202         ArtifactRepository remoteRepo =
203             artifactRepositoryFactory.createArtifactRepository( repositoryId, repositoryUrl, repositoryLayout, always,
204                                                                 always );
205 
206         if ( pomRemoteRepositories == null )
207         {
208             pomRemoteRepositories = new ArrayList();
209         }
210 
211         List repoList = new ArrayList( pomRemoteRepositories );
212         if ( remoteRepositories != null )
213         {
214             // TODO: remote repositories as Strings?
215             repoList.addAll( Arrays.asList( StringUtils.split( remoteRepositories, "," ) ) );
216         }
217 
218         repoList.add( remoteRepo );
219 
220         try
221         {
222             if ( transitive )
223             {
224                 artifactResolver.resolveTransitively( Collections.singleton( toDownload ), dummyOriginatingArtifact,
225                                                       repoList, localRepository, source );
226             }
227             else
228             {
229                 artifactResolver.resolve( toDownload, repoList, localRepository );
230             }
231 
232         }
233         catch ( AbstractArtifactResolutionException e )
234         {
235             throw new MojoExecutionException( "Couldn't download artifact: " + e.getMessage(), e );
236         }
237     }
238 }