001 /** 002 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. 003 * 004 * The contents of this file are subject to the terms of the Liferay Enterprise 005 * Subscription License ("License"). You may not use this file except in 006 * compliance with the License. You can obtain a copy of the License by 007 * contacting Liferay, Inc. See the License for the specific language governing 008 * permissions and limitations under the License, including but not limited to 009 * distribution rights of the Software. 010 * 011 * 012 * 013 */ 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.util.FileImpl; 022 023 import java.io.File; 024 import java.io.FileInputStream; 025 026 import java.util.ArrayList; 027 import java.util.Collections; 028 import java.util.List; 029 import java.util.Properties; 030 031 import org.apache.oro.io.GlobFilenameFilter; 032 import org.apache.tools.ant.DirectoryScanner; 033 034 /** 035 * @author Alexander Chow 036 * @author Brian Wing Shun Chan 037 */ 038 public class PluginsEnvironmentBuilder { 039 040 public static void main(String[] args) throws Exception { 041 try { 042 File dir = new File(System.getProperty("plugins.env.dir")); 043 044 new PluginsEnvironmentBuilder(dir); 045 } 046 catch (Exception e) { 047 e.printStackTrace(); 048 } 049 } 050 051 public PluginsEnvironmentBuilder(File dir) throws Exception { 052 DirectoryScanner ds = new DirectoryScanner(); 053 054 ds.setBasedir(dir); 055 ds.setIncludes(new String[] {"**\\liferay-plugin-package.properties"}); 056 057 ds.scan(); 058 059 String dirName = dir.getCanonicalPath(); 060 061 String[] fileNames = ds.getIncludedFiles(); 062 063 for (String fileName : fileNames) { 064 setupProject(dirName, fileName); 065 } 066 } 067 068 protected void addClasspathEntry(StringBundler sb, String jar) 069 throws Exception { 070 071 sb.append("\t<classpathentry kind=\"lib\" path=\""); 072 sb.append(jar); 073 sb.append("\" />\n"); 074 } 075 076 protected void setupProject(String dirName, String fileName) 077 throws Exception { 078 079 File propertiesFile = new File(dirName + "/" + fileName); 080 081 File libDir = new File(propertiesFile.getParent() + "/lib"); 082 083 String libDirPath = StringUtil.replace( 084 libDir.getPath(), StringPool.BACK_SLASH, StringPool.SLASH); 085 086 if (libDirPath.contains("/themes/")) { 087 return; 088 } 089 090 File projectDir = new File(propertiesFile.getParent() + "/../.."); 091 092 Properties properties = new Properties(); 093 094 properties.load(new FileInputStream(propertiesFile)); 095 096 String[] dependencyJars = StringUtil.split( 097 properties.getProperty( 098 "portal-dependency-jars", 099 properties.getProperty("portal.dependency.jars"))); 100 101 List<String> jars = ListUtil.toList(dependencyJars); 102 103 jars.add("commons-logging.jar"); 104 jars.add("log4j.jar"); 105 jars.add("util-bridges.jar"); 106 jars.add("util-java.jar"); 107 jars.add("util-taglib.jar"); 108 109 Collections.sort(jars); 110 111 writeEclipseFiles(libDir, projectDir, dependencyJars); 112 113 List<String> ignores = ListUtil.fromFile( 114 libDir.getCanonicalPath() + "/../.gitignore"); 115 116 if (!ignores.contains("lib")) { 117 File gitignoreFile = new File( 118 libDir.getCanonicalPath() + "/.gitignore"); 119 120 System.out.println("Updating " + gitignoreFile); 121 122 _fileUtil.write( 123 gitignoreFile, 124 StringUtil.merge(jars.toArray(new String[jars.size()]), "\n")); 125 } 126 } 127 128 protected void writeClasspathFile( 129 File libDir, String[] dependencyJars, String projectDirName, 130 String projectName, boolean javaProject) 131 throws Exception { 132 133 File classpathFile = new File(projectDirName + "/.classpath"); 134 135 if (!javaProject) { 136 classpathFile.delete(); 137 138 return; 139 } 140 141 List<String> portalJars = ListUtil.toList(dependencyJars); 142 143 portalJars.add("commons-logging.jar"); 144 portalJars.add("log4j.jar"); 145 146 portalJars = ListUtil.sort(portalJars); 147 148 String[] customJarsArray = libDir.list(new GlobFilenameFilter("*.jar")); 149 150 List<String> customJars = null; 151 152 if (customJarsArray != null) { 153 customJars = ListUtil.toList(customJarsArray); 154 155 for (String jar : portalJars) { 156 customJars.remove(jar); 157 } 158 159 customJars.remove(projectName + "-service.jar"); 160 customJars.remove("util-bridges.jar"); 161 customJars.remove("util-java.jar"); 162 customJars.remove("util-taglib.jar"); 163 164 Collections.sort(customJars); 165 } 166 else { 167 customJars = new ArrayList<String>(); 168 } 169 170 StringBundler sb = new StringBundler(); 171 172 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"); 173 sb.append("<classpath>\n"); 174 175 if (_fileUtil.exists(projectDirName + "/docroot/WEB-INF/service")) { 176 sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" "); 177 sb.append("kind=\"src\" path=\"docroot/WEB-INF/service\" />\n"); 178 } 179 180 sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" "); 181 sb.append("kind=\"src\" path=\"docroot/WEB-INF/src\" />\n"); 182 sb.append("\t<classpathentry kind=\"src\" path=\"/portal\" />\n"); 183 sb.append("\t<classpathentry kind=\"con\" "); 184 sb.append("path=\"org.eclipse.jdt.launching.JRE_CONTAINER\" />\n"); 185 186 if (_fileUtil.exists(projectDirName + "/test")) { 187 sb.append("\t<classpathentry excluding=\"**/.svn/**|.svn/\" "); 188 sb.append("kind=\"src\" path=\"test\" />\n"); 189 190 addClasspathEntry(sb, "/portal/lib/development/junit.jar"); 191 addClasspathEntry(sb, "/portal/lib/portal/commons-io.jar"); 192 } 193 194 addClasspathEntry(sb, "/portal/lib/development/activation.jar"); 195 addClasspathEntry(sb, "/portal/lib/development/annotations.jar"); 196 addClasspathEntry(sb, "/portal/lib/development/jsp-api.jar"); 197 addClasspathEntry(sb, "/portal/lib/development/mail.jar"); 198 addClasspathEntry(sb, "/portal/lib/development/servlet-api.jar"); 199 addClasspathEntry(sb, "/portal/lib/global/portlet.jar"); 200 201 for (String jar : portalJars) { 202 addClasspathEntry(sb, "/portal/lib/portal/" + jar); 203 } 204 205 addClasspathEntry(sb, "/portal/portal-service/portal-service.jar"); 206 addClasspathEntry(sb, "/portal/util-bridges/util-bridges.jar"); 207 addClasspathEntry(sb, "/portal/util-java/util-java.jar"); 208 addClasspathEntry(sb, "/portal/util-taglib/util-taglib.jar"); 209 210 String libDirPath = StringUtil.replace( 211 libDir.getPath(), StringPool.BACK_SLASH, StringPool.SLASH); 212 213 for (String jar : customJars) { 214 if (libDirPath.contains("/tmp/WEB-INF/lib")) { 215 addClasspathEntry(sb, "tmp/WEB-INF/lib/" + jar); 216 } 217 else { 218 addClasspathEntry(sb, "docroot/WEB-INF/lib/" + jar); 219 } 220 } 221 222 sb.append("\t<classpathentry kind=\"output\" path=\"bin\" />\n"); 223 sb.append("</classpath>"); 224 225 System.out.println("Updating " + classpathFile); 226 227 String content = StringUtil.replace( 228 sb.toString(), "\"/portal", "\"/portal-" + _BRANCH); 229 230 _fileUtil.write(classpathFile, content); 231 } 232 233 protected void writeEclipseFiles( 234 File libDir, File projectDir, String[] dependencyJars) 235 throws Exception { 236 237 String projectDirName = projectDir.getCanonicalPath(); 238 239 String projectName = StringUtil.extractLast( 240 projectDirName, File.separatorChar); 241 242 boolean javaProject = false; 243 244 if (_fileUtil.exists(projectDirName + "/docroot/WEB-INF/src")) { 245 javaProject = true; 246 } 247 else { 248 System.out.println( 249 "Eclipse Java project will not be used because " + 250 projectDirName + "/docroot/WEB-INF/src does not exist"); 251 } 252 253 writeProjectFile(projectDirName, projectName, javaProject); 254 255 writeClasspathFile( 256 libDir, dependencyJars, projectDirName, projectName, javaProject); 257 258 if (_fileUtil.exists(projectDirName + "/test")) { 259 _fileUtil.write( 260 projectDirName + "/.gitignore", "test-classes\ntest-results"); 261 } 262 else { 263 _fileUtil.delete(projectDirName + "/.gitignore"); 264 } 265 } 266 267 protected void writeProjectFile( 268 String projectDirName, String projectName, boolean javaProject) 269 throws Exception { 270 271 StringBundler sb = new StringBundler(17); 272 273 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"); 274 sb.append("<projectDescription>\n"); 275 sb.append("\t<name>"); 276 sb.append(projectName); 277 sb.append("-"); 278 sb.append(_BRANCH); 279 sb.append("</name>\n"); 280 sb.append("\t<comment></comment>\n"); 281 sb.append("\t<projects></projects>\n"); 282 sb.append("\t<buildSpec>\n"); 283 284 if (javaProject) { 285 sb.append("\t\t<buildCommand>\n"); 286 sb.append("\t\t\t<name>org.eclipse.jdt.core.javabuilder</name>\n"); 287 sb.append("\t\t\t<arguments></arguments>\n"); 288 sb.append("\t\t</buildCommand>\n"); 289 } 290 291 sb.append("\t</buildSpec>\n"); 292 sb.append("\t<natures>\n"); 293 294 if (javaProject) { 295 sb.append("\t\t<nature>org.eclipse.jdt.core.javanature</nature>\n"); 296 } 297 298 sb.append("\t</natures>\n"); 299 sb.append("</projectDescription>"); 300 301 File projectFile = new File(projectDirName + "/.project"); 302 303 System.out.println("Updating " + projectFile); 304 305 _fileUtil.write(projectFile, sb.toString()); 306 } 307 308 private static final String _BRANCH = "trunk"; 309 310 private static FileImpl _fileUtil = FileImpl.getInstance(); 311 312 }