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