View Javadoc

1   package org.apache.maven.plugin.install;
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.installer.ArtifactInstallationException;
24  import org.apache.maven.artifact.metadata.ArtifactMetadata;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.LifecyclePhase;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
30  
31  import java.io.File;
32  import java.util.Collection;
33  import java.util.Iterator;
34  import java.util.LinkedHashSet;
35  import java.util.List;
36  
37  /**
38   * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the local repository.
39   *
40   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
41   * @version $Id: InstallMojo.html 831044 2012-09-03 20:19:45Z olamy $
42   */
43  @Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
44  public class InstallMojo
45      extends AbstractInstallMojo
46  {
47      /**
48       */
49      @Parameter( defaultValue = "${project.packaging}", required = true, readonly = true )
50      protected String packaging;
51  
52      /**
53       */
54      @Parameter( defaultValue = "${project.file}", required = true, readonly = true )
55      private File pomFile;
56  
57      /**
58       * Set this to <code>true</code> to bypass artifact installation.
59       * Use this for artifacts that does not need to be installed in the local repository.
60       *
61       * @since 2.4
62       */
63      @Parameter( property = "maven.install.skip", defaultValue = "false", required = true )
64      private boolean skip;
65  
66      /**
67       */
68      @Parameter( defaultValue = "${project.artifact}", required = true, readonly = true )
69      private Artifact artifact;
70  
71      /**
72       */
73      @Parameter( defaultValue = "${project.attachedArtifacts}", required = true, readonly = true )
74      private List attachedArtifacts;
75  
76      public void execute()
77          throws MojoExecutionException
78      {
79          if ( skip )
80          {
81              getLog().info( "Skipping artifact installation" );
82              return;
83          }
84  
85          // TODO: push into transformation
86          boolean isPomArtifact = "pom".equals( packaging );
87  
88          ArtifactMetadata metadata = null;
89  
90          if ( updateReleaseInfo )
91          {
92              artifact.setRelease( true );
93          }
94  
95          try
96          {
97              Collection metadataFiles = new LinkedHashSet();
98  
99              if ( isPomArtifact )
100             {
101                 installer.install( pomFile, artifact, localRepository );
102                 installChecksums( artifact, metadataFiles );
103             }
104             else
105             {
106                 metadata = new ProjectArtifactMetadata( artifact, pomFile );
107                 artifact.addMetadata( metadata );
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( file, artifact, localRepository );
116                     installChecksums( artifact, metadataFiles );
117                 }
118                 else if ( !attachedArtifacts.isEmpty() )
119                 {
120                     getLog().info( "No primary artifact to install, installing attached artifacts instead." );
121 
122                     Artifact pomArtifact =
123                         artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
124                                                                artifact.getBaseVersion() );
125                     pomArtifact.setFile( pomFile );
126                     if ( updateReleaseInfo )
127                     {
128                         pomArtifact.setRelease( true );
129                     }
130 
131                     installer.install( pomFile, pomArtifact, localRepository );
132                     installChecksums( pomArtifact, metadataFiles );
133                 }
134                 else
135                 {
136                     throw new MojoExecutionException(
137                         "The packaging for this project did not assign a file to the build artifact" );
138                 }
139             }
140 
141             for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
142             {
143                 Artifact attached = (Artifact) i.next();
144 
145                 installer.install( attached.getFile(), attached, localRepository );
146                 installChecksums( attached, metadataFiles );
147             }
148 
149             installChecksums( metadataFiles );
150         }
151         catch ( ArtifactInstallationException e )
152         {
153             throw new MojoExecutionException( e.getMessage(), e );
154         }
155     }
156 
157     public void setSkip( boolean skip )
158     {
159         this.skip = skip;
160     }
161 }