1 | |
|
2 | |
|
3 | |
|
4 | |
package com.aurea.maven.plugins.sonic.esb; |
5 | |
|
6 | |
import java.io.File; |
7 | |
import java.io.FileInputStream; |
8 | |
import java.io.FileNotFoundException; |
9 | |
import java.io.FileOutputStream; |
10 | |
import java.io.IOException; |
11 | |
import java.util.Collections; |
12 | |
import java.util.Enumeration; |
13 | |
import java.util.Properties; |
14 | |
import java.util.Vector; |
15 | |
|
16 | |
import org.apache.maven.artifact.handler.ArtifactHandler; |
17 | |
import org.apache.maven.execution.MavenSession; |
18 | |
import org.apache.maven.plugin.BuildPluginManager; |
19 | |
import org.apache.maven.plugin.MojoExecutionException; |
20 | |
import org.apache.maven.plugin.MojoFailureException; |
21 | |
import org.codehaus.plexus.archiver.ArchiverException; |
22 | |
import org.codehaus.plexus.archiver.zip.ZipArchiver; |
23 | |
import org.codehaus.plexus.util.FileUtils; |
24 | |
import org.twdata.maven.mojoexecutor.MojoExecutor; |
25 | |
|
26 | |
import com.aurea.maven.plugins.sonic.utils.FileUtilities; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | public class ConnectPackageMojo extends EsbPackageMojo { |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
private MavenSession session; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
protected BuildPluginManager pluginManager; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
ArtifactHandler handler; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
protected String connectSpringLocation; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
protected String connectPropertiesLocation; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
protected String connectServicesFolder; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
@Override |
101 | |
protected void doExecute() throws MojoExecutionException, MojoFailureException { |
102 | |
|
103 | 0 | getLog().info("Packaging Sonic Connect artifact"); |
104 | 0 | project.getArtifact().setArtifactHandler(handler); |
105 | |
|
106 | |
|
107 | |
try { |
108 | 0 | createConnectFiles(); |
109 | 0 | copyLibFilesToPackageDir(); |
110 | |
|
111 | 0 | super.doExecute(); |
112 | |
|
113 | 0 | } catch (final MojoExecutionException e) { |
114 | 0 | getLog().error("Error packaging Sonic Connect artifact : " + e.getMessage(), e); |
115 | 0 | throw new MojoExecutionException("Error packaging Sonic Connect artifact : ", e); |
116 | 0 | } |
117 | 0 | } |
118 | |
|
119 | |
private void copyLibFilesToPackageDir() throws MojoExecutionException { |
120 | |
|
121 | 0 | final String targetLibDir = getPackageXarDir() + "/SonicFS/workspace/" + getProjectName() + "/lib"; |
122 | |
try { |
123 | 0 | FileUtilities.copyFiles(project.getBasedir() + "/lib", targetLibDir); |
124 | 0 | } catch (final Exception e) { |
125 | |
|
126 | 0 | e.printStackTrace(); |
127 | 0 | throw new MojoExecutionException("Error copy files from lib directory: " + e.getMessage(), e); |
128 | 0 | } |
129 | |
|
130 | 0 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
protected void createConnectFiles() throws MojoExecutionException { |
139 | |
|
140 | 0 | final String packagedConnectFileTemplate = project.getBasedir() + "/lib/" + getProjectName(); |
141 | 0 | final File connectCsarFile = new File(packagedConnectFileTemplate + ".csar"); |
142 | 0 | final File connectPropertiesFile = new File(packagedConnectFileTemplate + ".properties"); |
143 | 0 | final String baseDir = project.getBasedir().getAbsolutePath(); |
144 | |
|
145 | |
|
146 | 0 | connectCsarFile.getParentFile().mkdirs(); |
147 | |
|
148 | |
|
149 | 0 | connectCsarFile.delete(); |
150 | 0 | connectPropertiesFile.delete(); |
151 | |
|
152 | 0 | refreshDependenciesInLib(); |
153 | |
|
154 | |
|
155 | |
try { |
156 | 0 | final ZipArchiver za = new ZipArchiver(); |
157 | 0 | za.setDestFile(connectCsarFile); |
158 | |
|
159 | |
|
160 | 0 | za.addFile(new File(baseDir, connectSpringLocation), "config/spring.xml"); |
161 | |
|
162 | |
|
163 | 0 | final String servicesDir = new File(baseDir, connectServicesFolder).getPath(); |
164 | 0 | final String[] esbCamelFiles = FileUtilities.getFileList(servicesDir, "**/*.esbcamel", null); |
165 | 0 | for (String fileName : esbCamelFiles) { |
166 | 0 | final File file = new File(servicesDir, fileName); |
167 | 0 | za.addFile(file, ".services/" + fileName); |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | 0 | final String[] wsdlFileNames = FileUtilities.getFileList(baseDir, |
173 | |
"**/*.wsdl,**/*.xsd", null); |
174 | 0 | for (final String fileName : wsdlFileNames) { |
175 | 0 | za.addFile(new File(baseDir, fileName), fileName); |
176 | |
} |
177 | |
|
178 | |
|
179 | 0 | final String jarFileName = project.getArtifactId() + "-" + project.getVersion() + ".jar"; |
180 | 0 | za.addFile(new File(project.getBuild().getDirectory(), jarFileName), jarFileName); |
181 | |
|
182 | |
|
183 | 0 | final String[] dependentJars = { |
184 | |
"*.jar" |
185 | |
}; |
186 | 0 | za.addDirectory(new File(baseDir, "lib"), dependentJars, null); |
187 | |
|
188 | 0 | za.createArchive(); |
189 | 0 | } catch (final ArchiverException e) { |
190 | 0 | throw new MojoExecutionException(e.getMessage(), e); |
191 | 0 | } catch (final IOException e) { |
192 | 0 | throw new MojoExecutionException(e.getMessage(), e); |
193 | 0 | } |
194 | |
|
195 | |
|
196 | |
|
197 | 0 | final SortedProperties completePropertyList = new SortedProperties(); |
198 | |
try { |
199 | |
|
200 | 0 | final String connectProperties = connectPropertiesLocation != null ? connectPropertiesLocation : |
201 | |
project.getArtifactId() + ".properties"; |
202 | 0 | final String[] topLevelPropertiesFileNames = FileUtilities.getFileList(baseDir, |
203 | |
connectProperties, null); |
204 | 0 | for (final String fileName : topLevelPropertiesFileNames) { |
205 | 0 | final Properties props = new Properties(); |
206 | 0 | props.load(new FileInputStream(new File(baseDir, fileName))); |
207 | 0 | completePropertyList.putAll(props); |
208 | |
} |
209 | |
|
210 | |
|
211 | 0 | final String[] servicesPropertiesFileNames = FileUtilities.getFileList( |
212 | |
new File(baseDir, connectServicesFolder).getAbsolutePath(), "**/*.properties", null); |
213 | 0 | for (final String fileName : servicesPropertiesFileNames) { |
214 | 0 | final Properties props = new Properties(); |
215 | 0 | props.load(new FileInputStream(new File(new File(baseDir, connectServicesFolder), fileName))); |
216 | 0 | completePropertyList.putAll(props); |
217 | |
} |
218 | |
|
219 | |
|
220 | 0 | completePropertyList.store(new FileOutputStream(new File(new File(baseDir, "lib"), project |
221 | |
.getArtifactId() |
222 | |
+ ".properties")), "Generated by maven-sonicesb-plugin:connect-package"); |
223 | |
|
224 | 0 | } catch (final FileNotFoundException e) { |
225 | 0 | throw new MojoExecutionException(e.getMessage(), e); |
226 | 0 | } catch (final IOException e) { |
227 | 0 | throw new MojoExecutionException(e.getMessage(), e); |
228 | 0 | } |
229 | 0 | } |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
private void refreshDependenciesInLib() throws MojoExecutionException { |
239 | |
|
240 | 0 | final String libDirName = project.getBasedir() + "/lib/"; |
241 | 0 | final File markersDirectory = new File(libDirName, "markers"); |
242 | |
|
243 | |
|
244 | 0 | FileUtilities.deleteFilesInDir(project.getBasedir() + "/lib/", "*", ""); |
245 | |
|
246 | 0 | MojoExecutor.executeMojo(MojoExecutor.plugin(MojoExecutor.groupId("org.apache.maven.plugins"), MojoExecutor |
247 | |
.artifactId("maven-dependency-plugin"), MojoExecutor.version("2.7")), MojoExecutor.goal("copy-dependencies"), |
248 | |
MojoExecutor.configuration(new MojoExecutor.Element[] { |
249 | |
MojoExecutor.element(MojoExecutor.name("outputDirectory"), project.getBasedir() + "/lib"), |
250 | |
MojoExecutor.element(MojoExecutor.name("includeScope"), "runtime"), |
251 | |
MojoExecutor.element(MojoExecutor.name("markersDirectory"), markersDirectory.getAbsolutePath()), |
252 | |
MojoExecutor.element(MojoExecutor.name("includeTypes"), "jar"), |
253 | |
MojoExecutor.element(MojoExecutor.name("useRepositoryLayout"), "false"), |
254 | |
MojoExecutor.element(MojoExecutor.name("useSubDirectoryPerType"), "false") |
255 | |
}), MojoExecutor.executionEnvironment(project, session, pluginManager)); |
256 | |
|
257 | |
try { |
258 | 0 | FileUtils.deleteDirectory(markersDirectory); |
259 | 0 | } catch (final IOException e) { |
260 | |
|
261 | 0 | getLog().error(e); |
262 | 0 | } |
263 | 0 | } |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | 0 | @SuppressWarnings("serial") |
297 | 0 | class SortedProperties extends Properties { |
298 | |
|
299 | |
|
300 | |
@Override |
301 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
302 | |
public Enumeration keys() { |
303 | |
|
304 | 0 | final Enumeration keysEnum = super.keys(); |
305 | 0 | final Vector keyList = new Vector(); |
306 | 0 | while (keysEnum.hasMoreElements()) { |
307 | 0 | keyList.add(keysEnum.nextElement()); |
308 | |
} |
309 | 0 | Collections.sort(keyList); |
310 | 0 | return keyList.elements(); |
311 | |
} |
312 | |
} |
313 | |
} |