This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
/
TomcatPlugin.groovy
141 lines (125 loc) · 8.35 KB
/
TomcatPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2010 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 org.gradle.api.plugins.tomcat
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.WarPlugin
import org.gradle.api.plugins.WarPluginConvention
/**
* <p>A {@link Plugin} which extends the {@link WarPlugin} to add tasks which run the web application using an embedded
* Tomcat web container.</p>
*
* @author Benjamin Muschko
*/
class TomcatPlugin implements Plugin<Project> {
static final String TOMCAT_RUN_TASK_NAME = 'tomcatRun'
static final String TOMCAT_RUN_WAR_TASK_NAME = 'tomcatRunWar'
static final String TOMCAT_STOP_TASK_NAME = 'tomcatStop'
static final String TOMCAT_JASPER_TASK_NAME = 'tomcatJasper'
static final String CLASSPATH = 'classpath'
static final String HTTP_PORT_CONVENTION = 'httpPort'
static final String HTTPS_PORT_CONVENTION = 'httpsPort'
static final String STOP_PORT_CONVENTION = 'stopPort'
static final String STOP_KEY_CONVENTION = 'stopKey'
static final String ENABLE_SSL_CONVENTION = 'enableSSL'
static final String HTTP_PROTOCOL = 'httpProtocol'
static final String HTTPS_PROTOCOL = 'httpsProtocol'
static final String TOMCAT_CONFIGURATION_NAME = 'tomcat'
@Override
void apply(Project project) {
project.plugins.apply(WarPlugin)
project.configurations.add(TOMCAT_CONFIGURATION_NAME).setVisible(false).setTransitive(true)
.setDescription('The Tomcat libraries to be used for this project.')
TomcatPluginConvention tomcatConvention = new TomcatPluginConvention()
project.convention.plugins.tomcat = tomcatConvention
configureMappingRules(project, tomcatConvention)
configureTomcatRun(project)
configureTomcatRunWar(project)
configureTomcatStop(project, tomcatConvention)
configureJasper(project, tomcatConvention)
}
private void configureMappingRules(final Project project, final TomcatPluginConvention tomcatConvention) {
project.tasks.withType(AbstractTomcatRunTask).whenTaskAdded { AbstractTomcatRunTask abstractTomcatRunTask ->
configureAbstractTomcatTask(project, tomcatConvention, abstractTomcatRunTask)
}
}
private void configureAbstractTomcatTask(final Project project, final TomcatPluginConvention tomcatConvention, AbstractTomcatRunTask tomcatTask) {
tomcatTask.daemon = false
tomcatTask.reloadable = true
tomcatTask.conventionMapping.map('buildscriptClasspath') { project.buildscript.configurations.getByName(CLASSPATH).asFileTree }
tomcatTask.conventionMapping.map('tomcatClasspath') { project.configurations.getByName(TOMCAT_CONFIGURATION_NAME).asFileTree }
tomcatTask.conventionMapping.map('contextPath') { project.tasks.getByName(WarPlugin.WAR_TASK_NAME).baseName }
tomcatTask.conventionMapping.map(HTTP_PORT_CONVENTION) { tomcatConvention.httpPort }
tomcatTask.conventionMapping.map(HTTPS_PORT_CONVENTION) { tomcatConvention.httpsPort }
tomcatTask.conventionMapping.map(STOP_PORT_CONVENTION) { tomcatConvention.stopPort }
tomcatTask.conventionMapping.map(STOP_KEY_CONVENTION) { tomcatConvention.stopKey }
tomcatTask.conventionMapping.map(ENABLE_SSL_CONVENTION) { tomcatConvention.enableSSL }
tomcatTask.conventionMapping.map(HTTP_PROTOCOL) { tomcatConvention.httpProtocol }
tomcatTask.conventionMapping.map(HTTPS_PROTOCOL) { tomcatConvention.httpsProtocol }
}
private void configureTomcatRun(final Project project) {
project.tasks.withType(TomcatRun).whenTaskAdded { TomcatRun tomcatRun ->
tomcatRun.conventionMapping.map('webAppClasspath') { project.tasks.getByName(WarPlugin.WAR_TASK_NAME).classpath }
tomcatRun.conventionMapping.map('webAppSourceDirectory') { getWarConvention(project).webAppDir }
tomcatRun.conventionMapping.map('classesDirectory') { project.sourceSets.main.output.classesDir }
}
TomcatRun tomcatRun = project.tasks.add(TOMCAT_RUN_TASK_NAME, TomcatRun)
tomcatRun.description = 'Uses your files as and where they are and deploys them to Tomcat.'
tomcatRun.group = WarPlugin.WEB_APP_GROUP
}
private void configureTomcatRunWar(final Project project) {
project.tasks.withType(TomcatRunWar).whenTaskAdded { TomcatRunWar tomcatRunWar ->
tomcatRunWar.dependsOn(WarPlugin.WAR_TASK_NAME)
tomcatRunWar.conventionMapping.map('webApp') { project.tasks.getByName(WarPlugin.WAR_TASK_NAME).archivePath }
}
TomcatRunWar tomcatRunWar = project.tasks.add(TOMCAT_RUN_WAR_TASK_NAME, TomcatRunWar)
tomcatRunWar.description = 'Assembles the webapp into a war and deploys it to Tomcat.'
tomcatRunWar.group = WarPlugin.WEB_APP_GROUP
}
private void configureTomcatStop(final Project project, final TomcatPluginConvention tomcatConvention) {
TomcatStop tomcatStop = project.tasks.add(TOMCAT_STOP_TASK_NAME, TomcatStop)
tomcatStop.description = 'Stops Tomcat.'
tomcatStop.group = WarPlugin.WEB_APP_GROUP
tomcatStop.conventionMapping.map(STOP_PORT_CONVENTION) { tomcatConvention.stopPort }
tomcatStop.conventionMapping.map(STOP_KEY_CONVENTION) { tomcatConvention.stopKey }
}
private void configureJasper(final Project project, final TomcatPluginConvention tomcatConvention) {
project.tasks.withType(TomcatJasper).whenTaskAdded { TomcatJasper tomcatJasperTask ->
tomcatJasperTask.conventionMapping.map('classpath') { project.configurations.getByName(TOMCAT_CONFIGURATION_NAME).asFileTree + project.tasks.getByName(WarPlugin.WAR_TASK_NAME).classpath }
tomcatJasperTask.conventionMapping.map('validateXml') { tomcatConvention.jasper.validateXml ?: false }
tomcatJasperTask.conventionMapping.map('uriroot') { tomcatConvention.jasper.uriroot ?: project.webAppDir }
tomcatJasperTask.conventionMapping.map('webXmlFragment') { tomcatConvention.jasper.webXmlFragment }
tomcatJasperTask.conventionMapping.map('outputDir') { tomcatConvention.jasper.outputDir ?: new File(project.buildDir, 'jasper') }
tomcatJasperTask.conventionMapping.map('classdebuginfo') { tomcatConvention.jasper.classdebuginfo ?: true }
tomcatJasperTask.conventionMapping.map('compiler') { tomcatConvention.jasper.compiler }
tomcatJasperTask.conventionMapping.map('compilerSourceVM') { tomcatConvention.jasper.compilerSourceVM ?: '1.6' }
tomcatJasperTask.conventionMapping.map('compilerTargetVM') { tomcatConvention.jasper.compilerTargetVM ?: '1.6' }
tomcatJasperTask.conventionMapping.map('poolingEnabled') { tomcatConvention.jasper.poolingEnabled ?: true }
tomcatJasperTask.conventionMapping.map('errorOnUseBeanInvalidClassAttribute') { tomcatConvention.jasper.errorOnUseBeanInvalidClassAttribute ?: true }
tomcatJasperTask.conventionMapping.map('genStringAsCharArray') { tomcatConvention.jasper.genStringAsCharArray ?: false }
tomcatJasperTask.conventionMapping.map('ieClassId') { tomcatConvention.jasper.ieClassId ?: 'clsid:8AD9C840-044E-11D1-B3E9-00805F499D93' }
tomcatJasperTask.conventionMapping.map('javaEncoding') { tomcatConvention.jasper.javaEncoding ?: 'UTF8' }
tomcatJasperTask.conventionMapping.map('trimSpaces') { tomcatConvention.jasper.trimSpaces ?: false }
tomcatJasperTask.conventionMapping.map('xpoweredBy') { tomcatConvention.jasper.xpoweredBy ?: false }
}
TomcatJasper tomcatJasper = project.tasks.add(TOMCAT_JASPER_TASK_NAME, TomcatJasper)
tomcatJasper.description = 'Runs the JSP compiler and turns JSP pages into Java source.'
tomcatJasper.group = WarPlugin.WEB_APP_GROUP
}
WarPluginConvention getWarConvention(Project project) {
project.convention.getPlugin(WarPluginConvention)
}
}