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

Save This Page
Home » maven-ant-tasks-2.1.0-src » org.apache » maven » artifact » ant » [javadoc | source]