View Javadoc

1   package org.apache.maven.artifact.installer;
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.metadata.ArtifactMetadata;
24  import org.apache.maven.artifact.repository.ArtifactRepository;
25  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataInstallationException;
26  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
27  import org.apache.maven.artifact.transform.ArtifactTransformationManager;
28  import org.codehaus.plexus.logging.AbstractLogEnabled;
29  import org.codehaus.plexus.util.FileUtils;
30  
31  import java.io.File;
32  import java.io.IOException;
33  import java.util.Iterator;
34  
35  public class DefaultArtifactInstaller
36      extends AbstractLogEnabled
37      implements ArtifactInstaller
38  {
39      private ArtifactTransformationManager transformationManager;
40  
41      private RepositoryMetadataManager repositoryMetadataManager;
42  
43      /**
44       * @deprecated we want to use the artifact method only, and ensure artifact.file is set correctly.
45       */
46      public void install( String basedir, String finalName, Artifact artifact, ArtifactRepository localRepository )
47          throws ArtifactInstallationException
48      {
49          String extension = artifact.getArtifactHandler().getExtension();
50          File source = new File( basedir, finalName + "." + extension );
51  
52          install( source, artifact, localRepository );
53      }
54  
55      public void install( File source, Artifact artifact, ArtifactRepository localRepository )
56          throws ArtifactInstallationException
57      {
58          try
59          {
60              transformationManager.transformForInstall( artifact, localRepository );
61  
62              String localPath = localRepository.pathOf( artifact );
63  
64              // TODO: use a file: wagon and the wagon manager?
65              File destination = new File( localRepository.getBasedir(), localPath );
66              if ( !destination.getParentFile().exists() )
67              {
68                  destination.getParentFile().mkdirs();
69              }
70  
71              getLogger().info( "Installing " + source.getPath() + " to " + destination );
72  
73              FileUtils.copyFile( source, destination );
74  
75              // must be after the artifact is installed
76              for ( Iterator i = artifact.getMetadataList().iterator(); i.hasNext(); )
77              {
78                  ArtifactMetadata metadata = (ArtifactMetadata) i.next();
79                  repositoryMetadataManager.install( metadata, localRepository );
80              }
81              // TODO: would like to flush this, but the plugin metadata is added in advance, not as an install/deploy transformation
82              // This would avoid the need to merge and clear out the state during deployment
83  //            artifact.getMetadataList().clear();
84          }
85          catch ( IOException e )
86          {
87              throw new ArtifactInstallationException( "Error installing artifact: " + e.getMessage(), e );
88          }
89          catch ( RepositoryMetadataInstallationException e )
90          {
91              throw new ArtifactInstallationException( "Error installing artifact's metadata: " + e.getMessage(), e );
92          }
93      }
94  }