LoginSignup
10
12

More than 3 years have passed since last update.

War ファイルにコンテキスト固有の情報を設定する

Last updated at Posted at 2017-01-19

JavaEE の war ファイルの仕組みは簡単にデプロイできるため、とても便利な仕組みではあるのですが、ファイルシステムやアプリケーション・サーバとは独立して存在するため、外部の設定ファイルの読み込み、ログの出力先といったコンテキスト固有の情報が付加できないという問題があります。

そのような問題に対処するため、各アプリケーション・サーバそれぞれで独自の解決方法が用意されています。

Tomcat の場合

war ファイル内の /META-INF に context.xml を配置します。Parameter 要素を使うことで web.xml の context-param を設定したり、クラスパスを追加することが可能です。デプロイの直前に war ファイル内に追加するのが良いでしょう。

/META-INF/context.xml
<Context>
  <!-- context-param の追加 -->
  <Parameter name="WEBAPP_HOME" value="/webapp_home" override="false" />

  <!-- クラスパスの追加 -->
  <Resources>
    <PreResources className="org.apache.catalina.webresources.DirResourceSet"
        base="/app/lib" webAppMount="/WEB-INF/lib" />
  </Resources>
</Context>

なお、autoDeploy や deployOnStartup を使ってデプロイする場合には Context の path 属性は無視されるため、コンテキストパスを変えたい場合は、webapps にコピーするときにファイル名自体を変える必要があります。

[参考] Apache Tomcat 8 Configuration Reference - The Context Container
[参考] Apache Tomcat 8 Configuration Reference - The Resources Component

Jetty の場合

war ファイル内の /WEB-INF に jetty-web.xml を配置します。WebAppContext の contextPath を設定したり、ServletContext の setInitParameter に値を設定することで context-param の設定が可能です。

/WEB-INF/jetty-web.xml
<?xml version="1.0"  encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC
    "-//Mort Bay Consulting//DTD Configure//EN"
    "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/contextpath</Set>
    <Get name="ServletContext">
        <Call name="setInitParameter">
            <Arg>WEBAPP_HOME</Arg>
            <Arg>/webapp_home</Arg>
        </Call>
    </Get>
</Configure>

[参考] Jetty The Definitive Reference - What to Configure in Jetty

JBoss の場合

JBoss の中身は Tomcat なのですが、意外にも設定方法には違いがあります。war ファイル内の /WEB-INF あるいは /META-INF に jboss-web.xml を配置します。コンテキストパスの設定は可能ですが、context-param の指定はできません。/META-INF/context.xml は JBoss AS7 では無視されるようです。

/WEB-INF/jboss-web.xml
<jboss-web>
    <context-root>/contextpath</context-root>
</jboss-web>

[参考] JBoss Web Application Deployment Descriptor
[参考] The Context Container

Weblogic の場合

war ファイル内の /WEB-INF に weblogic.xml を配置します。コンテキストパスの設定は可能ですが、パラメータの指定はできません。prefer-web-inf-classes は、Weblogic 付属のライブラリよりも war ファイル内のライブラリを優先する記述です。クラスロード時にエラーが発生する場合には、指定するとよいでしょう。

/WEB-INF/weblogic.xml
<?xml version="1.0" encoding="UTF-8"?> 
<wls:weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wls="http://www.bea.com/ns/weblogic/90" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd"> 
    <wls:weblogic-version>12.1.1</wls:weblogic-version> 
    <wls:context-root>contextpath</wls:context-root> 
    <wls:container-descriptor> 
         <wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes> 
    </wls:container-descriptor> 
</wls:weblogic-web-app>

バージョンが変わると、記載内容やタグが変わるので、注意が必要です。

[参考] Installation of Jenkins into Weblogic

10
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
12