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 java.io.File;
23  import java.io.IOException;
24  import java.util.Collection;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.factory.ArtifactFactory;
28  import org.apache.maven.artifact.installer.ArtifactInstaller;
29  import org.apache.maven.artifact.metadata.ArtifactMetadata;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.plugin.AbstractMojo;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugins.annotations.Component;
34  import org.apache.maven.plugins.annotations.Parameter;
35  import org.apache.maven.shared.utils.io.FileUtils;
36  
37  /**
38   * Common fields for installation mojos.
39   *
40   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
41   * @version $Id: AbstractInstallMojo.java 1530417 2013-10-08 20:49:56Z rfscholte $
42   */
43  public abstract class AbstractInstallMojo
44      extends AbstractMojo
45  {
46  
47      /**
48       */
49      @Component
50      protected ArtifactFactory artifactFactory;
51  
52      /**
53       */
54      @Component
55      protected ArtifactInstaller installer;
56  
57      /**
58       */
59      @Parameter( property = "localRepository", required = true, readonly = true )
60      protected ArtifactRepository localRepository;
61  
62      /**
63       * Flag whether to create checksums (MD5, SHA-1) or not.
64       *
65       * @since 2.2
66       */
67      @Parameter( property = "createChecksum", defaultValue = "false" )
68      protected boolean createChecksum;
69  
70      /**
71       * Whether to update the metadata to make the artifact a release version.
72       */
73      @Parameter( property = "updateReleaseInfo", defaultValue = "false" )
74      protected boolean updateReleaseInfo;
75  
76      protected final DualDigester digester = new DualDigester();
77  
78      /**
79       * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist
80       * (yet).
81       *
82       * @param artifact The artifact whose local repo path should be determined, must not be <code>null</code>.
83       * @return The absolute path to the artifact when installed, never <code>null</code>.
84       */
85      protected File getLocalRepoFile( Artifact artifact )
86      {
87          String path = localRepository.pathOf( artifact );
88          return new File( localRepository.getBasedir(), path );
89      }
90  
91      /**
92       * Gets the path of the specified artifact metadata within the local repository. Note that the returned path need
93       * not exist (yet).
94       *
95       * @param metadata The artifact metadata whose local repo path should be determined, must not be <code>null</code>.
96       * @return The absolute path to the artifact metadata when installed, never <code>null</code>.
97       */
98      protected File getLocalRepoFile( ArtifactMetadata metadata )
99      {
100         String path = localRepository.pathOfLocalRepositoryMetadata( metadata, localRepository );
101         return new File( localRepository.getBasedir(), path );
102     }
103 
104     /**
105      * Installs the checksums for the specified artifact if this has been enabled in the plugin configuration. This
106      * method creates checksums for files that have already been installed to the local repo to account for on-the-fly
107      * generated/updated files. For example, in Maven 2.0.4- the <code>ProjectArtifactMetadata</code> did not install
108      * the original POM file (cf. MNG-2820). While the plugin currently requires Maven 2.0.6, we continue to hash the
109      * installed POM for robustness with regard to future changes like re-introducing some kind of POM filtering.
110      *
111      * @param artifact The artifact for which to create checksums, must not be <code>null</code>.
112      * @param createChecksum {@code true} if checksum should be created, otherwise {@code false}.
113      * @throws MojoExecutionException If the checksums could not be installed.
114      */
115     protected void installChecksums( Artifact artifact, boolean createChecksum )
116         throws MojoExecutionException
117     {
118         if ( !createChecksum )
119         {
120             return;
121         }
122 
123         File artifactFile = getLocalRepoFile( artifact );
124         installChecksums( artifactFile );
125     }
126 
127     protected void addMetaDataFilesForArtifact( Artifact artifact, Collection<File> targetMetadataFiles, boolean createChecksum )
128     {
129         if ( !createChecksum )
130         {
131             return;
132         }
133 
134         @SuppressWarnings( "unchecked" ) Collection<ArtifactMetadata> metadatas = artifact.getMetadataList();
135         if ( metadatas != null )
136         {
137             for ( ArtifactMetadata metadata : metadatas )
138             {
139                 File metadataFile = getLocalRepoFile( metadata );
140                 targetMetadataFiles.add( metadataFile );
141             }
142         }
143     }
144 
145     /**
146      * Installs the checksums for the specified metadata files.
147      *
148      * @param metadataFiles The collection of metadata files to install checksums for, must not be <code>null</code>.
149      * @throws MojoExecutionException If the checksums could not be installed.
150      */
151     protected void installChecksums( Collection<File> metadataFiles )
152         throws MojoExecutionException
153     {
154         for ( File metadataFile : metadataFiles )
155         {
156             installChecksums( metadataFile );
157         }
158     }
159 
160     /**
161      * Installs the checksums for the specified file (if it exists).
162      *
163      * @param installedFile The path to the already installed file in the local repo for which to generate checksums,
164      *                      must not be <code>null</code>.
165      * @throws MojoExecutionException If the checksums could not be installed.
166      */
167     private void installChecksums( File installedFile )
168         throws MojoExecutionException
169     {
170         boolean signatureFile = installedFile.getName().endsWith( ".asc" );
171         if ( installedFile.isFile() && !signatureFile )
172         {
173 
174             getLog().debug( "Calculating checksums for " + installedFile );
175             digester.calculate( installedFile );
176             installChecksum( installedFile, ".md5", digester.getMd5() );
177             installChecksum( installedFile, ".sha1", digester.getSha1() );
178         }
179     }
180 
181     /**
182      * Installs a checksum for the specified file.
183      *
184      * @param installedFile The base path from which the path to the checksum files is derived by appending the given
185      *                      file extension, must not be <code>null</code>.
186      * @param ext           The file extension (including the leading dot) to use for the checksum file, must not be
187      *                      <code>null</code>.
188      * @param checksum      the checksum to write
189      * @throws MojoExecutionException If the checksum could not be installed.
190      */
191     private void installChecksum( File installedFile, String ext, String checksum )
192         throws MojoExecutionException
193     {
194         File checksumFile = new File( installedFile.getAbsolutePath() + ext );
195         getLog().debug( "Installing checksum to " + checksumFile );
196         try
197         {
198             //noinspection ResultOfMethodCallIgnored
199             checksumFile.getParentFile().mkdirs();
200             FileUtils.fileWrite( checksumFile.getAbsolutePath(), "UTF-8", checksum );
201         }
202         catch ( IOException e )
203         {
204             throw new MojoExecutionException( "Failed to install checksum to " + checksumFile, e );
205         }
206     }
207 
208 }