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).
There are two main locations for context.xml.
META-INF / context.xml
under the Web application $ 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.
--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 **.
--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.
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.
Create new `context / META-INF``` and
`context / user``` directories under the Eclipse project.
context/meta-inf
The contents of are in the web application according to the settings described below.meta-inf
It 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>
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
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.
Open the project properties in Eclipse and open Deployment Assembly.
Press the "Add ..." button and select "Folder" in the new assembly directive.
Select the context / META-INF
directory and specify `META-INF /`
for the Deployment Path to save your settings.
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.
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
Now you can operate in the following flow.
`context_ <developer user name> .xml`
to connect to it, and `` `context / users Place it under / ``` and commit. context / user / setup-context.bat
. [^ permission]`context / META-INF / context.xml`
is displayed.`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