001
014
015 package com.liferay.portal.tools;
016
017 import com.liferay.portal.kernel.util.ListUtil;
018 import com.liferay.portal.kernel.util.StringBundler;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.kernel.util.UniqueList;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.util.FileImpl;
024 import com.liferay.portal.util.PropsValues;
025
026 import java.io.File;
027 import java.io.FileInputStream;
028 import java.io.FilenameFilter;
029
030 import java.util.ArrayList;
031 import java.util.Arrays;
032 import java.util.Collections;
033 import java.util.HashMap;
034 import java.util.List;
035 import java.util.Map;
036 import java.util.Properties;
037 import java.util.Set;
038 import java.util.TreeSet;
039
040 import org.apache.oro.io.GlobFilenameFilter;
041 import org.apache.tools.ant.DirectoryScanner;
042
043
047 public class PluginsEnvironmentBuilder {
048
049 public static void main(String[] args) throws Exception {
050 try {
051 File dir = new File(System.getProperty("plugins.env.dir"));
052
053 new PluginsEnvironmentBuilder(dir);
054 }
055 catch (Exception e) {
056 e.printStackTrace();
057 }
058 }
059
060 public PluginsEnvironmentBuilder(File dir) throws Exception {
061 DirectoryScanner directoryScanner = new DirectoryScanner();
062
063 directoryScanner.setBasedir(dir);
064 directoryScanner.setIncludes(
065 new String[] {"**\\liferay-plugin-package.properties"});
066
067 directoryScanner.scan();
068
069 String dirName = dir.getCanonicalPath();
070
071 for (String fileName : directoryScanner.getIncludedFiles()) {
072 setupWarProject(dirName, fileName);
073 }
074
075 directoryScanner = new DirectoryScanner();
076
077 directoryScanner.setBasedir(dir);
078 directoryScanner.setIncludes(new String[] {"**\\build.xml"});
079
080 directoryScanner.scan();
081
082 for (String fileName : directoryScanner.getIncludedFiles()) {
083 String content = _fileUtil.read(dirName + "/" + fileName);
084
085 boolean osgiProject = false;
086
087 if (content.contains("../build-common-osgi-plugin.xml\" />") ||
088 content.contains(
089 "../tools/sdk/build-common-osgi-plugin.xml\" />")) {
090
091 osgiProject = true;
092 }
093
094 boolean sharedProject = false;
095
096 if (content.contains(
097 "<import file=\"../build-common-shared.xml\" />") ||
098 content.contains(
099 "../tools/sdk/build-common-shared.xml\" />")) {
100
101 sharedProject = true;
102 }
103
104 List<String> dependencyJars = Collections.emptyList();
105
106 if (osgiProject) {
107 int x = content.indexOf("osgi.plugin.portal.lib.jars");
108
109 if (x != -1) {
110 x = content.indexOf("value=\"", x);
111 x = content.indexOf("\"", x);
112
113 int y = content.indexOf("\"", x + 1);
114
115 dependencyJars = Arrays.asList(
116 StringUtil.split(content.substring(x + 1, y)));
117 }
118 }
119
120 if (osgiProject || sharedProject) {
121 setupJarProject(
122 dirName, fileName, dependencyJars, sharedProject);
123 }
124 }
125 }
126
127 protected void addClasspathEntry(StringBundler sb, String jar) {
128 addClasspathEntry(sb, jar, null);
129 }
130
131 protected void addClasspathEntry(
132 StringBundler sb, String jar, Map<String, String> attributes) {
133
134 sb.append("\t<classpathentry kind=\"lib\" path=\"");
135 sb.append(jar);
136
137 if ((attributes == null) || attributes.isEmpty()) {
138 sb.append("\" />\n");
139
140 return;
141 }
142
143 sb.append("\">\n\t\t<attributes>\n");
144
145 for (Map.Entry<String, String> entry : attributes.entrySet()) {
146 sb.append("\t\t\t<attribute name=\"");
147 sb.append(entry.getKey());
148 sb.append("\" value=\"");
149 sb.append(entry.getValue());
150 sb.append("\" />\n");
151 }
152
153 sb.append("\t\t</attributes>\n\t</classpathentry>\n");
154 }
155
156 protected List<String> getCommonJars() {
157 List<String> jars = new ArrayList<String>();
158
159 jars.add("commons-logging.jar");
160 jars.add("log4j.jar");
161 jars.add("util-bridges.jar");
162 jars.add("util-java.jar");
163 jars.add("util-taglib.jar");
164
165 return jars;
166 }
167
168 protected List<String> getImportSharedJars(File projectDir)
169 throws Exception {
170
171 File buildXmlFile = new File(projectDir, "build.xml");
172
173 String content = _fileUtil.read(buildXmlFile);
174
175 int x = content.indexOf("import.shared");
176
177 if (x == -1) {
178 return new ArrayList<String>();
179 }
180
181 x = content.indexOf("value=\"", x);
182 x = content.indexOf("\"", x);
183
184 int y = content.indexOf("\" />", x);
185
186 if ((x == -1) || (y == -1)) {
187 return new ArrayList<String>();
188 }
189
190 String[] importShared = StringUtil.split(content.substring(x + 1, y));
191
192 if (importShared.length == 0) {
193 return new ArrayList<String>();
194 }
195
196 List<String> jars = new ArrayList<String>();
197
198 for (String currentImportShared : importShared) {
199 jars.add(currentImportShared + ".jar");
200
201 File currentImportSharedLibDir = new File(
202 projectDir, "/../../shared/" + currentImportShared + "/lib");
203
204 if (!currentImportSharedLibDir.exists()) {
205 continue;
206 }
207
208 for (File file : currentImportSharedLibDir.listFiles()) {
209 jars.add(file.getName());
210 }
211 }
212
213 return jars;
214 }
215
216 protected List<String> getPortalDependencyJars(Properties properties) {
217 String[] dependencyJars = StringUtil.split(
218 properties.getProperty(
219 "portal-dependency-jars",
220 properties.getProperty("portal.dependency.jars")));
221
222 return ListUtil.toList(dependencyJars);
223 }
224
225 protected List<String> getRequiredDeploymentContextsJars(
226 File libDir, Properties properties)
227 throws Exception {
228
229 List<String> jars = new ArrayList<String>();
230
231 String[] requiredDeploymentContexts = StringUtil.split(
232 properties.getProperty("required-deployment-contexts"));
233
234 for (String requiredDeploymentContext : requiredDeploymentContexts) {
235 if (_fileUtil.exists(
236 libDir.getCanonicalPath() + "/" +
237 requiredDeploymentContext + "-service.jar")) {
238
239 jars.add(requiredDeploymentContext + "-service.jar");
240 }
241 }
242
243 return jars;
244 }
245
246 protected void setupJarProject(
247 String dirName, String fileName, List<String> dependencyJars,
248 boolean sharedProject)
249 throws Exception {
250
251 File buildFile = new File(dirName + "/" + fileName);
252
253 File projectDir = new File(buildFile.getParent());
254
255 File libDir = new File(projectDir, "lib");
256
257 writeEclipseFiles(libDir, projectDir, dependencyJars);
258
259 List<String> importSharedJars = getImportSharedJars(projectDir);
260
261 if (sharedProject) {
262 if (!importSharedJars.contains("portal-compat-shared.jar")) {
263 importSharedJars.add("portal-compat-shared.jar");
264 }
265 }
266
267 File gitignoreFile = new File(
268 projectDir.getCanonicalPath() + "/.gitignore");
269
270 String[] gitIgnores = importSharedJars.toArray(
271 new String[importSharedJars.size()]);
272
273 for (int i = 0; i < gitIgnores.length; i++) {
274 String gitIgnore = gitIgnores[i];
275
276 gitIgnore = "/lib/" + gitIgnore;
277
278 gitIgnores[i] = gitIgnore;
279 }
280
281 if (gitIgnores.length > 0) {
282 System.out.println("Updating " + gitignoreFile);
283
284 _fileUtil.write(gitignoreFile, StringUtil.merge(gitIgnores, "\n"));
285 }
286 }
287
288 protected void setupWarProject(String dirName, String fileName)
289 throws Exception {
290
291 File propertiesFile = new File(dirName + "/" + fileName);
292
293 Properties properties = new Properties();
294
295 properties.load(new FileInputStream(propertiesFile));
296
297 Set<String> jars = new TreeSet<String>();
298
299 jars.addAll(getCommonJars());
300
301 List<String> dependencyJars = getPortalDependencyJars(properties);
302
303 jars.addAll(dependencyJars);
304
305 File projectDir = new File(propertiesFile.getParent() + "/../..");
306
307 jars.addAll(getImportSharedJars(projectDir));
308
309 File libDir = new File(propertiesFile.getParent() + "/lib");
310
311 jars.addAll(getRequiredDeploymentContextsJars(libDir, properties));
312
313 writeEclipseFiles(libDir, projectDir, dependencyJars);
314
315 String libDirPath = StringUtil.replace(
316 libDir.getPath(), StringPool.BACK_SLASH, StringPool.SLASH);
317
318 List<String> ignores = ListUtil.fromFile(
319 libDir.getCanonicalPath() + "/../.gitignore");
320
321 if (libDirPath.contains("/ext/") || ignores.contains("/lib")) {
322 return;
323 }
324
325 File gitignoreFile = new File(
326 libDir.getCanonicalPath() + "/.gitignore");
327
328 System.out.println("Updating " + gitignoreFile);
329
330 String[] gitIgnores = jars.toArray(new String[jars.size()]);
331
332 for (int i = 0; i < gitIgnores.length; i++) {
333 String gitIgnore = gitIgnores[i];
334
335 if (Validator.isNotNull(gitIgnore) && !gitIgnore.startsWith("/")) {
336 gitIgnores[i] = "/" + gitIgnore;
337 }
338 }
339
340 _fileUtil.write(gitignoreFile, StringUtil.merge(gitIgnores, "\n"));
341 }
342
343 protected void writeClasspathFile(
344 File libDir, List<String> dependencyJars, String projectDirName,
345 String projectName, boolean javaProject)
346 throws Exception {
347
348 File classpathFile = new File(projectDirName + "/.classpath");
349
350 if (!javaProject) {
351 classpathFile.delete();
352
353 return;
354 }
355
356 List<String> globalJars = new UniqueList<String>();
357 List<String> portalJars = new UniqueList<String>();
358
359 List<String> extGlobalJars = new UniqueList<String>();
360 List<String> extPortalJars = new UniqueList<String>();
361
362 String libDirPath = StringUtil.replace(
363 libDir.getPath(), StringPool.BACK_SLASH, StringPool.SLASH);
364
365 if (libDirPath.contains("/ext/")) {
366 FilenameFilter filenameFilter = new GlobFilenameFilter("*.jar");
367
368 for (String dirName : new String[] {"global", "portal"}) {
369 File file = new File(libDirPath + "/../ext-lib/" + dirName);
370
371 List<String> jars = ListUtil.toList(file.list(filenameFilter));
372
373 if (dirName.equals("global")) {
374 extGlobalJars.addAll(ListUtil.sort(jars));
375
376 File dir = new File(PropsValues.LIFERAY_LIB_GLOBAL_DIR);
377
378 String[] fileNames = dir.list(filenameFilter);
379
380 globalJars.addAll(
381 ListUtil.sort(ListUtil.toList(fileNames)));
382 globalJars.removeAll(extGlobalJars);
383 }
384 else if (dirName.equals("portal")) {
385 extPortalJars.addAll(ListUtil.sort(jars));
386
387 File dir = new File(PropsValues.LIFERAY_LIB_PORTAL_DIR);
388
389 String[] fileNames = dir.list(filenameFilter);
390
391 portalJars.addAll(
392 ListUtil.sort(ListUtil.toList(fileNames)));
393 portalJars.removeAll(extPortalJars);
394 }
395 }
396 }
397 else {
398 globalJars.add("portlet.jar");
399
400 portalJars.addAll(dependencyJars);
401 portalJars.add("commons-logging.jar");
402 portalJars.add("log4j.jar");
403
404 Collections.sort(portalJars);
405 }
406
407 String[] customJarsArray = libDir.list(new GlobFilenameFilter("*.jar"));
408
409 List<String> customJars = null;
410
411 if (customJarsArray != null) {
412 customJars = ListUtil.toList(customJarsArray);
413
414 for (String jar : portalJars) {
415 customJars.remove(jar);
416 }
417
418 customJars.remove(projectName + "-service.jar");
419 customJars.remove("util-bridges.jar");
420 customJars.remove("util-java.jar");
421 customJars.remove("util-taglib.jar");
422
423 Collections.sort(customJars);
424 }
425 else {
426 customJars = new ArrayList<String>();
427 }
428
429 StringBundler sb = new StringBundler();
430
431 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
432 sb.append("<classpath>\n");
433
434 for (String sourceDirName : _SOURCE_DIR_NAMES) {
435 if (_fileUtil.exists(projectDirName + "/" + sourceDirName)) {
436 sb.append("\t<classpathentry excluding=\"**/.svn.svn