본문 바로가기
SPRING

Tomcat 기동 시 폴더 생성

by ez.pang 2023. 7. 13.

Tomcat이 재기동 할 때 생기는 ROOT 폴더에 특정 폴더를 만들고 거기에 백업된 이미지데이터들을 불러와야할 일이 있었다.

startup.bat 에 서버 기동시 수행해야할 것들을 코드로 작성해놓아서 해결했다.

 

@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements.  See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License.  You may obtain a copy of the License at
rem
rem     http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem ---------------------------------------------------------------------------

setlocal

rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHome

set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"

rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end
:okExec

rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

call "%EXECUTABLE%" start %CMD_LINE_ARGS%

:end

이건 기존에 있던 startup.bat 파일이다.

 

@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements.  See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License.  You may obtain a copy of the License at
rem
rem     http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem ---------------------------------------------------------------------------

setlocal

rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHome

set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"

rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end
:okExec

rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

rd /s /q      "C:\Tomcat\webapps\ROOT"

call "%EXECUTABLE%" start %CMD_LINE_ARGS%

:waitForFolder
timeout /t 1 /nobreak >nul
if exist "C:\Tomcat\webapps\ROOT" goto createClaimFolder
goto waitForFolder

:createClaimFolder
mkdir "C:\Tomcat\webapps\ROOT\claim"
xcopy /E /-Y /I "C:\dnattach\pos\*" "C:\Tomcat\webapps\ROOT\claim"

:end

이게 수정한 파일인데, 추가한 내용을 보면

 

rd /s /q      "C:\Tomcat\webapps\ROOT"

기존에 있던 ROOT폴더를 지워야한다. ROOT 폴더가 남아있으면 새로 풀릴 ROOT.war가 제대로 안풀리는 듯

 

:waitForFolder
timeout /t 1 /nobreak >nul
if exist "C:\Tomcat\webapps\ROOT" goto createClaimFolder
goto waitForFolder

:createClaimFolder
mkdir "C:\Tomcat\webapps\ROOT\claim"
xcopy /E /-Y /I "C:\dnattach\pos\*" "C:\Tomcat\webapps\ROOT\claim"

 

call 명령어는 catalina.bat 을 실행시킨다.

Tomcat은 서버 기동시 자동으로 ROOT.war의 압축을 해제하여 ROOT 폴더를 만든다. 

ROOT 폴더가 만들어지길 기다리지 않고 내가 필요한 ROOT/claim 폴더를 만들 경우 ROOT.war 가 제대로 풀리질 않는다.

그래서 call명령어가 정상적으로 끝나 ROOT 폴더가 만들어질때까지 기다려야한다.

 

ROOT 폴더가 만들어졌다면 뒤쪽 소스가 작동

claim 폴더를 만들고 백업되어있던 곳에서 파일들을 복사해온다.

'SPRING' 카테고리의 다른 글

Jackson과 ObjectMapper  (0) 2023.01.03
OkHttpClient  (0) 2023.01.03
접근제어지시자와 정보은닉  (0) 2022.09.27
Properties 란?  (0) 2022.09.19

댓글