1 package org.apache.maven.shared.transfer.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.transfer.artifact.install.ArtifactInstaller;
36 import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
37 import org.apache.maven.shared.transfer.project.MavenAetherUtils;
38 import org.apache.maven.shared.transfer.project.NoFileAssignedException;
39 import org.apache.maven.shared.transfer.project.install.ProjectInstaller;
40 import org.apache.maven.shared.transfer.project.install.ProjectInstallerRequest;
41 import org.apache.maven.shared.transfer.repository.RepositoryManager;
42 import org.codehaus.plexus.component.annotations.Component;
43 import org.codehaus.plexus.component.annotations.Requirement;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48
49
50
51
52 @Component( role = ProjectInstaller.class )
53 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
66
67
68 @Override
69 public void install( ProjectBuildingRequest buildingRequest, ProjectInstallerRequest installerRequest )
70 throws IOException, ArtifactInstallerException, NoFileAssignedException, IllegalArgumentException
71 {
72
73 validateParameters( buildingRequest, installerRequest );
74
75 MavenAetherUtils.importAetherLibrary();
76
77 MavenProject project = installerRequest.getProject();
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 Collection<File> metadataFiles = new LinkedHashSet<File>();
91
92 if ( isPomArtifact )
93 {
94 if ( pomFile != null )
95 {
96 installer.install( buildingRequest,
97 Collections.<Artifact>singletonList( new ProjectArtifact( project ) ) );
98 addMetaDataFilesForArtifact( buildingRequest, artifact, metadataFiles );
99 }
100 }
101 else
102 {
103 if ( pomFile != null )
104 {
105 metadata = new ProjectArtifactMetadata( artifact, pomFile );
106 artifact.addMetadata( metadata );
107 }
108
109 File file = artifact.getFile();
110
111
112
113 if ( file != null && file.isFile() )
114 {
115 installer.install( buildingRequest, Collections.<Artifact>singletonList( artifact ) );
116 addMetaDataFilesForArtifact( buildingRequest, artifact, metadataFiles );
117 }
118 else if ( !attachedArtifacts.isEmpty() )
119 {
120 throw new NoFileAssignedException( "The packaging plugin for this project did not assign "
121 + "a main file to the project but it has attachments. Change packaging to 'pom'." );
122 }
123 else
124 {
125
126 throw new NoFileAssignedException( "The packaging for this project did not assign a file to the build artifact" );
127
128 }
129 }
130
131 for ( Artifact attached : attachedArtifacts )
132 {
133 LOGGER.debug( "Installing artifact: ", attached.getId() );
134 installer.install( buildingRequest, Collections.singletonList( attached ) );
135 addMetaDataFilesForArtifact( buildingRequest, attached, metadataFiles );
136 }
137
138 }
139
140 private void validateParameters( ProjectBuildingRequest buildingRequest, ProjectInstallerRequest installerRequest )
141 {
142 if ( buildingRequest == null )
143 {
144 throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
145 }
146 if ( installerRequest == null )
147 {
148 throw new IllegalArgumentException( "The parameter installerRequest is not allowed to be null." );
149 }
150 }
151
152
153 private void addMetaDataFilesForArtifact( ProjectBuildingRequest buildingRequest, Artifact artifact,
154 Collection<File> targetMetadataFiles )
155
156 {
157 Collection<ArtifactMetadata> metadatas = artifact.getMetadataList();
158 if ( metadatas != null )
159 {
160 for ( ArtifactMetadata metadata : metadatas )
161 {
162 File metadataFile = getLocalRepoFile( buildingRequest, metadata );
163 targetMetadataFiles.add( metadataFile );
164 }
165 }
166 }
167
168
169
170
171
172
173
174
175
176 private File getLocalRepoFile( ProjectBuildingRequest buildingRequest, ArtifactMetadata metadata )
177 {
178 String path = repositoryManager.getPathForLocalMetadata( buildingRequest, metadata );
179 return new File( repositoryManager.getLocalRepositoryBasedir( buildingRequest ), path );
180 }
181
182 }