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.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   * This will install a whole project into the appropriate repository.
49   * 
50   * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>
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       * {@inheritDoc}
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          // TODO: push into transformation
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             // Here, we have a temporary solution to MINSTALL-3 (isDirectory() is true if it went through compile
112             // but not package). We are designing in a proper solution for Maven 2.1
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                 // CHECKSTYLE_OFF: LineLength
126                 throw new NoFileAssignedException( "The packaging for this project did not assign a file to the build artifact" );
127                 // CHECKSTYLE_ON: LineLength
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     // CHECKSTYLE_OFF: LineLength
153     private void addMetaDataFilesForArtifact( ProjectBuildingRequest buildingRequest, Artifact artifact,
154                                               Collection<File> targetMetadataFiles )
155     // CHECKSTYLE_ON: LineLength
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      * Gets the path of the specified artifact metadata within the local repository. Note that the returned path need
170      * not exist (yet).
171      *
172      * @param buildingRequest The project building request, must not be <code>null</code>.
173      * @param metadata The artifact metadata whose local repo path should be determined, must not be <code>null</code>.
174      * @return The absolute path to the artifact metadata when installed, never <code>null</code>.
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 }