/* * Copyright 2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.jelmerk; import groovy.lang.Closure; import org.gradle.api.Project; import org.gradle.api.file.FileCollection; import org.gradle.api.file.FileTree; import org.gradle.api.initialization.dsl.ScriptHandler; import org.gradle.util.ConfigureUtil; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Extension that holds Liferay specific paths. * * @author Jelmer Kuperus */ public class LiferayPluginExtension { private final Project project; private String appServerDirName; private String appServerGlobalLibDirName; private String appServerPortalDirName; private String autoDeployDirName; /** * Constructs a new LiferayPluginExtension. * * @param project the project this extension is registered under */ public LiferayPluginExtension(Project project) { this.project = project; } /** * Returns the path to the root folder of the application server that Liferay is running in. * * @return the path to the root folder of the application server that Liferay is running in */ public String getAppServerDirName() { return appServerDirName; } /** * Sets the path to the root folder of the application server that Liferay is running in. * This property is required to be set. * * @param appServerDirName the path to the root folder of the application server that Liferay is running in. */ public void setAppServerDirName(String appServerDirName) { this.appServerDirName = appServerDirName; } /** * Return the path to the folder that holds the Liferay libraries that are on the global classpath. * * @return the path to the folder that holds the Liferay libraries that are on the global classpath */ public String getAppServerGlobalLibDirName() { if (appServerGlobalLibDirName != null) { return appServerGlobalLibDirName; } File appServerPortalDir = new File(getAppServerDirName(), "lib/ext"); return appServerPortalDir.getPath(); } /** * Set the path to the folder that holds the Liferay libraries that are on the global classpath. * If unset this value defaults to $appServerDirName/lib/ext * * @param appServerGlobalLibDirName the path to the folder that holds the Liferay libraries that are on * the global classpath */ public void setAppServerGlobalLibDirName(String appServerGlobalLibDirName) { this.appServerGlobalLibDirName = appServerGlobalLibDirName; } /** * Returns the path to the exploded Liferay web application. * * @return the path to the exploded Liferay web application */ public String getAppServerPortalDirName() { if (appServerPortalDirName != null) { return appServerPortalDirName; } File appServerPortalDir = new File(getAppServerDirName(), "webapps/ROOT"); return appServerPortalDir.getPath(); } /** * Sets the path to the exploded Liferay web application. * If unset this value defaults to $appServerDirName/webapps/ROOT * * @param appServerPortalDirName the path to the exploded Liferay web application */ public void setAppServerPortalDirName(String appServerPortalDirName) { this.appServerPortalDirName = appServerPortalDirName; } /** * Returns the path to the Liferay auto deploy folder. Plugins placed in this folder will automatically be deployed. * * @return the path to the Liferay auto deploy folder */ public String getAutoDeployDirName() { if (autoDeployDirName != null) { return autoDeployDirName; } File deployDir = new File(getAppServerDirName(), "../deploy"); return deployDir.getPath(); } /** * Sets the path to the Liferay auto deploy folder. Plugins placed in this folder will automatically be deployed. * If unset this value defaults to $appServerDirName/../deploy * * @param autoDeployDirName the path to the Liferay auto deploy folder */ public void setAutoDeployDirName(String autoDeployDirName) { this.autoDeployDirName = autoDeployDirName; } /** * Returns a file pointing to the root folder of the application server that Liferay is running in. * * @return a file pointing to the root folder of the application server that Liferay is running in */ public File getAppServerDir() { return project.file(getAppServerDirName()); } /** * Returns a file pointing to the folder that holds the Liferay libraries that are on the global classpath. * * @return a file pointing to the folder that holds the Liferay libraries that are on the global classpath */ public File getAppServerGlobalLibDir() { return project.file(getAppServerGlobalLibDirName()); } /** * Returns a file pointing to the exploded Liferay web application. * * @return a file pointing to the exploded Liferay web application */ public File getAppServerPortalDir() { return project.file(getAppServerPortalDirName()); } /** * Returns a file pointing to the auto deploy folder. Plugins placed in this folder will automatically be deployed. * * @return a file pointing to the auto deploy folder. Plugins placed in this folder will automatically be deployed */ public File getAutoDeployDir() { return project.file(getAutoDeployDirName()); } /** * Returns a file collection that holds all classes on the portal web application's classpath. It includes classes * that are not available to plugins. * * @return a file collection that holds all classes on the portal web application's classpath */ public FileCollection getPortalClasspath() { File portalClassesDir = new File(getAppServerPortalDir(), "WEB-INF/classes"); File portalLibDir = new File(getAppServerPortalDir(), "WEB-INF/lib"); FileCollection pluginClasspath = project.getBuildscript().getConfigurations() .getByName(ScriptHandler.CLASSPATH_CONFIGURATION); List<Object> classPath = new ArrayList<Object>(); classPath.add(portalClassesDir); classPath.add(newFileTree(portalLibDir, "*.jar")); classPath.add(newFileTree(getAppServerGlobalLibDir(), "*.jar")); classPath.add(pluginClasspath); return project.files(classPath); } /** * Configures this class from a groovy closure. * * @param closure the closure that configures this class */ public void liferay(Closure closure) { ConfigureUtil.configure(closure, this); } private FileTree newFileTree(File dir, String include) { Map<String, Object> args = new HashMap<String, Object>(); args.put("dir", dir); args.put("include", include); return project.fileTree(args); } }