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