1 package org.apache.maven.plugin.install;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
42
43
44
45
46 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
67
68
69
70 @Parameter( property = "createChecksum", defaultValue = "false" )
71 protected boolean createChecksum;
72
73
74
75
76 @Parameter( property = "updateReleaseInfo", defaultValue = "false" )
77 protected boolean updateReleaseInfo;
78
79
80
81
82 @Component( hint = "md5" )
83 protected Digester md5Digester;
84
85
86
87
88 @Component( hint = "sha1" )
89 protected Digester sha1Digester;
90
91
92
93
94
95
96
97
98 protected File getLocalRepoFile( Artifact artifact )
99 {
100 String path = localRepository.pathOf( artifact );
101 return new File( localRepository.getBasedir(), path );
102 }
103
104
105
106
107
108
109
110
111 protected File getLocalRepoFile( ArtifactMetadata metadata )
112 {
113 String path = localRepository.pathOfLocalRepositoryMetadata( metadata, localRepository );
114 return new File( localRepository.getBasedir(), path );
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129 protected void installChecksums( Artifact artifact, Collection metadataFiles )
130 throws MojoExecutionException
131 {
132 if ( !createChecksum )
133 {
134 return;
135 }
136
137 File artifactFile = getLocalRepoFile( artifact );
138 installChecksums( artifactFile );
139
140 Collection metadatas = artifact.getMetadataList();
141 if ( metadatas != null )
142 {
143 for ( Iterator it = metadatas.iterator(); it.hasNext(); )
144 {
145 ArtifactMetadata metadata = (ArtifactMetadata) it.next();
146 File metadataFile = getLocalRepoFile( metadata );
147 metadataFiles.add( metadataFile );
148 }
149 }
150 }
151
152
153
154
155
156
157
158 protected void installChecksums( Collection metadataFiles )
159 throws MojoExecutionException
160 {
161 for ( Iterator it = metadataFiles.iterator(); it.hasNext(); )
162 {
163 File metadataFile = (File) it.next();
164 installChecksums( metadataFile );
165 }
166 }
167
168
169
170
171
172
173
174
175 private void installChecksums( File installedFile )
176 throws MojoExecutionException
177 {
178 boolean signatureFile = installedFile.getName().endsWith( ".asc" );
179 if ( installedFile.isFile() && !signatureFile )
180 {
181 installChecksum( installedFile, installedFile, md5Digester, ".md5" );
182 installChecksum( installedFile, installedFile, sha1Digester, ".sha1" );
183 }
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197 private void installChecksum( File originalFile, File installedFile, Digester digester, String ext )
198 throws MojoExecutionException
199 {
200 String checksum;
201 getLog().debug( "Calculating " + digester.getAlgorithm() + " checksum for " + originalFile );
202 try
203 {
204 checksum = digester.calc( originalFile );
205 }
206 catch ( DigesterException e )
207 {
208 throw new MojoExecutionException( "Failed to calculate " + digester.getAlgorithm() + " checksum for "
209 + originalFile, e );
210 }
211
212 File checksumFile = new File( installedFile.getAbsolutePath() + ext );
213 getLog().debug( "Installing checksum to " + checksumFile );
214 try
215 {
216 checksumFile.getParentFile().mkdirs();
217 FileUtils.fileWrite( checksumFile.getAbsolutePath(), "UTF-8", checksum );
218 }
219 catch ( IOException e )
220 {
221 throw new MojoExecutionException( "Failed to install checksum to " + checksumFile, e );
222 }
223 }
224
225 }