001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.FileComparator;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.xml.Document;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.kernel.xml.SAXReader;
027    import com.liferay.portal.util.FileImpl;
028    import com.liferay.portal.util.PropsValues;
029    import com.liferay.portal.xml.SAXReaderImpl;
030    
031    import java.io.File;
032    import java.io.FileInputStream;
033    import java.io.FilenameFilter;
034    
035    import java.util.ArrayList;
036    import java.util.Arrays;
037    import java.util.Collections;
038    import java.util.HashMap;
039    import java.util.LinkedHashSet;
040    import java.util.List;
041    import java.util.Map;
042    import java.util.Properties;
043    import java.util.Set;
044    import java.util.TreeSet;
045    
046    import org.apache.oro.io.GlobFilenameFilter;
047    import org.apache.tools.ant.DirectoryScanner;
048    
049    /**
050     * @author Alexander Chow
051     * @author Brian Wing Shun Chan
052     */
053    public class PluginsEnvironmentBuilder {
054    
055            public static void main(String[] args) throws Exception {
056                    try {
057                            File dir = new File(System.getProperty("plugins.env.dir"));
058    
059                            new PluginsEnvironmentBuilder(dir);
060                    }
061                    catch (Exception e) {
062                            e.printStackTrace();
063                    }
064            }
065    
066            public PluginsEnvironmentBuilder(File dir) throws Exception {
067                    DirectoryScanner directoryScanner = new DirectoryScanner();
068    
069                    directoryScanner.setBasedir(dir);
070                    directoryScanner.setIncludes(
071                            new String[] {"**\\liferay-plugin-package.properties"});
072    
073                    directoryScanner.scan();
074    
075                    String dirName = dir.getCanonicalPath();
076    
077                    for (String fileName : directoryScanner.getIncludedFiles()) {
078                            setupWarProject(dirName, fileName);
079                    }
080    
081                    directoryScanner = new DirectoryScanner();
082    
083                    directoryScanner.setBasedir(dir);
084                    directoryScanner.setIncludes(new String[] {"**\\build.xml"});
085    
086                    directoryScanner.scan();
087    
088                    for (String fileName : directoryScanner.getIncludedFiles()) {
089                            String content = _fileUtil.read(dirName + "/" + fileName);
090    
091                            boolean osgiProject = false;
092    
093                            if (content.contains("../build-common-osgi-plugin.xml\" />") ||
094                                    content.contains(
095                                            "../tools/sdk/build-common-osgi-plugin.xml\" />")) {
096    
097                                    osgiProject = true;
098                            }
099    
100                            boolean sharedProject = false;
101    
102                            if (content.contains(
103                                            "<import file=\"../build-common-shared.xml\" />") ||
104                                    content.contains(
105                                            "../tools/sdk/build-common-shared.xml\" />")) {
106    
107                                    sharedProject = true;
108                            }
109    
110                            List<String> dependencyJars = Collections.emptyList();
111    
112                            if (osgiProject) {
113                                    int x = content.indexOf("osgi.ide.dependencies");
114    
115                                    if (x != -1) {
116                                            x = content.indexOf("value=\"", x);
117                                            x = content.indexOf("\"", x);
118    
119                                            int y = content.indexOf("\"", x + 1);
120    
121                                            dependencyJars = Arrays.asList(
122                                                    StringUtil.split(content.substring(x + 1, y)));
123                                    }
124                            }
125    
126                            if (osgiProject || sharedProject) {
127                                    setupJarProject(
128                                            dirName, fileName, dependencyJars, sharedProject);
129                            }
130                    }
131            }
132    
133            protected void addClasspathEntry(StringBundler sb, String jar) {
134                    addClasspathEntry(sb, jar, null);
135            }
136    
137            protected void addClasspathEntry(
138                    StringBundler sb, String jar, Map<String, String> attributes) {
139    
140                    sb.append("\t<classpathentry kind=\"lib\" path=\"");
141                    sb.append(jar);
142    
143                    if ((attributes == null) || attributes.isEmpty()) {
144                            sb.append("\" />\n");
145    
146                            return;
147                    }
148    
149                    sb.append("\">\n\t\t<attributes>\n");
150    
151                    for (Map.Entry<String, String> entry : attributes.entrySet()) {
152                            sb.append("\t\t\t<attribute name=\"");
153                            sb.append(entry.getKey());
154                            sb.append("\" value=\"");
155                            sb.append(entry.getValue());
156                            sb.append("\" />\n");
157                    }
158    
159                    sb.append("\t\t</attributes>\n\t</classpathentry>\n");
160            }
161    
162            protected void addIvyCacheJar(
163                            StringBundler sb, String ivyDirName, String dependencyName,
164                            String version)
165                    throws Exception {
166    
167                    String string = sb.toString();
168    
169                    if (string.contains(dependencyName)) {
170                            System.out.println(
171                                    "Skipping duplicate " + dependencyName + " " + version);
172    
173                            return;
174                    }
175    
176                    System.out.println("Adding " + dependencyName + " " + version);
177    
178                    if (version.equals("latest.integration")) {
179                            File dir = new File(ivyDirName + "/cache/" + dependencyName);
180    
181                            File[] files = dir.listFiles();
182    
183                            Arrays.sort(files, new FileComparator());
184    
185                            for (int i = files.length - 1; i >= 0; i--) {
186                                    File file = files[i];
187    
188                                    if (!file.isFile()) {
189                                            continue;
190                                    }
191    
192                                    String fileName = file.getName();
193    
194                                    if (!fileName.endsWith(".xml")) {
195                                            continue;
196                                    }
197    
198                                    version = fileName.substring(4, fileName.length() - 4);
199    
200                                    System.out.println(
201                                            "Substituting " + version + " for latest.integration");
202                            }
203                    }
204    
205                    String ivyFileName =
206                            ivyDirName + "/cache/" + dependencyName + "/ivy-" + version +
207                                    ".xml";
208    
209                    if (_fileUtil.exists(ivyFileName)) {
210                            Document document = _saxReader.read(new File(ivyFileName));
211    
212                            Element rootElement = document.getRootElement();
213    
214                            Element dependenciesElement = rootElement.element("dependencies");
215    
216                            if (dependenciesElement != null) {
217                                    List<Element> dependencyElements = dependenciesElement.elements(
218                                            "dependency");
219    
220                                    for (Element dependencyElement : dependencyElements) {
221                                            String conf = GetterUtil.getString(
222                                                    dependencyElement.attributeValue("conf"));
223    
224                                            if (!conf.startsWith("compile")) {
225                                                    continue;
226                                            }
227    
228                                            String name = GetterUtil.getString(
229                                                    dependencyElement.attributeValue("name"));
230                                            String org = GetterUtil.getString(
231                                                    dependencyElement.attributeValue("org"));
232                                            String rev = GetterUtil.getString(
233                                                    dependencyElement.attributeValue("rev"));
234    
235                                            string = sb.toString();
236    
237                                            if (string.contains(name)) {
238                                                    continue;
239                                            }
240    
241                                            addIvyCacheJar(sb, ivyDirName, org + "/" + name, rev);
242                                    }
243                            }
244                    }
245    
246                    String dirName = ivyDirName + "/cache/" + dependencyName + "/bundles";
247    
248                    if (!_fileUtil.exists(dirName)) {
249                            dirName = ivyDirName + "/cache/" + dependencyName + "/jars";
250    
251                            if (!_fileUtil.exists(dirName)) {
252                                    System.out.println("Unable to find jars in " + dirName);
253    
254                                    return;
255                            }
256                    }
257    
258                    File dir = new File(dirName);
259    
260                    File[] files = dir.listFiles();
261    
262                    for (File file : files) {
263                            if (!file.isFile()) {
264                                    continue;
265                            }
266    
267                            String fileName = file.getName();
268    
269                            if (!fileName.endsWith("-" + version + ".jar")) {
270                                    continue;
271                            }
272    
273                            int index = dirName.indexOf("/.ivy");
274    
275                            String eclipseRelativeDirName =
276                                    "/portal" + dirName.substring(index);
277    
278                            addClasspathEntry(sb, eclipseRelativeDirName + "/" + fileName);
279    
280                            return;
281                    }
282    
283                    System.out.println(
284                            "Unable to find jars in " + dirName + " for " + version);
285            }
286    
287            protected void addIvyCacheJars(
288                            StringBundler sb, String content, String ivyDirName)
289                    throws Exception {
290    
291                    Document document = _saxReader.read(content);
292    
293                    Element rootElement = document.getRootElement();
294    
295                    Element dependenciesElement = rootElement.element("dependencies");
296    
297                    List<Element> dependencyElements = dependenciesElement.elements(
298                            "dependency");
299    
300                    for (Element dependencyElement : dependencyElements) {
301                            String conf = GetterUtil.getString(
302                                    dependencyElement.attributeValue("conf"));
303    
304                            if (!conf.equals("test->default")) {
305                                    continue;
306                            }
307    
308                            String name = GetterUtil.getString(
309                                    dependencyElement.attributeValue("name"));
310                            String org = GetterUtil.getString(
311                                    dependencyElement.attributeValue("org"));
312                            String rev = GetterUtil.getString(
313                                    dependencyElement.attributeValue("rev"));
314    
315                            addIvyCacheJar(sb, ivyDirName, org + "/" + name, rev);
316                    }
317            }
318    
319            protected List<String> getCommonJars() {
320                    List<String> jars = new ArrayList<>();
321    
322                    jars.add("commons-logging.jar");
323                    jars.add("log4j.jar");
324                    jars.add("util-bridges.jar");
325                    jars.add("util-java.jar");
326                    jars.add("util-taglib.jar");
327    
328                    return jars;
329            }
330    
331            protected List<String> getImportSharedJars(File projectDir)
332                    throws Exception {
333    
334                    File buildXmlFile = new File(projectDir, "build.xml");
335    
336                    String content = _fileUtil.read(buildXmlFile);
337    
338                    int x = content.indexOf("import.shared");
339    
340                    if (x == -1) {
341                            return new ArrayList<>();
342                    }
343    
344                    x = content.indexOf("value=\"", x);
345                    x = content.indexOf("\"", x);
346    
347                    int y = content.indexOf("\" />", x);
348    
349                    if ((x == -1) || (y == -1)) {
350                            return new ArrayList<>();
351                    }
352    
353                    String[] importShared = StringUtil.split(content.substring(x + 1, y));
354    
355                    if (importShared.length == 0) {
356                            return new ArrayList<>();
357                    }
358    
359                    List<String> jars = new ArrayList<>();
360    
361                    for (String currentImportShared : importShared) {
362                            jars.add(currentImportShared + ".jar");
363    
364                            File currentImportSharedLibDir = new File(
365                                    projectDir, "../../shared/" + currentImportShared + "/lib");
366    
367                            if (!currentImportSharedLibDir.exists()) {
368                                    continue;
369                            }
370    
371                            for (File file : currentImportSharedLibDir.listFiles()) {
372                                    jars.add(file.getName());
373                            }
374                    }
375    
376                    return jars;
377            }
378    
379            protected List<String> getPortalDependencyJars(Properties properties) {
380                    String[] dependencyJars = StringUtil.split(
381                            properties.getProperty(
382                                    "portal-dependency-jars",
383                                    properties.getProperty("portal.dependency.jars")));
384    
385                    return ListUtil.toList(dependencyJars);
386            }
387    
388            protected List<String> getRequiredDeploymentContextsJars(
389                            File libDir, Properties properties)
390                    throws Exception {
391    
392                    List<String> jars = new ArrayList<>();
393    
394                    String[] requiredDeploymentContexts = StringUtil.split(
395                            properties.getProperty("required-deployment-contexts"));
396    
397                    for (String requiredDeploymentContext : requiredDeploymentContexts) {
398                            if (_fileUtil.exists(
399                                            libDir.getCanonicalPath() + "/" +
400                                                    requiredDeploymentContext + "-service.jar")) {
401    
402                                    jars.add(requiredDeploymentContext + "-service.jar");
403                            }
404                    }
405    
406                    return jars;
407            }
408    
409            protected boolean hasModulesGitIgnore(String dirName) {
410                    int index = dirName.indexOf("/modules/");
411    
412                    if (index == -1) {
413                            return false;
414                    }
415    
416                    return _fileUtil.exists(
417                            dirName.substring(0, index) + "/modules/.gitignore");
418            }
419    
420            protected void setupJarProject(
421                            String dirName, String fileName, List<String> dependencyJars,
422                            boolean sharedProject)
423                    throws Exception {
424    
425                    File buildFile = new File(dirName + "/" + fileName);
426    
427                    File projectDir = new File(buildFile.getParent());
428    
429                    File libDir = new File(projectDir, "lib");
430    
431                    if (!libDir.exists()) {
432                            libDir = new File(projectDir, "docroot/WEB-INF/lib");
433                    }
434    
435                    writeEclipseFiles(libDir, projectDir, dependencyJars);
436    
437                    List<String> importSharedJars = getImportSharedJars(projectDir);
438    
439                    if (sharedProject) {
440                            if (!importSharedJars.contains("portal-compat-shared.jar")) {
441                                    importSharedJars.add("portal-compat-shared.jar");
442                            }
443                    }
444    
445                    File gitignoreFile = new File(
446                            projectDir.getCanonicalPath() + "/.gitignore");
447    
448                    if (hasModulesGitIgnore(dirName)) {
449                            gitignoreFile.delete();
450    
451                            return;
452                    }
453    
454                    String[] gitIgnores = importSharedJars.toArray(
455                            new String[importSharedJars.size()]);
456    
457                    for (int i = 0; i < gitIgnores.length; i++) {
458                            String gitIgnore = gitIgnores[i];
459    
460                            gitIgnore = "/lib/" + gitIgnore;
461    
462                            gitIgnores[i] = gitIgnore;
463                    }
464    
465                    if (gitIgnores.length > 0) {
466                            System.out.println("Updating " + gitignoreFile);
467    
468                            _fileUtil.write(gitignoreFile, StringUtil.merge(gitIgnores, "\n"));
469                    }
470            }
471    
472            protected void setupWarProject(String dirName, String fileName)
473                    throws Exception {
474    
475                    File propertiesFile = new File(dirName + "/" + fileName);
476    
477                    Properties properties = new Properties();
478    
479                    properties.load(new FileInputStream(propertiesFile));
480    
481                    Set<String> jars = new TreeSet<>();
482    
483                    jars.addAll(getCommonJars());
484    
485                    List<String> dependencyJars = getPortalDependencyJars(properties);
486    
487                    jars.addAll(dependencyJars);
488    
489                    File projectDir = new File(propertiesFile.getParent() + "/../..");
490    
491                    jars.addAll(getImportSharedJars(projectDir));
492    
493                    File libDir = new File(propertiesFile.getParent() + "/lib");
494    
495                    jars.addAll(getRequiredDeploymentContextsJars(libDir, properties));
496    
497                    writeEclipseFiles(libDir, projectDir, dependencyJars);
498    
499                    String libDirPath = StringUtil.replace(
500                            libDir.getPath(), StringPool.BACK_SLASH, StringPool.SLASH);
501    
502                    List<String> ignores = ListUtil.fromFile(
503                            libDir.getCanonicalPath() + "/../.gitignore");
504    
505                    if (libDirPath.contains("/ext/") || ignores.contains("/lib")) {
506                            return;
507                    }
508    
509                    File gitignoreFile = new File(
510                            libDir.getCanonicalPath() + "/.gitignore");
511    
512                    System.out.println("Updating " + gitignoreFile);
513    
514                    String[] gitIgnores = jars.toArray(new String[jars.size()]);
515    
516                    for (int i = 0; i < gitIgnores.length; i++) {
517                            String gitIgnore = gitIgnores[i];
518    
519                            if (Validator.isNotNull(gitIgnore) && !gitIgnore.startsWith("/")) {
520                                    gitIgnores[i] = "/" + gitIgnore;
521                            }
522                    }
523    
524                    _fileUtil.write(gitignoreFile, StringUtil.merge(gitIgnores, "\n"));
525            }
526    
527            protected void writeClasspathFile(
528                            File libDir, List<String> dependencyJars, String projectDirName,
529                            String projectName, boolean javaProject)
530                    throws Exception {
531    
532                    File classpathFile = new File(projectDirName + "/.classpath");
533    
534                    if (!javaProject) {
535                            classpathFile.delete();
536    
537                            return;
538                    }
539    
540                    Set<String> globalJars = new LinkedHashSet<>();
541                    List<String> portalJars = new ArrayList<>();
542    
543                    Set<String> extGlobalJars = new LinkedHashSet<>();
544                    Set<String> extPortalJars = new LinkedHashSet<>();
545    
546                    String libDirPath = StringUtil.replace(
547                            libDir.getPath(), StringPool.BACK_SLASH, StringPool.SLASH);
548    
549                    if (libDirPath.contains("/ext/")) {
550                            FilenameFilter filenameFilter = new GlobFilenameFilter("*.jar");
551    
552                            for (String dirName : new String[] {"global", "portal"}) {
553                                    File file = new File(libDirPath + "/../ext-lib/" + dirName);
554    
555                                    List<String> jars = ListUtil.toList(file.list(filenameFilter));
556    
557                                    if (dirName.equals("global")) {
558                                            extGlobalJars.addAll(ListUtil.sort(jars));
559    
560                                            File dir = new File(PropsValues.LIFERAY_LIB_GLOBAL_DIR);
561    
562                                            String[] fileNames = dir.list(filenameFilter);
563    
564                                            globalJars.addAll(
565                                                    ListUtil.sort(ListUtil.toList(fileNames)));
566                                            globalJars.removeAll(extGlobalJars);
567                                    }
568                                    else if (dirName.equals("portal")) {
569                                            extPortalJars.addAll(ListUtil.sort(jars));
570    
571                                            File dir = new File(PropsValues.LIFERAY_LIB_PORTAL_DIR);
572    
573                                            String[] fileNames = dir.list(filenameFilter);
574    
575                                            portalJars.addAll(
576                                                    ListUtil.sort(ListUtil.toList(fileNames)));
577                                            portalJars.removeAll(extPortalJars);
578                                    }
579                            }
580                    }
581                    else {
582                            globalJars.add("portlet.jar");
583    
584                            portalJars.addAll(dependencyJars);
585                            portalJars.add("bnd.jar");
586                            portalJars.add("commons-logging.jar");
587                            portalJars.add("log4j.jar");
588    
589                            portalJars = ListUtil.unique(portalJars);
590    
591                            Collections.sort(portalJars);
592                    }
593    
594                    String[] customJarsArray = libDir.list(new GlobFilenameFilter("*.jar"));
595    
596                    List<String> customJars = null;
597    
598                    if (customJarsArray != null) {
599                            customJars = ListUtil.toList(customJarsArray);
600    
601                            for (String jar : portalJars) {
602                                    customJars.remove(jar);
603                            }
604    
605                            customJars.remove(projectName + "-service.jar");
606                            customJars.remove("util-bridges.jar");
607                            customJars.remove("util-java.jar");
608                            customJars.remove("util-taglib.jar");
609    
610                            Collections.sort(customJars);
611                    }
612                    else {
613                            customJars = new ArrayList<>();
614                    }
615    
616                    StringBundler sb = new StringBundler();
617    
618                    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
619                    sb.append("<classpath>\n");
620    
621                    for (String sourceDirName : _SOURCE_DIR_NAMES) {
622                            if (_fileUtil.exists(projectDirName + "/" + sourceDirName)) {
623                                    sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
624                                    sb.append("kind=\"src\" path=\"");
625                                    sb.append(sourceDirName);
626                                    sb.append("\" />\n");
627                            }
628                    }
629    
630                    sb.append("\t<classpathentry kind=\"src\" path=\"/portal\" />\n");
631                    sb.append("\t<classpathentry kind=\"con\" ");
632                    sb.append("path=\"org.eclipse.jdt.launching.JRE_CONTAINER\" />\n");
633    
634                    boolean addJunitJars = false;
635    
636                    for (String testType : _TEST_TYPES) {
637                            String testFolder = "test/" + testType;
638    
639                            if (_fileUtil.exists(projectDirName + "/" + testFolder)) {
640                                    addJunitJars = true;
641    
642                                    sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" ");
643                                    sb.append("kind=\"src\" path=\"");
644                                    sb.append(testFolder);
645                                    sb.append("\" />\n");
646                            }
647                    }
648    
649                    if (addJunitJars) {
650                            addClasspathEntry(sb, "/portal/lib/development/junit.jar");
651                            addClasspathEntry(sb, "/portal/lib/development/mockito.jar");
652                            addClasspathEntry(
653                                    sb, "/portal/lib/development/powermock-api-mockito.jar");
654                            addClasspathEntry(
655                                    sb, "/portal/lib/development/powermock-api-support.jar");
656                            addClasspathEntry(sb, "/portal/lib/development/powermock-core.jar");
657                            addClasspathEntry(
658                                    sb, "/portal/lib/development/powermock-module-junit4.jar");
659                            addClasspathEntry(
660                                    sb,
661                                    "/portal/lib/development/powermock-module-junit4-common.jar");
662                            addClasspathEntry(sb, "/portal/lib/development/spring-test.jar");
663    
664                            portalJars.add("commons-io.jar");
665                            portalJars.add("commons-lang.jar");
666                    }
667    
668                    addClasspathEntry(sb, "/portal/lib/development/activation.jar");
669                    addClasspathEntry(sb, "/portal/lib/development/annotations.jar");
670                    addClasspathEntry(sb, "/portal/lib/development/jsp-api.jar");
671                    addClasspathEntry(sb, "/portal/lib/development/mail.jar");
672                    addClasspathEntry(sb, "/portal/lib/development/servlet-api.jar");
673    
674                    Map<String, String> attributes = new HashMap<>();
675    
676                    if (libDirPath.contains("/ext/")) {
677                            attributes.put("optional", "true");
678                    }
679    
680                    for (String jar : globalJars) {
681                            addClasspathEntry(sb, "/portal/lib/global/" + jar, attributes);
682                    }
683    
684                    portalJars = ListUtil.unique(portalJars);
685    
686                    Collections.sort(portalJars);
687    
688                    for (String jar : portalJars) {
689                            if (!jar.equals("util-slf4j.jar")) {
690                                    addClasspathEntry(sb, "/portal/lib/portal/" + jar, attributes);
691                            }
692                    }
693    
694                    addClasspathEntry(sb, "/portal/portal-service/portal-service.jar");
695                    addClasspathEntry(sb, "/portal/util-bridges/util-bridges.jar");
696                    addClasspathEntry(sb, "/portal/util-java/util-java.jar");
697    
698                    if (portalJars.contains("util-slf4j.jar")) {
699                            addClasspathEntry(sb, "/portal/util-slf4j/util-slf4j.jar");
700                    }
701    
702                    addClasspathEntry(sb, "/portal/util-taglib/util-taglib.jar");
703    
704                    for (String jar : extGlobalJars) {
705                            addClasspathEntry(sb, "docroot/WEB-INF/ext-lib/global/" + jar);
706                    }
707    
708                    for (String jar : extPortalJars) {
709                            addClasspathEntry(sb, "docroot/WEB-INF/ext-lib/portal/" + jar);
710                    }
711    
712                    for (String jar : customJars) {
713                            if (libDirPath.contains("/tmp/WEB-INF/lib")) {
714                                    addClasspathEntry(sb, "tmp/WEB-INF/lib/" + jar);
715                            }
716                            else if (libDirPath.contains("/docroot/WEB-INF/lib")) {
717                                    addClasspathEntry(sb, "docroot/WEB-INF/lib/" + jar);
718                            }
719                            else {
720                                    addClasspathEntry(sb, "lib/" + jar);
721                            }
722                    }
723    
724                    File ivyXmlFile = new File(projectDirName, "ivy.xml");
725    
726                    if (ivyXmlFile.exists()) {
727                            String content = _fileUtil.read(ivyXmlFile);
728    
729                            if (content.contains("test->default")) {
730                                    String ivyDirName = ".ivy";
731    
732                                    for (int i = 0; i < 10; i++) {
733                                            if (_fileUtil.exists(ivyDirName)) {
734                                                    break;
735                                            }
736    
737                                            ivyDirName = "../" + ivyDirName;
738                                    }
739    
740                                    addIvyCacheJars(sb, content, ivyDirName);
741                            }
742                    }
743    
744                    sb.append("\t<classpathentry kind=\"output\" path=\"bin\" />\n");
745                    sb.append("</classpath>");
746    
747                    System.out.println("Updating " + classpathFile);
748    
749                    String content = StringUtil.replace(
750                            sb.toString(), "\"/portal", "\"/portal-" + _BRANCH);
751    
752                    _fileUtil.write(classpathFile, content);
753            }
754    
755            protected void writeEclipseFiles(
756                            File libDir, File projectDir, List<String> dependencyJars)
757                    throws Exception {
758    
759                    String projectDirName = projectDir.getCanonicalPath();
760    
761                    String projectName = StringUtil.extractLast(
762                            projectDirName, File.separatorChar);
763    
764                    boolean javaProject = false;
765    
766                    for (String sourceDirName : _SOURCE_DIR_NAMES) {
767                            if (_fileUtil.exists(projectDirName + "/" + sourceDirName)) {
768                                    javaProject = true;
769    
770                                    break;
771                            }
772                    }
773    
774                    if (!javaProject) {
775                            System.out.println(
776                                    "Eclipse Java project will not be used because a source " +
777                                            "folder does not exist");
778                    }
779    
780                    writeProjectFile(projectDirName, projectName, javaProject);
781    
782                    writeClasspathFile(
783                            libDir, dependencyJars, projectDirName, projectName, javaProject);
784    
785                    for (String sourceDirName : _SOURCE_DIR_NAMES) {
786                            if (_fileUtil.exists(projectDirName + "/" + sourceDirName)) {
787                                    List<String> gitIgnores = new ArrayList<>();
788    
789                                    if (sourceDirName.endsWith("ext-impl/src")) {
790                                            gitIgnores.add("/classes");
791                                            gitIgnores.add("/ext-impl.jar");
792                                    }
793                                    else if (sourceDirName.endsWith("ext-service/src")) {
794                                            gitIgnores.add("/classes");
795                                            gitIgnores.add("/ext-service.jar");
796                                    }
797                                    else if (sourceDirName.endsWith("ext-util-bridges/src")) {
798                                            gitIgnores.add("/classes");
799                                            gitIgnores.add("/ext-util-bridges.jar");
800                                    }
801                                    else if (sourceDirName.endsWith("ext-util-java/src")) {
802                                            gitIgnores.add("/classes");
803                                            gitIgnores.add("/ext-util-java.jar");
804                                    }
805                                    else if (sourceDirName.endsWith("ext-util-taglib/src")) {
806                                            gitIgnores.add("/classes");
807                                            gitIgnores.add("/ext-util-taglib.jar");
808                                    }
809                                    else {
810                                            continue;
811                                    }
812    
813                                    String dirName = projectDirName + "/" + sourceDirName + "/../";
814    
815                                    if (gitIgnores.isEmpty()) {
816                                            _fileUtil.delete(dirName + ".gitignore");
817                                    }
818                                    else {
819                                            String gitIgnoresString = StringUtil.merge(
820                                                    gitIgnores, "\n");
821    
822                                            _fileUtil.write(dirName + ".gitignore", gitIgnoresString);
823                                    }
824                            }
825                    }
826    
827                    if (_fileUtil.exists(projectDirName + "/test")) {
828                            _fileUtil.write(
829                                    projectDirName + "/.gitignore", "/test-classes\n/test-results");
830                    }
831                    else {
832                            _fileUtil.delete(projectDirName + "/.gitignore");
833                    }
834            }
835    
836            protected void writeProjectFile(
837                            String projectDirName, String projectName, boolean javaProject)
838                    throws Exception {
839    
840                    StringBundler sb = new StringBundler(19);
841    
842                    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
843                    sb.append("<projectDescription>\n");
844                    sb.append("\t<name>");
845                    sb.append(projectName);
846                    sb.append("-");
847                    sb.append(_BRANCH);
848                    sb.append("</name>\n");
849                    sb.append("\t<comment></comment>\n");
850                    sb.append("\t<projects></projects>\n");
851                    sb.append("\t<buildSpec>\n");
852    
853                    if (javaProject) {
854                            sb.append("\t\t<buildCommand>\n");
855                            sb.append("\t\t\t<name>org.eclipse.jdt.core.javabuilder</name>\n");
856                            sb.append("\t\t\t<arguments></arguments>\n");
857                            sb.append("\t\t</buildCommand>\n");
858                    }
859    
860                    sb.append("\t</buildSpec>\n");
861                    sb.append("\t<natures>\n");
862    
863                    if (javaProject) {
864                            sb.append("\t\t<nature>org.eclipse.jdt.core.javanature</nature>\n");
865                    }
866    
867                    sb.append("\t</natures>\n");
868                    sb.append("</projectDescription>");
869    
870                    File projectFile = new File(projectDirName + "/.project");
871    
872                    System.out.println("Updating " + projectFile);
873    
874                    _fileUtil.write(projectFile, sb.toString());
875            }
876    
877            private static final String _BRANCH = "master";
878    
879            private static final String[] _SOURCE_DIR_NAMES = new String[] {
880                    "docroot/WEB-INF/ext-impl/src", "docroot/WEB-INF/ext-service/src",
881                    "docroot/WEB-INF/ext-util-bridges/src",
882                    "docroot/WEB-INF/ext-util-java/src",
883                    "docroot/WEB-INF/ext-util-taglib/src", "docroot/WEB-INF/service",
884                    "docroot/WEB-INF/src", "src"
885            };
886    
887            private static final String[] _TEST_TYPES = {"integration", "unit"};
888    
889            private static final FileImpl _fileUtil = FileImpl.getInstance();
890            private static final SAXReader _saxReader = new SAXReaderImpl();
891    
892    }