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.project.artifact.ProjectArtifactMetadata;
27  
28  import java.io.File;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * Installs the project's main artifact in the local repository.
34   *
35   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
36   * @version $Id: InstallMojo.java 756223 2009-03-19 21:38:14Z bentmann $
37   * @goal install
38   * @phase install
39   */
40  public class InstallMojo
41      extends AbstractInstallMojo
42  {
43      /**
44       * @parameter default-value="${project.packaging}"
45       * @required
46       * @readonly
47       */
48      protected String packaging;
49  
50      /**
51       * @parameter default-value="${project.file}"
52       * @required
53       * @readonly
54       */
55      private File pomFile;
56  
57      /**
58       * @parameter default-value="${project.artifact}"
59       * @required
60       * @readonly
61       */
62      private Artifact artifact;
63  
64      /**
65       * @parameter default-value="${project.attachedArtifacts}
66       * @required
67       * @readonly
68       */
69      private List attachedArtifacts;
70  
71      public void execute()
72          throws MojoExecutionException
73      {
74          // TODO: push into transformation
75          boolean isPomArtifact = "pom".equals( packaging );
76  
77          ArtifactMetadata metadata = null;
78  
79          if ( updateReleaseInfo )
80          {
81              artifact.setRelease( true );
82          }
83  
84          try
85          {
86              if ( isPomArtifact )
87              {
88                  installer.install( pomFile, artifact, localRepository );
89                  installChecksums( artifact );
90              }
91              else
92              {
93                  metadata = new ProjectArtifactMetadata( artifact, pomFile );
94                  artifact.addMetadata( metadata );
95  
96                  File file = artifact.getFile();
97  
98                  // Here, we have a temporary solution to MINSTALL-3 (isDirectory() is true if it went through compile
99                  // but not package). We are designing in a proper solution for Maven 2.1
100                 if ( file != null && file.isFile() )
101                 {
102                     installer.install( file, artifact, localRepository );
103                     installChecksums( artifact );
104                 }
105                 else if ( !attachedArtifacts.isEmpty() )
106                 {
107                     getLog().info( "No primary artifact to install, installing attached artifacts instead." );
108 
109                     Artifact pomArtifact =
110                         artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
111                                                                artifact.getBaseVersion() );
112                     pomArtifact.setFile( pomFile );
113                     if ( updateReleaseInfo )
114                     {
115                         pomArtifact.setRelease( true );
116                     }
117 
118                     installer.install( pomFile, pomArtifact, localRepository );
119                     installChecksums( pomArtifact );
120                 }
121                 else
122                 {
123                     throw new MojoExecutionException(
124                         "The packaging for this project did not assign a file to the build artifact" );
125                 }
126             }
127 
128             for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
129             {
130                 Artifact attached = (Artifact) i.next();
131 
132                 installer.install( attached.getFile(), attached, localRepository );
133                 installChecksums( attached );
134             }
135         }
136         catch ( ArtifactInstallationException e )
137         {
138             throw new MojoExecutionException( e.getMessage(), e );
139         }
140     }
141 }