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          // If we're installing the POM, we need to transform it first. The source file supplied for 
70          // installation here may be the POM, but that POM may not be set as the file of the supplied
71          // artifact. Since the transformation only has access to the artifact and not the supplied
72          // source file, we have to use the Artifact.setFile(..) and Artifact.getFile(..) methods
73          // to shunt the POM file into the transformation process.
74          // Here, we also set a flag indicating that the POM has been shunted through the Artifact,
75          // and to expect the transformed version to be available in the Artifact afterwards...
76          boolean useArtifactFile = false;
77          File oldArtifactFile = artifact.getFile();
78          if ( "pom".equals( artifact.getType() ) )
79          {
80              artifact.setFile( source );
81              useArtifactFile = true;
82          }
83          
84          try
85          {
86              transformationManager.transformForDeployment( artifact, deploymentRepository, localRepository );
87  
88              // If we used the Artifact shunt to transform a POM source file, we need to install
89              // the transformed version, not the supplied version. Therefore, we need to replace
90              // the supplied source POM with the one from Artifact.getFile(..).
91              if ( useArtifactFile )
92              {
93                  source = artifact.getFile();
94                  artifact.setFile( oldArtifactFile );
95              }
96  
97              // FIXME: Why oh why are we re-installing the artifact in the local repository? Isn't this
98              // the responsibility of the ArtifactInstaller??
99              
100             // Copy the original file to the new one if it was transformed
101             File artifactFile = new File( localRepository.getBasedir(), localRepository.pathOf( artifact ) );
102             if ( !artifactFile.equals( source ) )
103             {
104                 FileUtils.copyFile( source, artifactFile );
105             }
106 
107             wagonManager.putArtifact( source, artifact, deploymentRepository );
108 
109             // must be after the artifact is installed
110             for ( Iterator i = artifact.getMetadataList().iterator(); i.hasNext(); )
111             {
112                 ArtifactMetadata metadata = (ArtifactMetadata) i.next();
113                 repositoryMetadataManager.deploy( metadata, localRepository, deploymentRepository );
114             }
115             // TODO: would like to flush this, but the plugin metadata is added in advance, not as an install/deploy transformation
116             // This would avoid the need to merge and clear out the state during deployment
117 //            artifact.getMetadataList().clear();
118         }
119         catch ( TransferFailedException e )
120         {
121             throw new ArtifactDeploymentException( "Error deploying artifact: " + e.getMessage(), e );
122         }
123         catch ( IOException e )
124         {
125             throw new ArtifactDeploymentException( "Error deploying artifact: " + e.getMessage(), e );
126         }
127         catch ( RepositoryMetadataDeploymentException e )
128         {
129             throw new ArtifactDeploymentException( "Error installing artifact's metadata: " + e.getMessage(), e );
130         }
131     }
132 }