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
59 // If we're installing the POM, we need to transform it first. The source file supplied for
60 // installation here may be the POM, but that POM may not be set as the file of the supplied
61 // artifact. Since the transformation only has access to the artifact and not the supplied
62 // source file, we have to use the Artifact.setFile(..) and Artifact.getFile(..) methods
63 // to shunt the POM file into the transformation process.
64 // Here, we also set a flag indicating that the POM has been shunted through the Artifact,
65 // and to expect the transformed version to be available in the Artifact afterwards...
66 boolean useArtifactFile = false;
67 File oldArtifactFile = artifact.getFile();
68 if ( "pom".equals( artifact.getType() ) )
69 {
70 artifact.setFile( source );
71 useArtifactFile = true;
72 }
73
74 try
75 {
76 transformationManager.transformForInstall( artifact, localRepository );
77
78 // If we used the Artifact shunt to transform a POM source file, we need to install
79 // the transformed version, not the supplied version. Therefore, we need to replace
80 // the supplied source POM with the one from Artifact.getFile(..).
81 if ( useArtifactFile )
82 {
83 source = artifact.getFile();
84 artifact.setFile( oldArtifactFile );
85 }
86
87 String localPath = localRepository.pathOf( artifact );
88
89 // TODO: use a file: wagon and the wagon manager?
90 File destination = new File( localRepository.getBasedir(), localPath );
91 if ( !destination.getParentFile().exists() )
92 {
93 destination.getParentFile().mkdirs();
94 }
95
96 getLogger().info( "Installing " + source.getPath() + " to " + destination );
97
98 FileUtils.copyFile( source, destination );
99
100 // Now, we'll set the artifact's file to the one installed in the local repository,
101 // to help avoid duplicate copy operations in the deployment step.
102 if ( useArtifactFile )
103 {
104 artifact.setFile( destination );
105 }
106
107 // must be after the artifact is installed
108 for ( Iterator i = artifact.getMetadataList().iterator(); i.hasNext(); )
109 {
110 ArtifactMetadata metadata = (ArtifactMetadata) i.next();
111 repositoryMetadataManager.install( metadata, localRepository );
112 }
113 // TODO: would like to flush this, but the plugin metadata is added in advance, not as an install/deploy transformation
114 // This would avoid the need to merge and clear out the state during deployment
115 // artifact.getMetadataList().clear();
116 }
117 catch ( IOException e )
118 {
119 throw new ArtifactInstallationException( "Error installing artifact: " + e.getMessage(), e );
120 }
121 catch ( RepositoryMetadataInstallationException e )
122 {
123 throw new ArtifactInstallationException( "Error installing artifact's metadata: " + e.getMessage(), e );
124 }
125 }
126 }