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.plugin.assembly.AssemblerConfigurationSource;
23  import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
24  import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
25  import org.apache.maven.plugin.assembly.format.FileSetFormatter;
26  import org.apache.maven.plugin.assembly.model.FileSet;
27  import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
28  import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.archiver.Archiver;
31  import org.codehaus.plexus.logging.Logger;
32  import org.codehaus.plexus.logging.console.ConsoleLogger;
33  
34  import java.io.File;
35  import java.io.IOException;
36  import java.util.ArrayList;
37  import java.util.Arrays;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   * @version $Id: AddFileSetsTask.java 999624 2010-09-21 20:40:03Z jdcasey $
43   */
44  public class AddFileSetsTask
45      implements ArchiverTask
46  {
47  
48      private final List<FileSet> fileSets;
49  
50      private Logger logger;
51  
52      private MavenProject project;
53  
54      private MavenProject moduleProject;
55  
56      public AddFileSetsTask( final List<FileSet> fileSets )
57      {
58          this.fileSets = fileSets;
59      }
60  
61      public AddFileSetsTask( final FileSet... fileSets )
62      {
63          this.fileSets = new ArrayList<FileSet>( Arrays.asList( fileSets ) );
64      }
65  
66      public void execute( final Archiver archiver, final AssemblerConfigurationSource configSource )
67          throws ArchiveCreationException, AssemblyFormattingException
68      {
69          // don't need this check here. it's more efficient here, but the logger is not actually
70          // used until addFileSet(..)...and the check should be there in case someone extends the
71          // class.
72          // checkLogger();
73  
74          final File archiveBaseDir = configSource.getArchiveBaseDirectory();
75  
76          if ( archiveBaseDir != null )
77          {
78              if ( !archiveBaseDir.exists() )
79              {
80                  throw new ArchiveCreationException( "The archive base directory '" + archiveBaseDir.getAbsolutePath()
81                                  + "' does not exist" );
82              }
83              else if ( !archiveBaseDir.isDirectory() )
84              {
85                  throw new ArchiveCreationException( "The archive base directory '" + archiveBaseDir.getAbsolutePath()
86                                  + "' exists, but it is not a directory" );
87              }
88          }
89  
90          for ( final Iterator<FileSet> i = fileSets.iterator(); i.hasNext(); )
91          {
92              final FileSet fileSet = i.next();
93  
94              addFileSet( fileSet, archiver, configSource, archiveBaseDir );
95          }
96      }
97  
98      protected void addFileSet( final FileSet fileSet, final Archiver archiver,
99                                 final AssemblerConfigurationSource configSource, final File archiveBaseDir )
100         throws AssemblyFormattingException, ArchiveCreationException
101     {
102         // throw this check in just in case someone extends this class...
103         checkLogger();
104 
105         final FileSetFormatter fileSetFormatter = new FileSetFormatter( configSource, logger );
106 
107         if ( project == null )
108         {
109             project = configSource.getProject();
110         }
111 
112         final File basedir = project.getBasedir();
113 
114         String destDirectory = fileSet.getOutputDirectory();
115 
116         if ( destDirectory == null )
117         {
118             destDirectory = fileSet.getDirectory();
119         }
120 
121         destDirectory =
122             AssemblyFormatUtils.getOutputDirectory( destDirectory, configSource.getProject(), moduleProject, project,
123                                                     configSource.getFinalName(), configSource );
124 
125         if ( logger.isDebugEnabled() )
126         {
127             logger.debug( "FileSet[" + destDirectory + "]" + " dir perms: "
128                             + Integer.toString( archiver.getOverrideDirectoryMode(), 8 ) + " file perms: "
129                             + Integer.toString( archiver.getOverrideFileMode(), 8 )
130                             + ( fileSet.getLineEnding() == null ? "" : " lineEndings: " + fileSet.getLineEnding() ) );
131         }
132 
133         logger.debug( "The archive base directory is '" + archiveBaseDir + "'" );
134 
135         File fileSetDir = getFileSetDirectory( fileSet, basedir, archiveBaseDir );
136 
137         if ( fileSetDir.exists() )
138         {
139             try
140             {
141                 fileSetDir = fileSetFormatter.formatFileSetForAssembly( fileSetDir, fileSet );
142             }
143             catch ( final IOException e )
144             {
145                 throw new ArchiveCreationException( "Error fixing file-set line endings for assembly: "
146                                 + e.getMessage(), e );
147             }
148 
149             logger.debug( "Adding file-set from directory: '" + fileSetDir.getAbsolutePath()
150                             + "'\nassembly output directory is: \'" + destDirectory + "\'" );
151 
152             final AddDirectoryTask task = new AddDirectoryTask( fileSetDir );
153 
154             final int dirMode = TypeConversionUtils.modeToInt( fileSet.getDirectoryMode(), logger );
155             if ( dirMode != -1 )
156             {
157                 task.setDirectoryMode( dirMode );
158             }
159 
160             final int fileMode = TypeConversionUtils.modeToInt( fileSet.getFileMode(), logger );
161             if ( fileMode != -1 )
162             {
163                 task.setFileMode( fileMode );
164             }
165 
166             task.setUseDefaultExcludes( fileSet.isUseDefaultExcludes() );
167 
168             final List<String> excludes = fileSet.getExcludes();
169             excludes.add( "**/*.filtered" );
170             excludes.add( "**/*.formatted" );
171             task.setExcludes( excludes );
172 
173             task.setIncludes( fileSet.getIncludes() );
174             task.setOutputDirectory( destDirectory );
175 
176             task.execute( archiver, configSource );
177         }
178     }
179 
180     protected File getFileSetDirectory( final FileSet fileSet, final File basedir, final File archiveBaseDir )
181         throws ArchiveCreationException, AssemblyFormattingException
182     {
183         String sourceDirectory = fileSet.getDirectory();
184 
185         if ( sourceDirectory == null || sourceDirectory.trim()
186                                                        .length() < 1 )
187         {
188             sourceDirectory = basedir.getAbsolutePath();
189         }
190 
191         File fileSetDir = null;
192 
193         if ( archiveBaseDir == null )
194         {
195             fileSetDir = new File( sourceDirectory );
196 
197             if ( !fileSetDir.isAbsolute() )
198             {
199                 fileSetDir = new File( basedir, sourceDirectory );
200             }
201         }
202         else
203         {
204             fileSetDir = new File( archiveBaseDir, sourceDirectory );
205         }
206 
207         return fileSetDir;
208     }
209 
210     private void checkLogger()
211     {
212         if ( logger == null )
213         {
214             logger = new ConsoleLogger( Logger.LEVEL_INFO, "AddFileSetsTask-internal" );
215         }
216     }
217 
218     public void setLogger( final Logger logger )
219     {
220         this.logger = logger;
221     }
222 
223     public void setProject( final MavenProject project )
224     {
225         this.project = project;
226     }
227 
228     public MavenProject getModuleProject()
229     {
230         return moduleProject;
231     }
232 
233     public void setModuleProject( final MavenProject moduleProject )
234     {
235         this.moduleProject = moduleProject;
236     }
237 
238 }