1 package org.apache.maven.shared.project.install.internal;
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 import java.util.Collections;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.metadata.ArtifactMetadata;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.project.ProjectBuildingRequest;
33 import org.apache.maven.project.artifact.ProjectArtifact;
34 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
35 import org.apache.maven.shared.artifact.install.ArtifactInstaller;
36 import org.apache.maven.shared.artifact.install.ArtifactInstallerException;
37 import org.apache.maven.shared.project.NoFileAssignedException;
38 import org.apache.maven.shared.project.install.ProjectInstaller;
39 import org.apache.maven.shared.project.install.ProjectInstallerRequest;
40 import org.apache.maven.shared.repository.RepositoryManager;
41 import org.codehaus.plexus.component.annotations.Component;
42 import org.codehaus.plexus.component.annotations.Requirement;
43 import org.codehaus.plexus.util.FileUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48
49
50
51
52 @Component( role = ProjectInstaller.class )
53 public class DefaultProjectInstaller
54 implements ProjectInstaller
55 {
56
57 private static final Logger LOGGER = LoggerFactory.getLogger( DefaultProjectInstaller.class );
58
59 @Requirement
60 private ArtifactInstaller installer;
61
62 @Requirement
63 private RepositoryManager repositoryManager;
64
65 private final DualDigester digester = new DualDigester();
66
67
68
69
70 @Override
71 public void install( ProjectBuildingRequest buildingRequest, ProjectInstallerRequest request )
72 throws IOException, ArtifactInstallerException, NoFileAssignedException
73 {
74
75 MavenProject project = request.getProject();
76 boolean createChecksum = request.isCreateChecksum();
77 boolean updateReleaseInfo = request.isUpdateReleaseInfo();
78
79 Artifact artifact = project.getArtifact();
80 String packaging = project.getPackaging();
81 File pomFile = project.getFile();
82
83 List<Artifact> attachedArtifacts = project.getAttachedArtifacts();
84
85
86 boolean isPomArtifact = "pom".equals( packaging );
87
88 ProjectArtifactMetadata metadata;
89
90 if ( updateReleaseInfo )
91 {
92 artifact.setRelease( true );
93 }
94
95 Collection<File> metadataFiles = new LinkedHashSet<File>();
96
97 if ( isPomArtifact )
98 {
99 if ( pomFile != null )
100 {
101 installer.install( buildingRequest,
102 Collections.<Artifact>singletonList( new ProjectArtifact( project ) ) );
103 installChecksums( buildingRequest, artifact, createChecksum );
104 addMetaDataFilesForArtifact( buildingRequest, artifact, metadataFiles, createChecksum );
105 }
106 }
107 else
108 {
109 if ( pomFile != null )
110 {
111 metadata = new ProjectArtifactMetadata( artifact, pomFile );
112 artifact.addMetadata( metadata );
113 }
114
115 File file = artifact.getFile();
116
117
118
119 if ( file != null && file.isFile() )
120 {
121 installer.install( buildingRequest, Collections.<Artifact>singletonList( artifact ) );
122 installChecksums( buildingRequest, artifact, createChecksum );
123 addMetaDataFilesForArtifact( buildingRequest, artifact, metadataFiles, createChecksum );
124 }
125 else if ( !attachedArtifacts.isEmpty() )
126 {
127 throw new NoFileAssignedException( "The packaging plugin for this project did not assign "
128 + "a main file to the project but it has attachments. Change packaging to 'pom'." );
129 }
130 else
131 {
132
133 throw new NoFileAssignedException( "The packaging for this project did not assign a file to the build artifact" );
134
135 }
136 }
137
138 for ( Artifact attached : attachedArtifacts )
139 {
140 installer.install( buildingRequest, Collections.singletonList( attached ) );
141 installChecksums( buildingRequest, attached, createChecksum );
142 addMetaDataFilesForArtifact( buildingRequest, attached, metadataFiles, createChecksum );
143 }
144
145 installChecksums( metadataFiles );
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160 private void installChecksums( ProjectBuildingRequest buildingRequest, Artifact artifact, boolean createChecksum )
161 throws IOException
162 {
163 if ( !createChecksum )
164 {
165 return;
166 }
167
168 File artifactFile = getLocalRepoFile( buildingRequest, artifact );
169 installChecksums( artifactFile );
170 }
171
172
173 private void addMetaDataFilesForArtifact( ProjectBuildingRequest buildingRequest, Artifact artifact,
174 Collection<File> targetMetadataFiles, boolean createChecksum )
175
176 {
177 if ( !createChecksum )
178 {
179 return;
180 }
181
182 Collection<ArtifactMetadata> metadatas = artifact.getMetadataList();
183 if ( metadatas != null )
184 {
185 for ( ArtifactMetadata metadata : metadatas )
186 {
187 File metadataFile = getLocalRepoFile( buildingRequest, metadata );
188 targetMetadataFiles.add( metadataFile );
189 }
190 }
191 }
192
193
194
195
196
197
198
199 private void installChecksums( Collection<File> metadataFiles )
200 throws IOException
201 {
202 for ( File metadataFile : metadataFiles )
203 {
204 installChecksums( metadataFile );
205 }
206 }
207
208
209
210
211
212
213
214
215 private void installChecksums( File installedFile )
216 throws IOException
217 {
218 boolean signatureFile = installedFile.getName().endsWith( ".asc" );
219 if ( installedFile.isFile() && !signatureFile )
220 {
221 LOGGER.debug( "Calculating checksums for " + installedFile );
222 digester.calculate( installedFile );
223 installChecksum( installedFile, ".md5", digester.getMd5() );
224 installChecksum( installedFile, ".sha1", digester.getSha1() );
225 }
226 }
227
228
229
230
231
232
233
234
235
236
237
238 private void installChecksum( File installedFile, String ext, String checksum )
239 throws IOException
240 {
241 File checksumFile = new File( installedFile.getAbsolutePath() + ext );
242 LOGGER.debug( "Installing checksum to " + checksumFile );
243 try
244 {
245
246 checksumFile.getParentFile().mkdirs();
247 FileUtils.fileWrite( checksumFile.getAbsolutePath(), "UTF-8", checksum );
248 }
249 catch ( IOException e )
250 {
251 throw new IOException( "Failed to install checksum to " + checksumFile, e );
252 }
253 }
254
255
256
257
258
259
260
261
262
263 private File getLocalRepoFile( ProjectBuildingRequest buildingRequest, Artifact artifact )
264 {
265 String path = repositoryManager.getPathForLocalArtifact( buildingRequest, artifact );
266 return new File( repositoryManager.getLocalRepositoryBasedir( buildingRequest ), path );
267 }
268
269
270
271
272
273
274
275
276
277 private File getLocalRepoFile( ProjectBuildingRequest buildingRequest, ArtifactMetadata metadata )
278 {
279 String path = repositoryManager.getPathForLocalMetadata( buildingRequest, metadata );
280 return new File( repositoryManager.getLocalRepositoryBasedir( buildingRequest ), path );
281 }
282
283 }