View Javadoc

1   package org.apache.maven.artifact.ant;
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.ArtifactUtils;
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.resolver.ArtifactNotFoundException;
28  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
29  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
30  import org.apache.maven.artifact.resolver.ArtifactResolver;
31  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
32  import org.apache.maven.artifact.versioning.VersionRange;
33  import org.apache.maven.project.artifact.MavenMetadataSource;
34  import org.apache.tools.ant.BuildException;
35  import org.apache.tools.ant.Project;
36  import org.codehaus.plexus.PlexusContainerException;
37  
38  import java.util.Collections;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * Ant Wrapper for wagon provider installation.
44   *
45   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
46   * @version $Id: InstallWagonProviderTask.html 806929 2012-03-01 18:57:40Z hboutemy $
47   */
48  public class InstallWagonProviderTask
49      extends AbstractArtifactWithRepositoryTask
50  {
51      private String groupId = "org.apache.maven.wagon";
52  
53      private String artifactId;
54  
55      private String version;
56  
57      public String getGroupId()
58      {
59          return groupId;
60      }
61  
62      public void setGroupId( String groupId )
63      {
64          this.groupId = groupId;
65      }
66  
67      public String getArtifactId()
68      {
69          return artifactId;
70      }
71  
72      public void setArtifactId( String artifactId )
73      {
74          this.artifactId = artifactId;
75      }
76  
77      public String getVersion()
78      {
79          return version;
80      }
81  
82      public void setVersion( String version )
83      {
84          this.version = version;
85      }
86  
87      public void doExecute()
88      {
89          VersionRange versionRange;
90          try
91          {
92              versionRange = VersionRange.createFromVersionSpec( version );
93          }
94          catch ( InvalidVersionSpecificationException e )
95          {
96              throw new BuildException( "Unable to get extension '"
97                                        + ArtifactUtils.versionlessKey( groupId, artifactId ) + "' because version '"
98                                        + version + " is invalid: " + e.getMessage(), e );
99          }
100 
101         ArtifactFactory factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
102         Artifact providerArtifact = factory.createExtensionArtifact( groupId, artifactId, versionRange );
103 
104         log( "Installing provider: " + providerArtifact );
105 
106         ArtifactResolutionResult result;
107         try
108         {
109             MavenMetadataSource metadataSource = (MavenMetadataSource) lookup( ArtifactMetadataSource.ROLE );
110             ArtifactResolver resolver = (ArtifactResolver) lookup( ArtifactResolver.ROLE );
111             List<ArtifactRepository> remoteRepositories = createRemoteArtifactRepositories();
112 
113             result = resolver.resolveTransitively( Collections.singleton( providerArtifact ),
114                                                    createDummyArtifact(), createLocalArtifactRepository(),
115                                                    remoteRepositories, metadataSource, null );
116         }
117         catch ( ArtifactResolutionException e )
118         {
119             throw new BuildException( "Error downloading wagon provider from the remote repository: " + e.getMessage(),
120                                       e );
121         }
122         catch ( ArtifactNotFoundException e )
123         {
124             throw new BuildException( "Unable to locate wagon provider in remote repository: " + e.getMessage(), e );
125         }
126 
127         try
128         {
129             for ( Iterator<Artifact> i = result.getArtifacts().iterator(); i.hasNext(); )
130             {
131                 Artifact a = i.next();
132 
133                 getContainer().addJarResource( a.getFile() );
134             }
135         }
136         catch ( PlexusContainerException e )
137         {
138             throw new BuildException( "Unable to locate wagon provider in remote repository", e );
139         }
140 
141         log( "Protocols now supported: " + getSupportedProtocolsAsString(), Project.MSG_VERBOSE );
142     }
143 
144 }