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.installer.ArtifactInstallationException;
   24   import org.apache.maven.artifact.installer.ArtifactInstaller;
   25   import org.apache.maven.artifact.metadata.ArtifactMetadata;
   26   import org.apache.maven.artifact.repository.ArtifactRepository;
   27   import org.apache.maven.project.artifact.ProjectArtifactMetadata;
   28   import org.apache.tools.ant.BuildException;
   29   
   30   import java.util.Iterator;
   31   
   32   /**
   33    * Install task, using maven-artifact.
   34    *
   35    * @author <a href="mailto:brett@apache.org">Brett Porter</a>
   36    * @version $Id: InstallTask.java 884304 2009-11-25 22:25:22Z pgier $
   37    * @todo should be able to incorporate into the install mojo?
   38    */
   39   public class InstallTask
   40       extends InstallDeployTaskSupport
   41   {
   42       protected void doExecute()
   43       {
   44           if ( file == null && ( attachedArtifacts.size() == 0 ) )
   45           {
   46               throw new BuildException( "You must specify a file and/or an attached artifact "
   47                   + "to install to the local repository." );
   48           }
   49           
   50           ArtifactRepository localRepo = createLocalArtifactRepository();
   51   
   52           Pom pom = initializePom( localRepo );
   53   
   54           if ( pom == null )
   55           {
   56               throw new BuildException( "A POM element is required to install to the local repository" );
   57           }
   58           
   59           Artifact artifact = pom.getArtifact();
   60   
   61           boolean isPomArtifact = "pom".equals( pom.getPackaging() );
   62           if ( !isPomArtifact )
   63           {
   64               ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pom.getFile() );
   65               artifact.addMetadata( metadata );
   66           }
   67   
   68           ArtifactInstaller installer = (ArtifactInstaller) lookup( ArtifactInstaller.ROLE );
   69           try
   70           {
   71               if ( file != null )
   72               {
   73                   if ( !isPomArtifact )
   74                   {
   75                       installer.install( file, artifact, localRepo );
   76                   }
   77                   else
   78                   {
   79                       installer.install( pom.getFile(), artifact, localRepo );
   80                   }
   81               }
   82   
   83               // Install any attached artifacts
   84               if ( attachedArtifacts != null )
   85               {
   86                   Iterator iter = pom.getAttachedArtifacts().iterator();
   87   
   88                   while ( iter.hasNext() )
   89                   {
   90                       Artifact attachedArtifact = (Artifact) iter.next();
   91                       installer.install( attachedArtifact.getFile(), attachedArtifact, localRepo );
   92                   }
   93               }
   94           }
   95           catch ( ArtifactInstallationException e )
   96           {
   97               throw new BuildException(
   98                   "Error installing artifact '" + artifact.getDependencyConflictId() + "': " + e.getMessage(), e );
   99           }
  100       }
  101   }

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