/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2017 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.gradle.tasks;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.gradle.api.Project;
import org.gradle.api.internal.tasks.options.Option;
import org.gradle.api.tasks.JavaExec;
import org.gradle.api.tasks.TaskAction;
import org.gradle.jvm.tasks.Jar;

import com.axelor.common.FileUtils;
import com.axelor.gradle.support.HotswapSupport;
import com.axelor.gradle.support.TomcatSupport;

public class TomcatRun extends JavaExec {

	private boolean hot;

	private int port = 8080;

	@Option(option = "hot", description = "Specify whether to enable hot-swaping.")
	public void setHot(boolean hot) {
		this.hot = hot;
	}

	@Option(option = "port", description = "Specify the tomcat server port.")
	public void setPort(String port) {
		this.port = Integer.parseInt(port);
	}

	public static List<String> getArgs(Project project, int port) {
		final File baseDir = FileUtils.getFile(project.getBuildDir(), "tomcat");
		final File confFile = FileUtils.getFile(baseDir, TomcatSupport.TOMCAT_RUNNER_CONFIG);

		final List<String> args = new ArrayList<>();

		args.add("--port");
		args.add("" + port);
		args.add("--config");
		args.add(TomcatSupport.toRelativePath(project, confFile));

		return args;
	}
	
	public static List<String> getJvmArgs(Project project, boolean hot, boolean debug) {
		final List<String> jvmArgs = new ArrayList<>();
		if (hot || debug) {
			if (HotswapSupport.hasDCEVM()) {
				HotswapSupport.getAgentArgs(project, !debug).forEach(jvmArgs::add);
			} else {
				project.getLogger().info("Cannot enable hot-swaping as DCEVM is not installed.");
			}
		}
		return jvmArgs;
	}

	@TaskAction
	@Override
	public void exec() {
		final Project project = getProject();
		setArgs(getArgs(project, port));
		setJvmArgs(getJvmArgs(project, hot, getDebug()));
		setClasspath(((Jar) project.getTasks().getByName(TomcatSupport.TOMCAT_RUNNER_JAR_TASK)).getOutputs().getFiles());
		setMain(TomcatSupport.TOMCAT_RUNNER_CLASS);
		super.exec();
	}
}

@@ -914,22 +914,113 @@
 api.
-tasks.JavaExec
+plugins.JavaPluginConvention;%0Aimport org.gradle.api.tasks.JavaExec;%0Aimport org.gradle.api.tasks.SourceSet
 ;%0Aim
@@ -1060,41 +1060,8 @@
 ion;
-%0Aimport org.gradle.jvm.tasks.Jar;
 %0A%0Aim
@@ -1192,16 +1192,16 @@
 pport;%0A%0A
-
 public c
@@ -1235,16 +1235,96 @@
 Exec %7B%0A%0A
+%09private static final String MAIN_CLASS = %22com.axelor.app.internal.AppRunner%22;%0A%0A
 %09private
@@ -2565,16 +2565,39 @@
 ject();%0A
+%09%09setMain(MAIN_CLASS);%0A
 %09%09setArg
@@ -2694,15 +2694,8 @@
 ath(
-((Jar) 
 proj
@@ -2705,134 +2705,154 @@
 .get
-Tasks().getByName(TomcatSupport.TOMCAT_RUNNER_JAR_TASK)).getOutputs().getFile
+Convention()%0A%09%09%09.getPlugin(JavaPluginConvention.class)%0A%09%09%09.getSourceSet
 s()
-);
 %0A%09%09
-setMain(TomcatSupport.TOMCAT_RUNNER_CLASS
+%09.getByName(SourceSet.MAIN_SOURCE_SET_NAME)%0A%09%09%09.getRuntimeClasspath()
 );%0A%09

Use app runner with run task for better logging support