Java 如何在 Tomcat 6 中部署战争

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2119678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:43:59  来源:igfitidea点击:

How can I deploy war in Tomcat 6

javadeploymenttomcat

提问by newbie

I have used Jboss and deployment happens by dropping war to deploy folder. But when I set my war project to Tomcat server eclipse claims that it is deploying war, but I cannot see my war in webapps folder. And what url is to my war? In jboss I can add app url to jboss-web.xml, so when I set app path to jboss-web.xml I can find my app from http://localhost:8080/my_app_path_in_jboss_web.xml/.

我使用过 Jboss,部署是通过将 war 放到部署文件夹来实现的。但是当我将我的战争项目设置为 Tomcat 服务器时,eclipse 声称它正在部署战争,但我在 webapps 文件夹中看不到我的战争。我的战争的网址是什么?在 jboss 中,我可以将应用程序 url 添加到 jboss-web.xml,因此当我将应用程序路径设置为 jboss-web.xml 时,我可以从http://localhost:8080/my_app_path_in_jboss_web.xml/找到我的应用程序。

采纳答案by Michael Krauklis

I deploy WARs to the webapps dir all the time without issue, probably ~25 times a day. You shouldn't have any problems with that. If you want more control by all means deploy the directory yourself, but this will most likely require a restart.

我一直将 WAR 部署到 webapps 目录,没有问题,每天大约 25 次。你不应该有任何问题。如果您想通过各种方式获得更多控制权,请自行部署目录,但这很可能需要重新启动。

回答by Bill

Using ant simplifies the task. Here is an an ant build.xml file I use to deploy to tomcat 6. (note, I wasn't the origial author of this, but I forget where I got it from)

使用 ant 可以简化任务。这是一个我用来部署到 tomcat 6 的 ant build.xml 文件。(注意,我不是这个的原始作者,但我忘记了我从哪里得到的)

<project name="provisioning" basedir="." default="usage">
<property file="build.properties"/>

<property name="src.dir" value="src"/>
<property name="web.dir" value="war"/>
<property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
<property name="name" value="provisioning"/>

<path id="master-classpath">
    <fileset dir="${web.dir}/WEB-INF/lib">
        <include name="*.jar"/>
    </fileset>
    <!-- We need the servlet API classes: -->
    <!--  * for Tomcat 5/6 use servlet-api.jar -->
    <!--  * for other app servers - check the docs -->
    <fileset dir="${appserver.lib}">
        <include name="servlet*.jar"/>
    </fileset>
    <pathelement path="${build.dir}"/>
</path>

<target name="usage">
    <echo message=""/>
    <echo message="${name} build file"/>
    <echo message="-----------------------------------"/>
    <echo message=""/>
    <echo message="Available targets are:"/>
    <echo message=""/>
    <echo message="build     --> Build the application"/>
    <echo message="deploy    --> Deploy application as directory"/>
    <echo message="deploywar --> Deploy application as a WAR file"/>
    <echo message="install   --> Install application in Tomcat"/>
    <echo message="reload    --> Reload application in Tomcat"/>
    <echo message="start     --> Start Tomcat application"/>
    <echo message="stop      --> Stop Tomcat application"/>
    <echo message="list      --> List Tomcat applications"/>
    <echo message=""/>
</target>

<target name="build" description="Compile main source tree java files">
    <mkdir dir="${build.dir}"/>
    <javac destdir="${build.dir}" source="1.6" target="1.6" debug="true"
           deprecation="false" optimize="false" failonerror="true">
        <src path="${src.dir}"/>
        <classpath refid="master-classpath"/>
    </javac>
</target>

<target name="deploy" depends="build" description="Deploy application">
    <copy todir="${deploy.path}/${name}" preservelastmodified="true">
        <fileset dir="${web.dir}">
            <include name="**/*.*"/>
        </fileset>
    </copy>
</target>

<target name="deploywar" depends="build" description="Deploy application as a WAR file">
    <war destfile="${name}.war"
         webxml="${web.dir}/WEB-INF/web.xml">
        <fileset dir="${web.dir}">
            <include name="**/*.*"/>
        </fileset>
    </war>
    <copy todir="${deploy.path}" preservelastmodified="true">
        <fileset dir=".">
            <include name="*.war"/>
        </fileset>
    </copy>
</target>

<path id="catalina-ant-classpath">
    <!-- We need the Catalina jars for Tomcat -->
    <!--  * for other app servers - check the docs -->
    <fileset dir="${appserver.lib}">
        <include name="catalina-ant.jar"/>
    </fileset>
</path>

<taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
    <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
    <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
    <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask">
    <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
    <classpath refid="catalina-ant-classpath"/>
</taskdef>

<target name="install" description="Install application in Tomcat">
    <install url="${tomcat.manager.url}"
             username="${tomcat.manager.username}"
             password="${tomcat.manager.password}"
             path="/${name}"
             war="${name}"/>
</target>

<target name="reload" description="Reload application in Tomcat">
    <reload url="${tomcat.manager.url}"
             username="${tomcat.manager.username}"
             password="${tomcat.manager.password}"
             path="/${name}"/>
</target>

<target name="start" description="Start Tomcat application">
    <start url="${tomcat.manager.url}"
             username="${tomcat.manager.username}"
             password="${tomcat.manager.password}"
             path="/${name}"/>
</target>

<target name="stop" description="Stop Tomcat application">
    <stop url="${tomcat.manager.url}"
             username="${tomcat.manager.username}"
             password="${tomcat.manager.password}"
             path="/${name}"/>
</target>

<target name="list" description="List Tomcat applications">
    <list url="${tomcat.manager.url}"
             username="${tomcat.manager.username}"
             password="${tomcat.manager.password}"/>
</target>

回答by Dave.Sol

If I understand correctly, you have configured eclipse to use Tomcat by adding a new Server. In that case eclipse deploys the war file in folder inside your workspace, NOT at $TOMCAT_HOME/webapps. I can't remember the path in eclipse's workspace folder from the top of my head - it should be something that contains tmp, server, ...

如果我理解正确,您已经通过添加新服务器将 Eclipse 配置为使用 Tomcat。在这种情况下,eclipse 会将 war 文件部署在您的工作区中的文件夹中,而不是在 $TOMCAT_HOME/webapps 中。我从头顶上记不起 eclipse 工作区文件夹中的路径 - 它应该是包含 tmp、server、...

回答by Mark R

You have added Tomcat server in eclipse, eclipse runs the WAR file within your workspace. Eclipse does not use your %TOMCAT_HOME%. To be able to see your WAR in the webapps folder simply drop it in there. You will then be albe to run the war from localhost.

您已经在 eclipse 中添加了 Tomcat 服务器,eclipse 在您的工作区中运行 WAR 文件。Eclipse 不使用您的 %TOMCAT_HOME%。为了能够在 webapps 文件夹中看到您的 WAR,只需将它放在那里。然后,您将可以从本地主机运行战争。