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