1 package org.apache.maven.plugin.install;
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.factory.ArtifactFactory;
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.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.codehaus.plexus.digest.Digester;
30 import org.codehaus.plexus.digest.DigesterException;
31 import org.codehaus.plexus.util.FileUtils;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.Collection;
36 import java.util.Iterator;
37
38 /**
39 * Common fields for installation mojos.
40 *
41 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42 * @version $Id: AbstractInstallMojo.java 756207 2009-03-19 21:18:49Z bentmann $
43 */
44 public abstract class AbstractInstallMojo
45 extends AbstractMojo
46 {
47
48 /**
49 * @component
50 */
51 protected ArtifactFactory artifactFactory;
52
53 /**
54 * @component
55 */
56 protected ArtifactInstaller installer;
57
58 /**
59 * @parameter expression="${localRepository}"
60 * @required
61 * @readonly
62 */
63 protected ArtifactRepository localRepository;
64
65 /**
66 * Flag whether to create checksums (MD5, SHA-1) or not.
67 *
68 * @parameter expression="${createChecksum}" default-value="false"
69 * @since 2.2
70 */
71 protected boolean createChecksum;
72
73 /**
74 * Whether to update the metadata to make the artifact a release version.
75 *
76 * @parameter expression="${updateReleaseInfo}" default-value="false"
77 */
78 protected boolean updateReleaseInfo;
79
80 /**
81 * Digester for MD5.
82 *
83 * @component role-hint="md5"
84 */
85 protected Digester md5Digester;
86
87 /**
88 * Digester for SHA-1.
89 *
90 * @component role-hint="sha1"
91 */
92 protected Digester sha1Digester;
93
94 /**
95 * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist
96 * (yet).
97 *
98 * @param artifact The artifact whose local repo path should be determined, must not be <code>null</code>.
99 * @return The absolute path to the artifact when installed, never <code>null</code>.
100 */
101 protected File getLocalRepoFile( Artifact artifact )
102 {
103 String path = localRepository.pathOf( artifact );
104 return new File( localRepository.getBasedir(), path );
105 }
106
107 /**
108 * Gets the path of the specified artifact metadata within the local repository. Note that the returned path need
109 * not exist (yet).
110 *
111 * @param metadata The artifact metadata whose local repo path should be determined, must not be <code>null</code>.
112 * @return The absolute path to the artifact metadata when installed, never <code>null</code>.
113 */
114 protected File getLocalRepoFile( ArtifactMetadata metadata )
115 {
116 String path = localRepository.pathOfLocalRepositoryMetadata( metadata, localRepository );
117 return new File( localRepository.getBasedir(), path );
118 }
119
120 /**
121 * Installs the checksums for the specified artifact (and its metadata files) if this has been enabled in the plugin
122 * configuration. This method creates checksums for files that have already been installed to the local repo to
123 * account for on-the-fly generated/updated files. For example, in Maven 2.0.4- the
124 * <code>ProjectArtifactMetadata</code> did not install the original POM file (cf. MNG-2820). While the plugin
125 * currently requires Maven 2.0.6, we continue to hash the installed POM for robustness with regard to future
126 * changes like re-introducing some kind of POM filtering.
127 *
128 * @param artifact The artifact for which to create checksums, must not be <code>null</code>.
129 * @throws MojoExecutionException If the checksums could not be installed.
130 */
131 protected void installChecksums( Artifact artifact )
132 throws MojoExecutionException
133 {
134 if ( !createChecksum )
135 {
136 return;
137 }
138
139 File artifactFile = getLocalRepoFile( artifact );
140 installChecksums( artifactFile );
141
142 Collection metadatas = artifact.getMetadataList();
143 if ( metadatas != null )
144 {
145 for ( Iterator it = metadatas.iterator(); it.hasNext(); )
146 {
147 ArtifactMetadata metadata = (ArtifactMetadata) it.next();
148 File metadataFile = getLocalRepoFile( metadata );
149 installChecksums( metadataFile );
150 }
151 }
152 }
153
154 /**
155 * Installs the checksums for the specified file (if it exists).
156 *
157 * @param installedFile The path to the already installed file in the local repo for which to generate checksums,
158 * must not be <code>null</code>.
159 * @throws MojoExecutionException If the checksums could not be installed.
160 */
161 private void installChecksums( File installedFile )
162 throws MojoExecutionException
163 {
164 boolean signatureFile = installedFile.getName().endsWith( ".asc" );
165 if ( installedFile.isFile() && !signatureFile )
166 {
167 installChecksum( installedFile, installedFile, md5Digester, ".md5" );
168 installChecksum( installedFile, installedFile, sha1Digester, ".sha1" );
169 }
170 }
171
172 /**
173 * Installs a checksum for the specified file.
174 *
175 * @param originalFile The path to the file from which the checksum is generated, must not be <code>null</code>.
176 * @param installedFile The base path from which the path to the checksum files is derived by appending the given
177 * file extension, must not be <code>null</code>.
178 * @param digester The checksum algorithm to use, must not be <code>null</code>.
179 * @param ext The file extension (including the leading dot) to use for the checksum file, must not be
180 * <code>null</code>.
181 * @throws MojoExecutionException If the checksum could not be installed.
182 */
183 private void installChecksum( File originalFile, File installedFile, Digester digester, String ext )
184 throws MojoExecutionException
185 {
186 String checksum;
187 getLog().debug( "Calculating " + digester.getAlgorithm() + " checksum for " + originalFile );
188 try
189 {
190 checksum = digester.calc( originalFile );
191 }
192 catch ( DigesterException e )
193 {
194 throw new MojoExecutionException( "Failed to calculate " + digester.getAlgorithm() + " checksum for "
195 + originalFile, e );
196 }
197
198 File checksumFile = new File( installedFile.getAbsolutePath() + ext );
199 getLog().debug( "Installing checksum to " + checksumFile );
200 try
201 {
202 checksumFile.getParentFile().mkdirs();
203 FileUtils.fileWrite( checksumFile.getAbsolutePath(), "UTF-8", checksum );
204 }
205 catch ( IOException e )
206 {
207 throw new MojoExecutionException( "Failed to install checksum to " + checksumFile, e );
208 }
209 }
210
211 }