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.deployer.ArtifactDeployer;
   24   import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
   25   import org.apache.maven.artifact.metadata.ArtifactMetadata;
   26   import org.apache.maven.artifact.repository.ArtifactRepository;
   27   import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
   28   import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
   29   import org.apache.maven.model.DistributionManagement;
   30   import org.apache.maven.project.artifact.ProjectArtifactMetadata;
   31   import org.apache.tools.ant.BuildException;
   32   import org.apache.tools.ant.Project;
   33   
   34   import java.util.Iterator;
   35   
   36   /**
   37    * Deploy task, using maven-artifact.
   38    *
   39    * @author <a href="mailto:brett@apache.org">Brett Porter</a>
   40    * @version $Id: DeployTask.java 884304 2009-11-25 22:25:22Z pgier $
   41    */
   42   public class DeployTask
   43       extends InstallDeployTaskSupport
   44   {
   45       private RemoteRepository remoteRepository;
   46   
   47       private RemoteRepository remoteSnapshotRepository;
   48   
   49       private boolean uniqueVersion = true;
   50   
   51       /**
   52        * Create a core-Maven deployment ArtifactRepository from a Maven Ant Tasks's RemoteRepository definition.
   53        * @param repository the remote repository as defined in Ant
   54        * @return the corresponding ArtifactRepository
   55        */
   56       protected ArtifactRepository createDeploymentArtifactRepository( RemoteRepository repository )
   57       {
   58           if ( repository.getId().equals( repository.getUrl() ) )
   59           {
   60               // MANTTASKS-103: avoid default id set to the url, since it is used for maven-metadata-<id>.xml  
   61               repository.setId( "remote" );
   62           }
   63   
   64           updateRepositoryWithSettings( repository );
   65   
   66           ArtifactRepositoryLayout repositoryLayout =
   67               (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, repository.getLayout() );
   68   
   69           ArtifactRepositoryFactory repositoryFactory = null;
   70   
   71           ArtifactRepository artifactRepository;
   72   
   73           try
   74           {
   75               repositoryFactory = getArtifactRepositoryFactory( repository );
   76   
   77               artifactRepository =
   78                   repositoryFactory.createDeploymentArtifactRepository( repository.getId(), repository.getUrl(),
   79                                                                         repositoryLayout, uniqueVersion );
   80           }
   81           finally
   82           {
   83               releaseArtifactRepositoryFactory( repositoryFactory );
   84           }
   85   
   86           return artifactRepository;
   87       }
   88   
   89   
   90       protected void doExecute()
   91       {
   92           if ( file == null && ( attachedArtifacts.size() == 0 ) )
   93           {
   94               throw new BuildException( "You must specify a file and/or an attached artifact "
   95                   + "to deploy to the repository." );
   96           }
   97           
   98           ArtifactRepository localRepo = createLocalArtifactRepository();
   99   
  100           Pom pom = initializePom( localRepo );
  101   
  102           if ( pom == null )
  103           {
  104               throw new BuildException( "A POM element is required to deploy to the repository" );
  105           }
  106           
  107           Artifact artifact = pom.getArtifact();
  108   
  109           // Deploy the POM
  110           boolean isPomArtifact = "pom".equals( pom.getPackaging() );
  111           if ( !isPomArtifact )
  112           {
  113               ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pom.getFile() );
  114               artifact.addMetadata( metadata );
  115           }
  116   
  117           ArtifactRepository deploymentRepository = getDeploymentRepository( pom, artifact );
  118   
  119           log( "Deploying to " + deploymentRepository.getUrl(), Project.MSG_INFO );
  120           ArtifactDeployer deployer = (ArtifactDeployer) lookup( ArtifactDeployer.ROLE );
  121           try
  122           {
  123               if ( file != null )
  124               {
  125                   if ( !isPomArtifact )
  126                   {
  127                       deployer.deploy( file, artifact, deploymentRepository, localRepo );
  128                   }
  129                   else
  130                   {
  131                       deployer.deploy( pom.getFile(), artifact, deploymentRepository, localRepo );
  132                   }
  133               }
  134   
  135               // Deploy any attached artifacts
  136               if ( attachedArtifacts != null )
  137               {
  138                   Iterator iter = pom.getAttachedArtifacts().iterator();
  139   
  140                   while ( iter.hasNext() )
  141                   {
  142                       Artifact attachedArtifact = (Artifact) iter.next();
  143                       deployer.deploy( attachedArtifact.getFile(), attachedArtifact, deploymentRepository, localRepo );
  144                   }
  145               }
  146           }
  147           catch ( ArtifactDeploymentException e )
  148           {
  149               throw new BuildException(
  150                   "Error deploying artifact '" + artifact.getDependencyConflictId() + "': " + e.getMessage(), e );
  151           }
  152       }
  153       
  154       private ArtifactRepository getDeploymentRepository( Pom pom, Artifact artifact )
  155       {
  156           DistributionManagement distributionManagement = pom.getDistributionManagement();
  157   
  158           if ( remoteSnapshotRepository == null && remoteRepository == null )
  159           {
  160               if ( distributionManagement != null )
  161               {
  162                   if ( distributionManagement.getSnapshotRepository() != null )
  163                   {
  164                       remoteSnapshotRepository = createAntRemoteRepositoryBase( distributionManagement
  165                           .getSnapshotRepository() );
  166                       uniqueVersion = distributionManagement.getSnapshotRepository().isUniqueVersion();
  167                   }
  168                   if ( distributionManagement.getRepository() != null )
  169                   {
  170                       remoteRepository = createAntRemoteRepositoryBase( distributionManagement.getRepository() );
  171                   }
  172               }
  173           }
  174   
  175           if ( remoteSnapshotRepository == null )
  176           {
  177               remoteSnapshotRepository = remoteRepository;
  178           }
  179   
  180           ArtifactRepository deploymentRepository;
  181           if ( artifact.isSnapshot() && remoteSnapshotRepository != null )
  182           {
  183               deploymentRepository = createDeploymentArtifactRepository( remoteSnapshotRepository );
  184           }
  185           else if ( remoteRepository != null )
  186           {
  187               deploymentRepository = createDeploymentArtifactRepository( remoteRepository );
  188           }
  189           else
  190           {
  191               throw new BuildException(
  192                   "A distributionManagement element or remoteRepository element is required to deploy" );
  193           }
  194   
  195           return deploymentRepository;
  196       }
  197   
  198       public RemoteRepository getRemoteRepository()
  199       {
  200           return remoteRepository;
  201       }
  202   
  203       public void addRemoteSnapshotRepository( RemoteRepository remoteSnapshotRepository )
  204       {
  205           this.remoteSnapshotRepository = remoteSnapshotRepository;
  206       }
  207   
  208       public void addRemoteRepository( RemoteRepository remoteRepository )
  209       {
  210           this.remoteRepository = remoteRepository;
  211       }
  212   
  213       public void setUniqueVersion( boolean uniqueVersion )
  214       {
  215           this.uniqueVersion = uniqueVersion;
  216       }
  217   
  218       public boolean getUniqueVersion()
  219       {
  220           return uniqueVersion;
  221       }
  222   }

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