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