View Javadoc
1   package org.apache.maven.shared.transfer.project.install.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * This will install a whole project into the appropriate repository.
48   * 
49   * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>
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       * {@inheritDoc}
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          // TODO: push into transformation
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             // Here, we have a temporary solution to MINSTALL-3 (isDirectory() is true if it went through compile
108             // but not package). We are designing in a proper solution for Maven 2.1
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                 // CHECKSTYLE_OFF: LineLength
122                 throw new NoFileAssignedException( "The packaging for this project did not assign a file to the build artifact" );
123                 // CHECKSTYLE_ON: LineLength
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      * Gets the path of the specified artifact metadata within the local repository. Note that the returned path need
164      * not exist (yet).
165      *
166      * @param buildingRequest The project building request, must not be <code>null</code>.
167      * @param metadata The artifact metadata whose local repo path should be determined, must not be <code>null</code>.
168      * @return The absolute path to the artifact metadata when installed, never <code>null</code>.
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 }