score:100

Accepted answer

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.

score:-3

**making war file in eclips gaynemed of grails web project **

1.import project:

2.change the datasource.groovy file

like this: url="jdbc:postgresql://18.247.120.101:8432/pgms"

2.chnge appconfig.xml

3.kill the java from task manager:

  1. run clean comand in eclips

  2. run 'prod war' fowllowed by project name.

  3. check the log file and find the same .war file in directory of workbench with same date.

score:0

simpler solution which also refreshes the eclipse workspace:

<?xml version="1.0" encoding="utf-8"?>
<project name="project" default="default">    
    <target name="default">
        <war destfile="target/myapplication.war" webxml="web/web-inf/web.xml">
            <fileset dir="src/main/java" />
            <fileset dir="web/web-inf/views" />
            <lib dir="web/web-inf/lib"/>
            <classes dir="target/classes" />
        </war>
        <eclipse.refreshlocal resource="myapplication/target" depth="infinite"/>
    </target>
</project>

score:0

simplistic shell code for creating war files from a standard eclipse dynamic web project. uses ram file system (/dev/shm) on a linux platform.

#!/bin/sh

utility=$(basename $0)

if [ -z "$1" ] ; then
    echo "usage: $utility [-s] <web-app-directory>..."
    echo "       -s ..... with source"
    exit 1
fi

if [ "$1" == "-s" ] ; then
    with_source=1
    shift
fi

while [ ! -z "$1" ] ; do
    web_app_dir=$1

    shift

    if [ ! -d $web_app_dir ] ; then
        echo "\"$web_app_dir\" is not a directory"
        continue
    fi

    if [ ! -d $web_app_dir/webcontent ] ; then
        echo "\"$web_app_dir\" is not a web application directory"
        continue
    fi

    tmp_dir=/dev/shm/${web_app_dir}.$$.tmp
    war_file=/dev/shm/${web_app_dir}.war

    mkdir $tmp_dir

    pushd $web_app_dir > /dev/null
    cp -r webcontent/* $tmp_dir
    cp -r build/* $tmp_dir/web-inf
    [ ! -z "$with_source" ] && cp -r src/* $tmp_dir/web-inf/classes
    cd $tmp_dir > /dev/null
    [ -e $war_file ] && rm -f $war_file
    jar cf $war_file .
    ls -lsf $war_file
    popd > /dev/null

    rm -rf $tmp_dir
done

score:2

another option would be to build it automatically using eclipse. of course if you have continuous integration environment ant or maven is recommended. the export alternative is not very convenient because you have to configure every time the export properties.

steps:

  1. enable "project archives" support; this might depend on your project (i used it on java ee/web project). right-click project root directory; configure -> add project archives support.

  2. go and create a new archive in the "project archives" top dir. you have only jar option, but name you archive *.war.

  3. configure fileset-s, i.e what files to be included. typical is to configure two filesets similar how the web deployment assembly (project property) is configured.

    • copy /webcontent to /
    • copy /build/classes to web-inf/classes (create this fileset after you define the web-inf/classes directory in the archive)
  4. you might need to tweek the fileset exclude property depending where you placed some of the config files or you might need more filesets, but the idea is that once you configured this you don't need to change it.

  5. build the archive manually or publish directly to server; but is also automatically built for you by eclipse

score:2

another common option is gradle.

http://www.gradle.org/docs/current/userguide/application_plugin.html

to build your war file in a web app:

in build.gradle, add:

apply plugin: 'war'

then:

./gradlew war

use the layout from accepted answer above.

score:3

use ant build code i use this for my project sms

<property name="web-inf" value="${basedir}/webroot/web-inf" />
<property name="out" value="${basedir}/out" />
<property name="war_file_name" value="mywebapplication.war" />
<property name="temp" value="${basedir}/temp" />

<target name="help">
    <echo>
        --------------------------------------------------
        compile - compile
        archive - generate war file
        --------------------------------------------------
    </echo>
</target>

<target name="init">
    <delete dir="${web-inf}/classes" />
    <mkdir dir="${web-inf}/classes" />
</target>

<target name="compile" depends="init">
    <javac srcdir="${basedir}/src" 
                destdir="${web-inf}/classes" 
                classpathref="libs">
    </javac>
</target>

<target name="archive" depends="compile">
    <delete dir="${out}" />
    <mkdir dir="${out}" />
    <delete dir="${temp}" />
    <mkdir dir="${temp}" />
    <copy todir="${temp}" >
        <fileset dir="${basedir}/webroot">
        </fileset>
    </copy>
    <move file="${temp}/log4j.properties" 
                    todir="${temp}/web-inf/classes" />
    <war destfile="${out}/${war_file_name}" 
                    basedir="${temp}" 
                    compress="true" 
                    webxml="${temp}/web-inf/web.xml" />
    <delete dir="${temp}" />
</target>

<path id="libs">
    <fileset includes="*.jar" dir="${web-inf}/lib" />
</path>

score:4

use the ant war task

score:4

use the following command outside the web-inf folder. this should create your war file and is the quickest method i know.

(you will need jdk 1.7+ installed and environment variables that point to the bin directory of your jdk.)

jar -cvf projectname.war *

reference link

score:12

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.

score:15

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.

score:23

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>

score:38

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.


Related Query

More Query from same tag