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