How to switch Tomcat context.xml with WTP in Eclipse

Introduction

When using the data source provided by Tomcat from a Web application, describe the data source settings in context.xml. In this article, I will show you how to switch context.xml for each developer when starting Tomcat using Eclipse's WTP (Web Tools Plugin) for development.

If you want to know how to do it quickly, please see [Switching method](#Switching method).

Why you need to switch context.xml

There are two main locations for context.xml.

  1. META-INF / context.xml under the Web application
  2. $ CATALINA_BASE / conf / context.xml under the Tomcat installation directory

(Reference: Apache Tomcat 8 Configuration Reference (8.5.32) --The Context Container)

Each has the following features, but it has advantages and disadvantages.

** 1. Under Web application **

--It's easy because you can include contex.xml in the war file and deploy it as it is. --context.xml is also subject to configuration management. (Aside from whether or not DB connection information can be targeted for configuration management) --Switch connection information in production environment, test environment, etc. at build time (= war creation time) using Maven resource filter etc. --Every time a developer tests in a local environment, it is not possible to build with Maven, so `` `META-INF / context.xml``` is ** manually modified and accidentally committed. Frequently **.

** 2. Under Tomcat installation directory **

--Click here if it is based on the idea that the data source is provided by the application server. --context.xml is not subject to configuration management. --Instead, it must be prepared according to the production environment, test environment, local environment of each developer, etc.

Especially when each developer develops while deploying to Tomcat on Eclipse while using his / her own development database during development, how to make context.xml a configuration management target and switch it according to the environment. to introduce.

How to switch

Basic idea

Keep all the files that describe the data source settings for each developer as Git management, and switch using symbolic links. Symbolic links should be deployed under META-INF in the WTP settings.

The context.xml that describes the data source settings for each developer is placed under the `context / user``` directory with the file name ``` context_login user name.xml```. This is Git managed. The login user name is the Windows login user name ( % USERNAME% `` environment variable). In the example below, alice, bob, and charlie are the developer login usernames. In this way, create as many files as there are developers.

Each developer configures the data source by creating a symbolic link to their own context_login username.xml``` as `context / META-INF / context.xml```. Achieves switching.

Eclipse project directory structure

Create new `context / META-INF``` and `context / user``` directories under the Eclipse project.

context/meta-infThe contents of are in the web application according to the settings described below.meta-infIt will be deployed under it.

context/Context for each developer mentioned above under user_***.Place the xml. Also, a batch file for creating symbolic links(See below)Also place.



(Project root directory) ┊ ├ context │ ├ META-INF │ │ ├ .gitignore │ │ └ context.xml ← <setup-context.A symbolic link created by bat. Not managed by Git> │ │ │ └ user │ ├ setup-context.bat ← <Batch file for creating symbolic links(See below)> │ ├ context_alice.xml ← │ ├ context_bob.xml ← │ └ context_charlie.xml ←



#### **`context_***.The content of xml is the following context.In xml itself, change only the connection destination according to the developer.(Below is an example in MySQL)`**

context_***.xml


<?xml version="1.0" encoding="UTF-8"?>
<Context path="/webapp" docBase="webapp" crossContext="true" reloadable="true" debug="1">
	<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
                  driverClassName="com.mysql.jdbc.Driver"
                  url="jdbc:mysql://host:3306/Database name"
                  username="User name" password="password"
                  maxIdle="10" defaultAutoCommit="false" />
</Context>

Creating a symbolic link

setup-context.bat is a batch file for creating symbolic links easily. The developer clones the project and then runs this batch file. setup-context.bat is%USERNAME%See context_Login username.Context with symbolic link to xml/META-INF/context.It will be created as xml.



 This symbolic link is the only file that changes from developer to developer. This symlink is out of Git control, as we'll see later, so it won't cause any conflicts.

## Set symlinks out of Git management

 Due to the Git specifications, the entity of a symbolic link is registered in the repository, so `` `context / META-INF / context.xml` `` is excluded from Git management. Therefore, create ``` context / META-INF / .gitignore``` as follows.



#### **`.gitignore`**
```python

/context.xml

Eclipse project settings

Set up the project in Eclipse as follows. As a result, when Tomcat is started with WTP, the files under `context / META-INF /` will be copied under` `` META-INF``` of the Web application.

  1. Open the project properties in Eclipse and open Deployment Assembly.

  2. Press the "Add ..." button and select "Folder" in the new assembly directive. assembly.png

  3. Select the context / META-INF directory and specify `META-INF /` for the Deployment Path to save your settings. context.png

Now when you start Tomcat using WTP, `context.xml``` (actually context _ ***. xml```) according to the developer will be `` WEB-INF / It is deployed under and you can switch the settings of the data source.

Batch file for creating symbolic links

setup-context.Create a bat with the following contents. In essence, you're just creating a symbolic link with the mklink command. If the symbolic link already exists, we will prompt you for confirmation and overwrite it.




#### **`setup-context.bat`**
```python

@echo off

set CONTEXT_DIR=..\META-INF
set CONTEXT_FILE=context.xml

%~d0
pushd %~p0

set USER_CONTEXT_DIR=%~p0

for %%I IN ( %USER_CONTEXT_DIR:~0,-1% ) do set "USER_CONTEXT_DIR=%%~nI"

cd %CONTEXT_DIR%

if not exist %CONTEXT_FILE% goto :MAKELINK

set /P INPUT="[WARNING] %CONTEXT_FILE%Exists.Delete Are you sure you want to? (y/n)"
if not %INPUT%==y goto :END

del %CONTEXT_FILE%

:MAKELINK
mklink %CONTEXT_FILE% ..\%USER_CONTEXT_DIR%\context_%USERNAME%.xml

:END
popd

pause

Operation method

Now you can operate in the following flow.

  1. When the number of developers increases, prepare a database for that developer, create `context_ <developer user name> .xml` to connect to it, and `` `context / users Place it under / ``` and commit.
  2. The developer clones the project and imports it into Eclipse.
  3. The developer runs context / user / setup-context.bat. [^ permission]
  4. `` `context / META-INF / context.xml``` is created as a symbolic link.
  5. Update by pressing F5 in Eclipse Package Explorer. Confirm that `context / META-INF / context.xml` is displayed.
  6. When you start Tomcat using WTP, `context / META-INF / context.xml` is copied under` `` META-INF``` of the web application according to the project settings of Eclipse, and the data Enabled as a source setting.

[^ permission]: At this time, if the message "You do not have sufficient privileges to perform this operation" is displayed, execute the batch file with administrator privileges.

There may be few development projects that use Tomcat data sources directly these days, but this may reduce the stress on the ground a little.

Recommended Posts

How to switch Tomcat context.xml with WTP in Eclipse
How to use Z3 library in Scala with Eclipse
How to use JDD library in Scala with Eclipse
How to run JUnit in Eclipse
How to set Lombok in Eclipse
How to deal with 405 Method Not Allowed error in Tomcat + JSP
How to switch thumbnail images with JavaScript
How to include Spring Tool in Eclipse 4.6.3?
How to switch Java version with direnv in terminal on Mac
[For beginners] How to debug in Eclipse
[JavaFX] How to write Eclipse permissions in build.gradle
How to color code console output in Eclipse
How to get started with Eclipse Micro Profile
How to automatically generate a constructor in Eclipse
To debug in eclipse
How to embed JavaScript variables in HTML with Thymeleaf
The right way to see the tomcat source in eclipse
How to sort in ascending / descending order with SQLite
How to call functions in bulk with Java reflection
Organized how to interact with the JDK in stages
[How to insert a video in haml with Rails]
How to delete untagged images in bulk with Docker
How to start tomcat local server without using eclipse
How to Git manage Java EE projects in Eclipse
How to query Array in jsonb with Rails + postgres
How to debug the generated jar file in Eclipse
How to number (number) with html.erb
How to update with activerecord-import
How to use Eclipse Debug_Shell
Connect to oracle with eclipse!
Install tomcat plugin in eclipse
How to get values in real time with TextWatcher (Android)
How to set character code and line feed code in Eclipse
How to dynamically switch JDK when building Java in Gradle
Mapping to a class with a value object in How to MyBatis
How to deploy Java application to Alibaba Cloud EDAS in Eclipse
How to switch Java in the OpenJDK era on Mac
How to set up a proxy with authentication in Feign
How to register as a customer with Square using Tomcat
How to dynamically switch between FIN and RST in Netty
How to use Lombok in Spring
How to scroll horizontally with ScrollView
How to find May'n in XPath
How to hide scrollbars in WebView
How to get started with slim
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to run Ant in Gradle
How to make a jar file with no dependencies in Maven
How to master programming in 3 months
How to enclose any character with "~"
I tried to build an http2 development environment with Eclipse + Tomcat
How to learn JAVA in 7 days
How to get parameters in Spark
How to install Bootstrap in Ruby
How to use mssql-tools with alpine
How to use InjectorHolder in OpenAM
How to run a job with docker login in AWS batch
How to get boolean value with jQuery in rails simple form
How to introduce jQuery in Rails 6
How to rename a model with foreign key constraints in Rails