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 org.apache.commons.io.FileUtils;
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.artifact.factory.ArtifactFactory;
25  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
28  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
29  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
30  import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
31  import org.apache.maven.artifact.resolver.ArtifactResolver;
32  import org.apache.maven.plugin.AbstractMojo;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.MojoFailureException;
35  import org.apache.maven.plugins.annotations.Component;
36  import org.apache.maven.plugins.annotations.Mojo;
37  import org.apache.maven.plugins.annotations.Parameter;
38  import org.codehaus.plexus.util.StringUtils;
39  
40  import java.io.File;
41  import java.io.IOException;
42  import java.util.ArrayList;
43  import java.util.Arrays;
44  import java.util.Collections;
45  import java.util.List;
46  import java.util.Map;
47  import java.util.regex.Matcher;
48  import java.util.regex.Pattern;
49  
50  /**
51   * Downloads a single artifact transitively from the specified remote repositories. Caveat: will always check the
52   * central repository defined in the super pom. You could use a mirror entry in your settings.xml
53   */
54  @Mojo( name = "get", requiresProject = false, threadSafe = true )
55  public class GetMojo
56      extends AbstractMojo
57  {
58      private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.*)::(.+)" );
59  
60      /**
61       *
62       */
63      @Component
64      private ArtifactFactory artifactFactory;
65  
66      /**
67       *
68       */
69      @Component
70      private ArtifactResolver artifactResolver;
71  
72      /**
73       *
74       */
75      @Component
76      private ArtifactRepositoryFactory artifactRepositoryFactory;
77  
78      /**
79       * Map that contains the layouts.
80       */
81      @Component( role = ArtifactRepositoryLayout.class )
82      private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
83  
84      /**
85       *
86       */
87      @Component
88      private ArtifactMetadataSource source;
89  
90      /**
91       *
92       */
93      @Parameter( defaultValue = "${localRepository}", readonly = true )
94      private ArtifactRepository localRepository;
95  
96      /**
97       * The groupId of the artifact to download. Ignored if {@link #artifact} is used.
98       */
99      @Parameter( property = "groupId" )
100     private String groupId;
101 
102     /**
103      * The artifactId of the artifact to download. Ignored if {@link #artifact} is used.
104      */
105     @Parameter( property = "artifactId" )
106     private String artifactId;
107 
108     /**
109      * The version of the artifact to download. Ignored if {@link #artifact} is used.
110      */
111     @Parameter( property = "version" )
112     private String version;
113 
114     /**
115      * The classifier of the artifact to download. Ignored if {@link #artifact} is used.
116      *
117      * @since 2.3
118      */
119     @Parameter( property = "classifier" )
120     private String classifier;
121 
122     /**
123      * The packaging of the artifact to download. Ignored if {@link #artifact} is used.
124      */
125     @Parameter( property = "packaging", defaultValue = "jar" )
126     private String packaging = "jar";
127 
128     /**
129      * The id of the repository from which we'll download the artifact
130      *
131      * @deprecated Use remoteRepositories
132      */
133     @Parameter( property = "repoId", defaultValue = "temp" )
134     private String repositoryId = "temp";
135 
136     /**
137      * The url of the repository from which we'll download the artifact. DEPRECATED Use remoteRepositories
138      *
139      * @deprecated Use remoteRepositories
140      */
141     @Parameter( property = "repoUrl" )
142     private String repositoryUrl;
143 
144     /**
145      * Repositories in the format id::[layout]::url or just url, separated by comma.
146      * ie. central::default::http://repo1.maven.apache.org/maven2,myrepo::::http://repo.acme.com,http://repo.acme2.com
147      */
148     @Parameter( property = "remoteRepositories" )
149     private String remoteRepositories;
150 
151     /**
152      * A string of the form groupId:artifactId:version[:packaging][:classifier].
153      */
154     @Parameter( property = "artifact" )
155     private String artifact;
156 
157     /**
158      * The destination file or directory to copy the artifact to, if other than the local repository
159      *
160      * @since 2.4
161      */
162     @Parameter( property = "dest" )
163     private String destination;
164 
165     /**
166      *
167      */
168     @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
169     private List<ArtifactRepository> pomRemoteRepositories;
170 
171     /**
172      * Download transitively, retrieving the specified artifact and all of its dependencies.
173      */
174     @Parameter( property = "transitive", defaultValue = "true" )
175     private boolean transitive = true;
176 
177     /**
178      * Skip plugin execution completely.
179      *
180      * @since 2.7
181      */
182     @Parameter( property = "mdep.skip", defaultValue = "false" )
183     private boolean skip;
184 
185     public void execute()
186         throws MojoExecutionException, MojoFailureException
187     {
188         if ( isSkip() )
189         {
190             getLog().info( "Skipping plugin execution" );
191             return;
192         }
193 
194         if ( artifactId == null && artifact == null )
195         {
196             throw new MojoFailureException( "You must specify an artifact, "
197                 + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0" );
198         }
199         if ( artifact != null )
200         {
201             String[] tokens = StringUtils.split( artifact, ":" );
202             if ( tokens.length < 3 || tokens.length > 5 )
203             {
204                 throw new MojoFailureException(
205                     "Invalid artifact, you must specify groupId:artifactId:version[:packaging][:classifier] "
206                         + artifact );
207             }
208             groupId = tokens[0];
209             artifactId = tokens[1];
210             version = tokens[2];
211             if ( tokens.length >= 4 )
212             {
213                 packaging = tokens[3];
214             }
215             if ( tokens.length == 5 )
216             {
217                 classifier = tokens[4];
218             }
219             else
220             {
221                 classifier = null;
222             }
223         }
224 
225         Artifact toDownload = classifier == null
226             ? artifactFactory.createBuildArtifact( groupId, artifactId, version, packaging )
227             : artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier );
228         Artifact dummyOriginatingArtifact =
229             artifactFactory.createBuildArtifact( "org.apache.maven.plugins", "maven-downloader-plugin", "1.0", "jar" );
230 
231         ArtifactRepositoryPolicy always =
232             new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
233                                           ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
234 
235         List<ArtifactRepository> repoList = new ArrayList<ArtifactRepository>();
236 
237         if ( pomRemoteRepositories != null )
238         {
239             repoList.addAll( pomRemoteRepositories );
240         }
241 
242         if ( remoteRepositories != null )
243         {
244             // Use the same format as in the deploy plugin id::layout::url
245             List<String> repos = Arrays.asList( StringUtils.split( remoteRepositories, "," ) );
246             for ( String repo : repos )
247             {
248                 repoList.add( parseRepository( repo, always ) );
249             }
250         }
251 
252         if ( repositoryUrl != null )
253         {
254             getLog().warn( "repositoryUrl parameter is deprecated. Use remoteRepositories instead" );
255             ArtifactRepository remoteRepo =
256                 artifactRepositoryFactory.createArtifactRepository( repositoryId, repositoryUrl, getLayout( "default" ),
257                                                                     always, always );
258             repoList.add( remoteRepo );
259         }
260 
261         try
262         {
263             if ( transitive )
264             {
265                 artifactResolver.resolveTransitively( Collections.singleton( toDownload ), dummyOriginatingArtifact,
266                                                       repoList, localRepository, source );
267             }
268             else
269             {
270                 artifactResolver.resolve( toDownload, repoList, localRepository );
271             }
272         }
273         catch ( AbstractArtifactResolutionException e )
274         {
275             throw new MojoExecutionException( "Couldn't download artifact: " + e.getMessage(), e );
276         }
277 
278         if ( destination != null )
279         {
280             File src = toDownload.getFile();
281             File dest = new File( destination );
282             if ( getLog().isInfoEnabled() )
283             {
284                 getLog().info( "Copying " + src.getAbsolutePath() + " to " + dest.getAbsolutePath() );
285             }
286             try
287             {
288                 if ( dest.isDirectory() )
289                 {
290                     FileUtils.copyFileToDirectory( src, dest );
291                 }
292                 else
293                 {
294                     FileUtils.copyFile( src, dest );
295                 } 
296             }
297             catch ( IOException e )
298             {
299                 throw new MojoExecutionException(
300                     "Couldn't copy downloaded artifact from " + src.getAbsolutePath() + " to " + dest.getAbsolutePath()
301                         + " : " + e.getMessage(), e );
302             }
303         }
304     }
305 
306     ArtifactRepository parseRepository( String repo, ArtifactRepositoryPolicy policy )
307         throws MojoFailureException
308     {
309         // if it's a simple url
310         String id = repositoryId;
311         ArtifactRepositoryLayout layout = getLayout( "default" );
312         String url = repo;
313 
314         // if it's an extended repo URL of the form id::layout::url
315         if ( repo.indexOf( "::" ) >= 0 )
316         {
317             Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( repo );
318             if ( !matcher.matches() )
319             {
320                 throw new MojoFailureException( repo, "Invalid syntax for repository: " + repo,
321                                                 "Invalid syntax for repository. Use \"id::layout::url\" or \"URL\"." );
322             }
323 
324             id = matcher.group( 1 ).trim();
325             if ( !StringUtils.isEmpty( matcher.group( 2 ) ) )
326             {
327                 layout = getLayout( matcher.group( 2 ).trim() );
328             }
329             url = matcher.group( 3 ).trim();
330         }
331         return artifactRepositoryFactory.createArtifactRepository( id, url, layout, policy, policy );
332     }
333 
334     private ArtifactRepositoryLayout getLayout( String id )
335         throws MojoFailureException
336     {
337         ArtifactRepositoryLayout layout = repositoryLayouts.get( id );
338 
339         if ( layout == null )
340         {
341             throw new MojoFailureException( id, "Invalid repository layout", "Invalid repository layout: " + id );
342         }
343 
344         return layout;
345     }
346 
347     public boolean isSkip()
348     {
349         return skip;
350 }
351 
352     public void setSkip( boolean skip )
353     {
354         this.skip = skip;
355     }
356 
357 }