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