Hi,
Unfortunately, CUBA WAR building tasks donât support out-of-the-box deploying one WAR file several times to the same Tomcat instance.
The problem is with âcuba.webContextNameâ application which should be different for each web application (and itâs not automatically determined by WAR file name in runtime).
The easiest solution would be to use several Tomcat servers, one for each application.
However Iâve spent some time and with some tweaking of configuration files I managed to successfully run two instances of the same web application built as single WAR (using CUBA 7.2).
You can use the same approach in your project.
You will need to create separate buildWar
task for each web application you want to deploy. Renaming assembled WAR files wonât work.
Here are the steps:
- Modify single-war-web.xml file (main web application descriptor used for single WAR building).
<context-param>
<description>List of app properties files for Web Client</description>
<param-name>appPropertiesConfigWeb</param-name>
<param-value>
classpath:com/company/xxxx/web-app.properties
/WEB-INF/local.app.properties
/WEB-INF/web.local.app.properties
"file:${app.home}/local.app.properties"
</param-value>
</context-param>
<!-- Middleware parameters -->
<context-param>
<description>List of app properties files for Middleware</description>
<param-name>appPropertiesConfigCore</param-name>
<param-value>
classpath:com/company/xxxx/app.properties
/WEB-INF/local.app.properties
/WEB-INF/core.local.app.properties
"file:${app.home}/local.app.properties"
</param-value>
</context-param>
Here I added two lines:
/WEB-INF/web.local.app.properties
and
/WEB-INF/core.local.app.properties
- Create extended CubaWarBuilding task class in the end of the build.gradle file.
Itâs necessary to correctly fill âcuba.webContextNameâ and âcuba.connectionUrlListâ properties.
class MyCubaWarBuilding extends CubaWarBuilding {
@Override
protected Map<String, Object> collectProperties(Project theProject) {
Map<String, Object> map = super.collectProperties(theProject)
if (theProject == webProject) {
map.put('cuba.connectionUrlList', "http://localhost:8080/${appName}-core")
}
return map
}
@Override
protected void writeLocalAppProperties(Project theProject, Object properties) {
super.writeLocalAppProperties(theProject, properties)
Map<String, String> coreProps = [
'cuba.webContextName': "${appName}-core"
]
writeAdditionalProps(theProject, 'core.local.app.properties', coreProps)
Map<String, String> webProps = [
'cuba.webContextName': "${appName}"
]
writeAdditionalProps(theProject, 'web.local.app.properties', webProps)
}
private void writeAdditionalProps(Project theProject, String fileName, Map<String, Object> properties) {
File appPropFile = new File("${warDir(theProject)}/WEB-INF/$fileName")
project.logger.info("[CubaWarBuilding] writing $appPropFile")
appPropFile.withWriter('UTF-8') { writer ->
properties.each { key, value ->
writer << key << ' = ' << value << '\n'
}
}
}
}
- For each additional WAR you need to deploy as separate web application, manually create a task in the build.gradle.
E.g. my application name is âappâ, but I want to deploy additional WAR âsales.warâ.
// normal buildWar for app.war
task buildWar(type: CubaWarBuilding) {
includeJdbcDriver = true
appProperties = ['cuba.automaticDatabaseUpdate': true,
'cuba.dataSource.username' : 'root',
'cuba.dataSource.password' : 'root',
'cuba.dataSource.dbName' : 'myproject',
'cuba.dataSource.host' : 'localhost',
'cuba.dataSource.port' : '',
'cuba.dataSourceProvider' : 'application']
webXmlPath = 'modules/web/web/WEB-INF/single-war-web.xml'
}
// custom task for sales.war
task buildWarSales(type: MyCubaWarBuilding) {
includeJdbcDriver = true
appProperties = ['cuba.automaticDatabaseUpdate': true,
'cuba.dataSource.username' : 'root',
'cuba.dataSource.password' : 'root',
'cuba.dataSource.dbName' : 'myproject_sales',
'cuba.dataSource.host' : 'localhost',
'cuba.dataSource.port' : '',
'cuba.dataSourceProvider' : 'application']
webXmlPath = 'modules/web/web/WEB-INF/single-war-web.xml'
appName = 'sales'
}
- task name must be unique for each WAR
- appName - is the name of the WAR file
- note using of custom MyCubaWarBuilding task class
- call âgradlew buildWarSalesâ in terminal or through Gradle tool window to build additional WAR file.