View Javadoc

1   package org.apache.maven.plugin.dependency;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.factory.ArtifactFactory;
26  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
29  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
30  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
31  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
32  import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
33  import org.apache.maven.artifact.resolver.ArtifactResolver;
34  import org.apache.maven.plugin.AbstractMojo;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugin.MojoFailureException;
37  import org.codehaus.plexus.util.StringUtils;
38  
39  /**
40   * Downloads a single artifact transitively from a specified remote repository.
41   *
42   * @goal get
43   * @requiresProject false
44   * 
45   */
46  public class GetMojo
47      extends AbstractMojo
48  {
49      
50      /**
51       * @component
52       * @readonly
53       */
54      private ArtifactFactory artifactFactory;
55      
56      /**
57       * @component
58       * @readonly
59       */
60      private ArtifactResolver artifactResolver;
61      
62      /**
63       * @component
64       * @readonly
65       */
66      private ArtifactRepositoryFactory artifactRepositoryFactory;
67      
68      /**
69       * @component
70       * @readonly
71       */
72      private ArtifactMetadataSource source;
73      
74      /**
75       * 
76       * @parameter expression="${localRepository}"
77       * @readonly
78       */
79      private ArtifactRepository localRepository;
80  
81      /**
82       * The groupId of the artifact to download
83       * @parameter expression="${groupId}"
84       */
85      private String groupId;
86  
87      /**
88       * The artifactId of the artifact to download
89       * @parameter expression="${artifactId}"
90       */
91      private String artifactId;
92  
93      /**
94       * The version of the artifact to download
95       * @parameter expression="${version}"
96       */
97      private String version;
98  
99      /**
100      * The packaging of the artifact to download
101      * @parameter expression="${packaging}" default-value="jar"
102      */
103     private String packaging = "jar";
104 
105     /**
106      * The id of the repository from which we'll download the artifact
107      * @parameter expression="${repoId}" default-value="temp"
108      */
109     private String repositoryId = "temp";
110 
111     /**
112      * The url of the repository from which we'll download the artifact
113      * @parameter expression="${repoUrl}"
114      * @required
115      */
116     private String repositoryUrl;
117 
118     /**
119      * @parameter expression="${remoteRepositories}"
120      * @readonly
121      */
122     private String remoteRepositories;
123     
124     /**
125      * A string of the form groupId:artifactId:version[:packaging].
126      * @parameter expression="${artifact}"
127      */
128     private String artifact;
129     
130     /**
131      * 
132      * @parameter expression="${project.remoteArtifactRepositories}"
133      * @required
134      * @readonly
135      */
136     private List pomRemoteRepositories;
137     
138     /**
139      * Download transitively, retrieving the specified artifact and all of its dependencies.
140      * @parameter expression="{$transitive}" default-value=true
141      */
142     private boolean transitive = true;
143     
144     public void execute()
145         throws MojoExecutionException, MojoFailureException
146     {        
147 
148         if ( artifactId == null && artifact == null )
149             throw new MojoFailureException( "You must specify an artifact, "
150                 + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0" );
151         if ( artifactId == null )
152         {
153             String[] tokens = StringUtils.split( artifact, ":" );
154             if ( tokens.length != 3 && tokens.length != 4 )
155                 throw new MojoFailureException( "Invalid artifact, you must specify "
156                     + "groupId:artifactId:version[:packaging] " + artifact );
157             groupId = tokens[0];
158             artifactId = tokens[1];
159             version = tokens[2];
160             if ( tokens.length == 4 )
161                 packaging = tokens[3];
162         }
163         Artifact toDownload = artifactFactory.createBuildArtifact( groupId, artifactId, version, packaging );
164         Artifact dummyOriginatingArtifact =
165             artifactFactory.createBuildArtifact( "org.apache.maven.plugins", "maven-downloader-plugin", "1.0", "jar" );
166 
167         ArtifactRepositoryLayout repositoryLayout = new DefaultRepositoryLayout();
168         ArtifactRepositoryPolicy always =
169             new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
170                                           ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
171         ArtifactRepository remoteRepo =
172             artifactRepositoryFactory.createArtifactRepository( repositoryId, repositoryUrl, repositoryLayout, always,
173                                                                 always );
174 
175         if ( pomRemoteRepositories == null )
176             pomRemoteRepositories = new ArrayList();
177 
178         List repoList = new ArrayList( pomRemoteRepositories );
179         if ( remoteRepositories != null )
180         {
181 
182             repoList.addAll( Arrays.asList( StringUtils.split( remoteRepositories, "," ) ) );
183 
184         }
185 
186         repoList.add( remoteRepo );
187 
188         try
189         {
190             if ( transitive )
191             {
192                 artifactResolver.resolveTransitively( Collections.singleton( toDownload ), dummyOriginatingArtifact,
193                                                       repoList, localRepository, source );
194             }
195             else
196             {
197                 artifactResolver.resolve ( toDownload, repoList, localRepository );
198             }
199             
200         }
201         catch ( AbstractArtifactResolutionException e )
202         {
203             throw new MojoExecutionException( "Couldn't download artifact: " + e.getMessage(), e );
204         }
205     }
206 }