I needed to add content to the war directory/file that play generates via the “play war” command. The content varied from files added to the top level of the war (alongside WEB-INF) to customising WEB-INF/web.xml. Checking the core documentation, no support for this is mentioned. Checking the python scripts that come with play!, I found a snippet of hope:

    if os.path.exists(os.path.join(app.path, 'war')):
        copy_directory(os.path.join(app.path, 'war'), war_path)

A basic play app has the structure

myapp
    |-app
    |-conf
    |-lib
    |-public
    |-test

If you add a directory called “war” at the same level as app, conf, etc, this will be used as the basis of the generated war. So,

myapp
    |-app
    |-conf
    |-lib
    |-public
    |-test
    |-war
        |-deployment
	|   |-deployment.properties
	|-WEB-INF
	    |-web.xml
	    |-arbitrary.txt

will add the deployment directory alongside the WEB-INF folder, and the default web.xml supplied by play! will be replaced by the one you provide. arbitrary.txt will also be placed in WEB-INF.

Make sure sure that, if you replace web.xml, you keep all content found in $PLAY_HOME/resources/war/web.xml otherwise your webapp will not start properly! So, your custom web.xml might look like

<?xml version="1.0" ?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
             version="2.4">
  
        <!-- START: standard play content -->
        <display-name>Play! (%APPLICATION_NAME%)</display-name>
  
        <context-param>
            <param-name>play.id</param-name>
            <param-value>%PLAY_ID%</param-value>
        </context-param>
  
        <listener>
            <listener-class>play.server.ServletWrapper</listener-class>
        </listener>
  
        <servlet>
            <servlet-name>play</servlet-name>
            <servlet-class>play.server.ServletWrapper</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>play</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!-- END: standard play content -->

	<!-- START: custom content -->
		whatever
	<!-- END: custom content -->
    </web-app>

Note the use of fields denoted by % to enable play to update the content.

(As usual, once this was done I later found a mention in the optional GAE module – http://www.playframework.org/modules/gae-1.1/home)

2 thoughts on “quick&easy: Automatically add content to play!-generated war files

  1. I’m not english fluent, by I think there is a typo error :

    the default web.xml supplied be play!
    should be :
    the default web.xml supplied by play!

    Regards

    PS: And perhaps Play! (not play!) 😉

  2. be -> by…corrected.

    play! -> Play!…I’ve generally stopped using the correct capitalisation for things due to laziness 🙂

    Thanks for the correction!

Leave a Reply to steve Cancel reply

Your email address will not be published. Required fields are marked *