How to create war files

184,453

Solution 1

You can use Ant to set up, compile, WAR, and deploy your solution.

<target name="default" depends="setup,compile,buildwar,deploy"></target>

You can then execute one click in Eclipse to run that Ant target. Here are examples of each of the steps:

Preconditions

We'll assume that you have your code organized like:

  • ${basedir}/src: Java files, properties, XML config files
  • ${basedir}/web: Your JSP files
  • ${basedir}/web/lib: Any JARs required at runtime
  • ${basedir}/web/META-INF: Your manifest
  • ${basedir}/web/WEB-INF: Your web.xml files

Set up

Define a setup task that creates the distribution directory and copies any artifacts that need to be WARred directly:

<target name="setup">
    <mkdir dir="dist" />
    <echo>Copying web into dist</echo>
    <copydir dest="dist/web" src="web" />
    <copydir dest="dist/web/WEB-INF/lib" src="${basedir}/../web/WEB-INF/lib" />
</target>

Compile

Build your Java files into classes and copy over any non-Java artifacts that reside under src but need to be available at runtime (e.g. properties, XML files, etc.):

<target name="compile">
    <delete dir="${dist.dir}/web/WEB-INF/classes" />
    <mkdir dir="${dist.dir}/web/WEB-INF/classes" />
    <javac destdir="${dist.dir}/web/WEB-INF/classes" srcdir="src">
        <classpath>
            <fileset dir="${basedir}/../web/WEB-INF/lib">
                  <include name="*" />
            </fileset>
        </classpath>
    </javac>
    <copy todir="${dist.dir}/web/WEB-INF/classes">
        <fileset dir="src">
            <include name="**/*.properties" />
            <include name="**/*.xml" />
        </fileset>
    </copy>
</target>

Build WAR

Create the WAR itself:

<target name="buildwar">
    <war basedir="${basedir}/dist/web" destfile="My.war"
     webxml="${basedir}/dist/web/WEB-INF/web.xml">
        <exclude name="WEB-INF/**" />
        <webinf dir="${basedir}/dist/web/WEB-INF/">
            <include name="**/*.jar" />
        </webinf>
    </war>
</target>

Deploy

Finally, you can set up a task to deploy the WAR directly into your Tomcat deploy location:

<target name="deploy">
    <copy file="My.war" todir="${tomcat.deploydir}" />
</target>

Click and go!

Once all this is set up, simply launching the default target from Eclipse will compile, WAR, and deploy your solution.

The advantage of this approach is that it will work outside Eclipse as well as within Eclipse and can be used to easily share your deployment strategy (e.g. via source control) with other developers who are also working on your project.

Solution 2

I've always just selected Export from Eclipse. It builds the war file and includes all necessary files. Providing you created the project as a web project that's all you'll need to do. Eclipse makes it very simple to do.

Solution 3

We use Maven (Ant's big brother) for all our java projects, and it has a very nifty WAR plugin. Tutorials and usage can be found there.

It's a lot easier than Ant, fully compatible with Eclipse (use maven eclipse:eclipse to create Eclipse projects) and easy to configure.

Maven's homepage

Maven WAR plugin

Sample Configuration:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1-alpha-2</version>
    <configuration>
        <outputDirectory>${project.build.directory}/tmp/</outputDirectory>
        <workDirectory>${project.build.directory}/tmp/war/work</workDirectory>
        <webappDirectory>${project.build.webappDirectory}</webappDirectory>
        <cacheFile>${project.build.directory}/tmp/war/work/webapp-cache.xml</cacheFile>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
            <nonFilteredFileExtension>png</nonFilteredFileExtension>
            <nonFilteredFileExtension>gif</nonFilteredFileExtension>
            <nonFilteredFileExtension>jsp</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
        <webResources>
            <resource>
                <directory>src/main/webapp/</directory>
                <targetPath>WEB-INF</targetPath>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </webResources>
        <warName>Application</warName>
    </configuration>
</plugin>

Solution 4

A war file is simply a jar file with a war extension, but what makes it work is how the contents is actually structured.

The J2EE/Java EE tutorial can be a start:

http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WebComponents3.html

And the Servlet specification contains the gory details:

http://java.sun.com/products/servlet/download.html

If you create a new web project in Eclipse (I am referring to the Java EE version), the structure is created for you and you can also tell it where your Appserver is installed and it will deploy and start the application for you.

Using the "Export->WAR file" option will let you save the war file.

Solution 5

If you are not sure what to do and are starting from scratch then Maven can help get you started.

By following the the below steps you can get a new war project setup perfectly in eclipse.

  1. Download and install Maven
  2. Go the command line run: mvn archetype:generate
  3. Follow the prompted steps - choosing the simple java web project (18) and a suitable name.
  4. When it is finished run: mvn eclipse:eclipse
  5. Start Eclipse. Choose File -> Import -> Existing project. Select the directory where you ran the mvn goals.
  6. That's it you should now have a very good start to a war project in eclipse
  7. You can create the war itself by running mvn package or deploy it by setting up a server in eclipse and simply adding adding the project to the server.

As some others have said the downside of using maven is that you have to use the maven conventions. But I think if you are just starting out, learning the conventions is a good idea before you start making your own. There's nothing to stop you changing/refactoring to your own preferred method at a later point.

Hope this helps.

Share:
184,453
Admin
Author by

Admin

Updated on July 08, 2022

Comments

  • Admin
    Admin almost 2 years

    What are the best practices of creating war files (using eclipse) to run on tomcat? tutorials, links, examples are highly appreciated.

  • David Citron
    David Citron almost 15 years
    I gave you your point back (though it wasn't I who removed it). Maven is certainly a reasonable choice if you happen to (want to?) fit into their particular version of "convention over configuration."
  • mikek
    mikek almost 15 years
    I happen to like it =) I'm working as CM on a pretty big project, and I appreciate having the grunt work of project configuration done for me. Also, the plugin support is wonderful. Being able to have a multi-module project up and running with a WAR-plugin, with Surefire and CheckStyle in five minutes is a treat. Ant, while more open and arguably more powerful, is a hassle in comparison. Anyway, Maven is a subset to Ant, so if you need to do any scripting there, you're welcome.
  • Admin
    Admin almost 15 years
    well I want to deploy it to tomcat. How should I change the targer?
  • David Citron
    David Citron almost 15 years
    Could you please elaborate? As long as ${tomcat.deploydir} points to your Tomcat context deployment directory, that should do it. No?
  • Brad Parks
    Brad Parks about 12 years
    Thanks! and for what it's worth, you select "Export" from the "File" menu, then in "Select an Export Destination" type "WAR" and the war file option will come up.
  • Renan
    Renan about 12 years
    my choice for quick and dirty web apps. writing an ant script just to get a test WAR out is just too much work.
  • mikek
    mikek almost 12 years
    Besides Maven's XML structure being based on ANT's, and ANT being incorporated as a subset of Maven.
  • M.C.
    M.C. about 11 years
    another solution instead of creating an ant file would to go to the folder you want to wrap in WAR file and execute this command "jar -cvf name.war *" .
  • ankitjaininfo
    ankitjaininfo about 11 years
    In the war target there is unnecessary '<exclude name="WEB-INF/**" />'. You have generated *.class files into this folder and excluding them does not work. Remove this exclude line from the war target it works.
  • simon
    simon about 11 years
    Thanks for this, a LOT simpler than configuring Maven or Ant (when it's not necesarily needed).
  • Alex R
    Alex R about 9 years
    This answer is needlessly complicated.
  • Jawad Al Shaikh
    Jawad Al Shaikh over 3 years
    If you receive 'jar' is not recognized as an internal or external command then try "%JAVA_HOME%\bin\jar" -cvf projectname.war *
  • MAbraham1
    MAbraham1 almost 3 years
    Oops. This is probably from their team notes.