如何创建war文件 [英] How to create war files

查看:44
本文介绍了如何创建war文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建war文件(使用eclipse)在tomcat上运行的最佳实践是什么?教程、链接、示例受到高度赞赏.

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

推荐答案

您可以使用 Ant设置、编译、WAR,然后部署您的解决方案.

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

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

然后您可以在 Eclipse 中执行一键操作来运行该 Ant 目标.以下是每个步骤的示例:

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

我们假设您的代码组织如下:

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

  • ${basedir}/src:Java 文件、属性、XML 配置文件
  • ${basedir}/web:您的 JSP 文件
  • ${basedir}/web/lib:运行时需要的任何 JAR
  • ${basedir}/web/META-INF:您的清单
  • ${basedir}/web/WEB-INF:您的 web.xml 文件
  • ${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

定义一个 setup 任务来创建分发目录并直接复制任何需要 WARred 的工件:

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>

编译

将您的 Java 文件构建到类中,并复制驻留在 src 下但需要在运行时可用的任何非 Java 工件(例如属性、XML 文件等):

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>

构建战争

创建 WAR 本身:

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>

部署

最后,您可以设置一个任务,将 WAR 直接部署到您的 Tomcat 部署位置:

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>

点击即可!

一旦所有这些设置完毕,只需从 Eclipse 启动 default 目标即可编译、WAR 和部署您的解决方案.

Click and go!

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

这种方法的优点是它既可以在 Eclipse 之外也可以在 Eclipse 中工作,并且可以用来与同样在您的项目上工作的其他开发人员轻松共享您的部署策略(例如,通过源代码控制).

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.

这篇关于如何创建war文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆