View Javadoc

1   package org.apache.maven.artifact.deployer;
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.manager.WagonManager;
24  import org.apache.maven.artifact.metadata.ArtifactMetadata;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataDeploymentException;
27  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
28  import org.apache.maven.artifact.transform.ArtifactTransformationManager;
29  import org.apache.maven.wagon.TransferFailedException;
30  import org.codehaus.plexus.logging.AbstractLogEnabled;
31  import org.codehaus.plexus.util.FileUtils;
32  
33  import java.io.File;
34  import java.io.IOException;
35  import java.util.Iterator;
36  
37  public class DefaultArtifactDeployer
38      extends AbstractLogEnabled
39      implements ArtifactDeployer
40  {
41      private WagonManager wagonManager;
42  
43      private ArtifactTransformationManager transformationManager;
44  
45      private RepositoryMetadataManager repositoryMetadataManager;
46  
47      /**
48       * @deprecated we want to use the artifact method only, and ensure artifact.file is set correctly.
49       */
50      public void deploy( String basedir, String finalName, Artifact artifact, ArtifactRepository deploymentRepository,
51                          ArtifactRepository localRepository )
52          throws ArtifactDeploymentException
53      {
54          String extension = artifact.getArtifactHandler().getExtension();
55          File source = new File( basedir, finalName + "." + extension );
56          deploy( source, artifact, deploymentRepository, localRepository );
57      }
58  
59      public void deploy( File source, Artifact artifact, ArtifactRepository deploymentRepository,
60                          ArtifactRepository localRepository )
61          throws ArtifactDeploymentException
62      {
63          if ( !wagonManager.isOnline() )
64          {
65              // deployment shouldn't silently fail when offline
66              throw new ArtifactDeploymentException( "System is offline. Cannot deploy artifact: " + artifact + "." );
67          }
68  
69          try
70          {
71              transformationManager.transformForDeployment( artifact, deploymentRepository, localRepository );
72  
73              // Copy the original file to the new one if it was transformed
74              File artifactFile = new File( localRepository.getBasedir(), localRepository.pathOf( artifact ) );
75              if ( !artifactFile.equals( source ) )
76              {
77                  FileUtils.copyFile( source, artifactFile );
78              }
79  
80              wagonManager.putArtifact( source, artifact, deploymentRepository );
81  
82              // must be after the artifact is installed
83              for ( Iterator i = artifact.getMetadataList().iterator(); i.hasNext(); )
84              {
85                  ArtifactMetadata metadata = (ArtifactMetadata) i.next();
86                  repositoryMetadataManager.deploy( metadata, localRepository, deploymentRepository );
87              }
88              // TODO: would like to flush this, but the plugin metadata is added in advance, not as an install/deploy transformation
89              // This would avoid the need to merge and clear out the state during deployment
90  //            artifact.getMetadataList().clear();
91          }
92          catch ( TransferFailedException e )
93          {
94              throw new ArtifactDeploymentException( "Error deploying artifact: " + e.getMessage(), e );
95          }
96          catch ( IOException e )
97          {
98              throw new ArtifactDeploymentException( "Error deploying artifact: " + e.getMessage(), e );
99          }
100         catch ( RepositoryMetadataDeploymentException e )
101         {
102             throw new ArtifactDeploymentException( "Error installing artifact's metadata: " + e.getMessage(), e );
103         }
104     }
105 }