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 java.io.File;
23 import java.io.IOException;
24 import java.util.Collection;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.factory.ArtifactFactory;
28 import org.apache.maven.artifact.installer.ArtifactInstaller;
29 import org.apache.maven.artifact.metadata.ArtifactMetadata;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugins.annotations.Component;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.shared.utils.io.FileUtils;
36
37
38
39
40
41
42
43 public abstract class AbstractInstallMojo
44 extends AbstractMojo
45 {
46
47
48
49 @Component
50 protected ArtifactFactory artifactFactory;
51
52
53
54 @Component
55 protected ArtifactInstaller installer;
56
57
58
59 @Parameter( property = "localRepository", required = true, readonly = true )
60 protected ArtifactRepository localRepository;
61
62
63
64
65
66
67 @Parameter( property = "createChecksum", defaultValue = "false" )
68 protected boolean createChecksum;
69
70
71
72
73 @Parameter( property = "updateReleaseInfo", defaultValue = "false" )
74 protected boolean updateReleaseInfo;
75
76 protected final DualDigester digester = new DualDigester();
77
78
79
80
81
82
83
84
85 protected File getLocalRepoFile( Artifact artifact )
86 {
87 String path = localRepository.pathOf( artifact );
88 return new File( localRepository.getBasedir(), path );
89 }
90
91
92
93
94
95
96
97
98 protected File getLocalRepoFile( ArtifactMetadata metadata )
99 {
100 String path = localRepository.pathOfLocalRepositoryMetadata( metadata, localRepository );
101 return new File( localRepository.getBasedir(), path );
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115 protected void installChecksums( Artifact artifact, boolean createChecksum )
116 throws MojoExecutionException
117 {
118 if ( !createChecksum )
119 {
120 return;
121 }
122
123 File artifactFile = getLocalRepoFile( artifact );
124 installChecksums( artifactFile );
125 }
126
127 protected void addMetaDataFilesForArtifact( Artifact artifact, Collection<File> targetMetadataFiles, boolean createChecksum )
128 {
129 if ( !createChecksum )
130 {
131 return;
132 }
133
134 @SuppressWarnings( "unchecked" ) Collection<ArtifactMetadata> metadatas = artifact.getMetadataList();
135 if ( metadatas != null )
136 {
137 for ( ArtifactMetadata metadata : metadatas )
138 {
139 File metadataFile = getLocalRepoFile( metadata );
140 targetMetadataFiles.add( metadataFile );
141 }
142 }
143 }
144
145
146
147
148
149
150
151 protected void installChecksums( Collection<File> metadataFiles )
152 throws MojoExecutionException
153 {
154 for ( File metadataFile : metadataFiles )
155 {
156 installChecksums( metadataFile );
157 }
158 }
159
160
161
162
163
164
165
166
167 private void installChecksums( File installedFile )
168 throws MojoExecutionException
169 {
170 boolean signatureFile = installedFile.getName().endsWith( ".asc" );
171 if ( installedFile.isFile() && !signatureFile )
172 {
173
174 getLog().debug( "Calculating checksums for " + installedFile );
175 digester.calculate( installedFile );
176 installChecksum( installedFile, ".md5", digester.getMd5() );
177 installChecksum( installedFile, ".sha1", digester.getSha1() );
178 }
179 }
180
181
182
183
184
185
186
187
188
189
190
191 private void installChecksum( File installedFile, String ext, String checksum )
192 throws MojoExecutionException
193 {
194 File checksumFile = new File( installedFile.getAbsolutePath() + ext );
195 getLog().debug( "Installing checksum to " + checksumFile );
196 try
197 {
198
199 checksumFile.getParentFile().mkdirs();
200 FileUtils.fileWrite( checksumFile.getAbsolutePath(), "UTF-8", checksum );
201 }
202 catch ( IOException e )
203 {
204 throw new MojoExecutionException( "Failed to install checksum to " + checksumFile, e );
205 }
206 }
207
208 }