View Javadoc
1   package org.apache.maven.plugin.assembly.archive.task;
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.plugin.assembly.AssemblerConfigurationSource;
24  import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
25  import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
26  import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
27  import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.archiver.Archiver;
30  import org.codehaus.plexus.archiver.ArchiverException;
31  import org.codehaus.plexus.logging.Logger;
32  import org.codehaus.plexus.util.FileUtils;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  import java.io.File;
36  import java.io.IOException;
37  import java.util.List;
38  
39  /**
40   * @version $Id: AddArtifactTask.java 1633832 2014-10-23 15:03:47Z krosenvold $
41   */
42  public class AddArtifactTask
43  {
44  
45      public static final String[] DEFAULT_INCLUDES_ARRAY = { "**/*" };
46  
47      private int directoryMode = -1;
48  
49      private int fileMode = -1;
50  
51      private boolean unpack = false;
52  
53      private List<String> includes;
54  
55      private List<String> excludes;
56  
57      private final Artifact artifact;
58  
59      private MavenProject project;
60  
61      private MavenProject moduleProject;
62  
63      private Artifact moduleArtifact;
64  
65      private String outputDirectory;
66  
67      private String outputFileNameMapping;
68  
69      private final Logger logger;
70  
71      public AddArtifactTask( final Artifact artifact, final Logger logger )
72      {
73          this.artifact = artifact;
74          this.logger = logger;
75      }
76  
77      public void execute( final Archiver archiver, final AssemblerConfigurationSource configSource )
78          throws ArchiveCreationException, AssemblyFormattingException
79      {
80          // MASSEMBLY-282: We should support adding a project's standard output file as part of an assembly that replaces
81          // it.
82          if ( ( ( artifact.getFile() != null ) && ( archiver.getDestFile() != null ) )
83                          && artifact.getFile()
84                                     .equals( archiver.getDestFile() ) )
85          {
86              final File tempRoot = configSource.getTemporaryRootDirectory();
87              final File tempArtifactFile = new File( tempRoot, artifact.getFile()
88                                                                        .getName() );
89  
90              logger.warn( "Artifact: "
91                              + artifact.getId()
92                              + " references the same file as the assembly destination file. Moving it to a temporary location for inclusion." );
93              try
94              {
95                  FileUtils.copyFile( artifact.getFile(), tempArtifactFile );
96              }
97              catch ( final IOException e )
98              {
99                  throw new ArchiveCreationException( "Error moving artifact file: '" + artifact.getFile()
100                                 + "' to temporary location: " + tempArtifactFile + ". Reason: " + e.getMessage(), e );
101             }
102 
103             artifact.setFile( tempArtifactFile );
104         }
105 
106         String destDirectory = outputDirectory;
107 
108         destDirectory =
109             AssemblyFormatUtils.getOutputDirectory( destDirectory, configSource.getProject(), moduleProject, project,
110                                                     configSource.getFinalName(), configSource );
111 
112         if ( unpack )
113         {
114             String outputLocation = destDirectory;
115 
116             if ( ( outputLocation.length() > 0 ) && !outputLocation.endsWith( "/" ) )
117             {
118                 outputLocation += "/";
119             }
120 
121             String[] includesArray = TypeConversionUtils.toStringArray( includes );
122             if ( includesArray == null )
123             {
124                 includesArray = DEFAULT_INCLUDES_ARRAY;
125             }
126             final String[] excludesArray = TypeConversionUtils.toStringArray( excludes );
127 
128             final int oldDirMode = archiver.getOverrideDirectoryMode();
129             final int oldFileMode = archiver.getOverrideFileMode();
130 
131             logger.debug( "Unpacking artifact: " + artifact.getId() + " to assembly location: " + outputLocation + "." );
132 
133             boolean fileModeSet = false;
134             boolean dirModeSet = false;
135 
136             try
137             {
138                 if ( fileMode != -1 )
139                 {
140                     archiver.setFileMode( fileMode );
141                     fileModeSet = true;
142                 }
143 
144                 if ( directoryMode != -1 )
145                 {
146                     archiver.setDirectoryMode( directoryMode );
147                     dirModeSet = true;
148                 }
149 
150                 final File artifactFile = artifact.getFile();
151                 if ( artifactFile == null )
152                 {
153                     logger.warn( "Skipping artifact: " + artifact.getId()
154                                     + "; it does not have an associated file or directory." );
155                 }
156                 else if ( artifactFile.isDirectory() )
157                 {
158                     logger.debug( "Adding artifact directory contents for: " + artifact + " to: " + outputLocation );
159                     archiver.addDirectory( artifactFile, outputLocation, includesArray, excludesArray );
160                 }
161                 else
162                 {
163                     logger.debug( "Unpacking artifact contents for: " + artifact + " to: " + outputLocation );
164                     logger.debug( "includes:\n" + StringUtils.join( includesArray, "\n" ) + "\n" );
165                     logger.debug( "excludes:\n"
166                                     + ( excludesArray == null ? "none" : StringUtils.join( excludesArray, "\n" ) )
167                                     + "\n" );
168                     archiver.addArchivedFileSet( artifactFile, outputLocation, includesArray, excludesArray );
169                 }
170             }
171             catch ( final ArchiverException e )
172             {
173                 throw new ArchiveCreationException( "Error adding file-set for '" + artifact.getId() + "' to archive: "
174                                 + e.getMessage(), e );
175             }
176             finally
177             {
178                 if ( dirModeSet )
179                 {
180                     archiver.setDirectoryMode( oldDirMode );
181                 }
182 
183                 if ( fileModeSet )
184                 {
185                     archiver.setFileMode( oldFileMode );
186                 }
187             }
188         }
189         else
190         {
191             final String fileNameMapping =
192                 AssemblyFormatUtils.evaluateFileNameMapping( outputFileNameMapping, artifact,
193                                                              configSource.getProject(), moduleProject, moduleArtifact,
194                                                              project, configSource );
195 
196             final String outputLocation = destDirectory + fileNameMapping;
197 
198             try
199             {
200                 final File artifactFile = artifact.getFile();
201 
202                 logger.debug( "Adding artifact: " + artifact.getId() + " with file: " + artifactFile
203                                 + " to assembly location: " + outputLocation + "." );
204 
205                 if ( fileMode != -1 )
206                 {
207                     archiver.addFile( artifactFile, outputLocation, fileMode );
208                 }
209                 else
210                 {
211                     archiver.addFile( artifactFile, outputLocation );
212                 }
213             }
214             catch ( final ArchiverException e )
215             {
216                 throw new ArchiveCreationException( "Error adding file '" + artifact.getId() + "' to archive: "
217                                 + e.getMessage(), e );
218             }
219         }
220     }
221 
222     public void setDirectoryMode( final int directoryMode )
223     {
224         this.directoryMode = directoryMode;
225     }
226 
227     public void setFileMode( final int fileMode )
228     {
229         this.fileMode = fileMode;
230     }
231 
232     public void setExcludes( final List<String> excludes )
233     {
234         this.excludes = excludes;
235     }
236 
237     public void setIncludes( final List<String> includes )
238     {
239         this.includes = includes;
240     }
241 
242     public void setUnpack( final boolean unpack )
243     {
244         this.unpack = unpack;
245     }
246 
247     public void setProject( final MavenProject project )
248     {
249         this.project = project;
250     }
251 
252     public void setOutputDirectory( final String outputDirectory )
253     {
254         this.outputDirectory = outputDirectory;
255     }
256 
257     public void setFileNameMapping( final String outputFileNameMapping )
258     {
259         this.outputFileNameMapping = outputFileNameMapping;
260     }
261 
262     public void setOutputDirectory( final String outputDirectory, final String defaultOutputDirectory )
263     {
264         setOutputDirectory( outputDirectory == null ? defaultOutputDirectory : outputDirectory );
265     }
266 
267     public void setFileNameMapping( final String outputFileNameMapping, final String defaultOutputFileNameMapping )
268     {
269         setFileNameMapping( outputFileNameMapping == null ? defaultOutputFileNameMapping : outputFileNameMapping );
270     }
271 
272     public void setModuleProject( final MavenProject moduleProject )
273     {
274         this.moduleProject = moduleProject;
275     }
276 
277     public void setModuleArtifact( final Artifact moduleArtifact )
278     {
279         this.moduleArtifact = moduleArtifact;
280     }
281 
282 }