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.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 | |
|
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 | 11 | { |
58 | 11 | this.fileSets = fileSets; |
59 | 11 | } |
60 | |
|
61 | |
public AddFileSetsTask( final FileSet... fileSets ) |
62 | 0 | { |
63 | 0 | this.fileSets = new ArrayList<FileSet>( Arrays.asList( fileSets ) ); |
64 | 0 | } |
65 | |
|
66 | |
public void execute( final Archiver archiver, final AssemblerConfigurationSource configSource ) |
67 | |
throws ArchiveCreationException, AssemblyFormattingException |
68 | |
{ |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | 4 | final File archiveBaseDir = configSource.getArchiveBaseDirectory(); |
75 | |
|
76 | 4 | if ( archiveBaseDir != null ) |
77 | |
{ |
78 | 4 | if ( !archiveBaseDir.exists() ) |
79 | |
{ |
80 | 1 | throw new ArchiveCreationException( "The archive base directory '" + archiveBaseDir.getAbsolutePath() |
81 | |
+ "' does not exist" ); |
82 | |
} |
83 | 3 | else if ( !archiveBaseDir.isDirectory() ) |
84 | |
{ |
85 | 1 | throw new ArchiveCreationException( "The archive base directory '" + archiveBaseDir.getAbsolutePath() |
86 | |
+ "' exists, but it is not a directory" ); |
87 | |
} |
88 | |
} |
89 | |
|
90 | 2 | for ( final Iterator<FileSet> i = fileSets.iterator(); i.hasNext(); ) |
91 | |
{ |
92 | 2 | final FileSet fileSet = i.next(); |
93 | |
|
94 | 2 | addFileSet( fileSet, archiver, configSource, archiveBaseDir ); |
95 | 2 | } |
96 | 2 | } |
97 | |
|
98 | |
protected void addFileSet( final FileSet fileSet, final Archiver archiver, |
99 | |
final AssemblerConfigurationSource configSource, final File archiveBaseDir ) |
100 | |
throws AssemblyFormattingException, ArchiveCreationException |
101 | |
{ |
102 | |
|
103 | 5 | checkLogger(); |
104 | |
|
105 | 5 | final FileSetFormatter fileSetFormatter = new FileSetFormatter( configSource, logger ); |
106 | |
|
107 | 5 | if ( project == null ) |
108 | |
{ |
109 | 1 | project = configSource.getProject(); |
110 | |
} |
111 | |
|
112 | 5 | final File basedir = project.getBasedir(); |
113 | |
|
114 | 5 | String destDirectory = fileSet.getOutputDirectory(); |
115 | |
|
116 | 5 | if ( destDirectory == null ) |
117 | |
{ |
118 | 2 | destDirectory = fileSet.getDirectory(); |
119 | |
} |
120 | |
|
121 | 5 | destDirectory = |
122 | |
AssemblyFormatUtils.getOutputDirectory( destDirectory, configSource.getProject(), moduleProject, project, |
123 | |
configSource.getFinalName(), configSource ); |
124 | |
|
125 | 5 | if ( logger.isDebugEnabled() ) |
126 | |
{ |
127 | 5 | 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 | 5 | logger.debug( "The archive base directory is '" + archiveBaseDir + "'" ); |
134 | |
|
135 | 5 | File fileSetDir = getFileSetDirectory( fileSet, basedir, archiveBaseDir ); |
136 | |
|
137 | 5 | if ( fileSetDir.exists() ) |
138 | |
{ |
139 | |
try |
140 | |
{ |
141 | 2 | fileSetDir = fileSetFormatter.formatFileSetForAssembly( fileSetDir, fileSet ); |
142 | |
} |
143 | 0 | catch ( final IOException e ) |
144 | |
{ |
145 | 0 | throw new ArchiveCreationException( "Error fixing file-set line endings for assembly: " |
146 | |
+ e.getMessage(), e ); |
147 | 2 | } |
148 | |
|
149 | 2 | logger.debug( "Adding file-set from directory: '" + fileSetDir.getAbsolutePath() |
150 | |
+ "'\nassembly output directory is: \'" + destDirectory + "\'" ); |
151 | |
|
152 | 2 | final AddDirectoryTask task = new AddDirectoryTask( fileSetDir ); |
153 | |
|
154 | 2 | final int dirMode = TypeConversionUtils.modeToInt( fileSet.getDirectoryMode(), logger ); |
155 | 2 | if ( dirMode != -1 ) |
156 | |
{ |
157 | 0 | task.setDirectoryMode( dirMode ); |
158 | |
} |
159 | |
|
160 | 2 | final int fileMode = TypeConversionUtils.modeToInt( fileSet.getFileMode(), logger ); |
161 | 2 | if ( fileMode != -1 ) |
162 | |
{ |
163 | 0 | task.setFileMode( fileMode ); |
164 | |
} |
165 | |
|
166 | 2 | task.setUseDefaultExcludes( fileSet.isUseDefaultExcludes() ); |
167 | |
|
168 | 2 | final List<String> excludes = fileSet.getExcludes(); |
169 | 2 | excludes.add( "**/*.filtered" ); |
170 | 2 | excludes.add( "**/*.formatted" ); |
171 | 2 | task.setExcludes( excludes ); |
172 | |
|
173 | 2 | task.setIncludes( fileSet.getIncludes() ); |
174 | 2 | task.setOutputDirectory( destDirectory ); |
175 | |
|
176 | 2 | task.execute( archiver, configSource ); |
177 | |
} |
178 | 5 | } |
179 | |
|
180 | |
protected File getFileSetDirectory( final FileSet fileSet, final File basedir, final File archiveBaseDir ) |
181 | |
throws ArchiveCreationException, AssemblyFormattingException |
182 | |
{ |
183 | 9 | String sourceDirectory = fileSet.getDirectory(); |
184 | |
|
185 | 9 | if ( sourceDirectory == null || sourceDirectory.trim() |
186 | |
.length() < 1 ) |
187 | |
{ |
188 | 1 | sourceDirectory = basedir.getAbsolutePath(); |
189 | |
} |
190 | |
|
191 | 9 | File fileSetDir = null; |
192 | |
|
193 | 9 | if ( archiveBaseDir == null ) |
194 | |
{ |
195 | 3 | fileSetDir = new File( sourceDirectory ); |
196 | |
|
197 | 3 | if ( !fileSetDir.isAbsolute() ) |
198 | |
{ |
199 | 1 | fileSetDir = new File( basedir, sourceDirectory ); |
200 | |
} |
201 | |
} |
202 | |
else |
203 | |
{ |
204 | 6 | fileSetDir = new File( archiveBaseDir, sourceDirectory ); |
205 | |
} |
206 | |
|
207 | 9 | return fileSetDir; |
208 | |
} |
209 | |
|
210 | |
private void checkLogger() |
211 | |
{ |
212 | 5 | if ( logger == null ) |
213 | |
{ |
214 | 0 | logger = new ConsoleLogger( Logger.LEVEL_INFO, "AddFileSetsTask-internal" ); |
215 | |
} |
216 | 5 | } |
217 | |
|
218 | |
public void setLogger( final Logger logger ) |
219 | |
{ |
220 | 5 | this.logger = logger; |
221 | 5 | } |
222 | |
|
223 | |
public void setProject( final MavenProject project ) |
224 | |
{ |
225 | 4 | this.project = project; |
226 | 4 | } |
227 | |
|
228 | |
public MavenProject getModuleProject() |
229 | |
{ |
230 | 0 | return moduleProject; |
231 | |
} |
232 | |
|
233 | |
public void setModuleProject( final MavenProject moduleProject ) |
234 | |
{ |
235 | 1 | this.moduleProject = moduleProject; |
236 | 1 | } |
237 | |
|
238 | |
} |