我正在编写一个 ant buildfile 来使用 eclipse 和 tomcat 7 服务器创建我的第一个 war 文件。如果您能帮我放松一下,我将不胜感激,因为这是我第一次创建战争文件。我已经得到了构建文件来创建一个war文件,但是当我解压缩war文件时,我发现它不包含它需要包含的所有内容。结果,当我尝试将应用程序以war文件的形式部署到tomcat时,web容器找不到应用程序中的任何servlet。我在下面发布我的输入、输出和代码。有人可以告诉我如何修复下面的代码,以便war文件中的输出与eclipse的输入相匹配吗?
以下是eclipse项目的结构:
以下是解压后的war文件内容:
这是 ant 构建文件的代码:
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="C:\mypath\workspace\myapp" name="myapp" default="default">
<target name="default" depends="setup,compile,buildwar,deploy"></target>
<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>
<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>
<target name="buildwar">
<war basedir="${basedir}/dist/web" destfile="myapp.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>
<target name="deploy">
<copy file="myapp.war" todir="${tomcat.deploydir}" />
</target>
</project>
请注意,运行此脚本会创建三个 war 文件副本。我只想要一份。
编辑:
我尝试了 Shaunak 的建议,现在当我解压缩 war 文件时得到以下输出:
将生成的 war 部署到 tomcat 仍然会产生 404 错误,因为 Web 容器找不到 servlet。我似乎在输出中找不到 web.xml 或 myapp.xml,war 文件中似乎也不存在任何 jar 文件依赖项,但好消息是包文件夹似乎同时包含 java 文件和类文件。
第二次编辑:
这是eclipse生成的ant构建文件。运行以下构建文件时应该创建的 .war 文件在哪里?战争文件的名称是什么?
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. --><project basedir="." default="build" name="myapp">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../../../java/eclipse"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.7"/>
<property name="source" value="1.7"/>
<path id="Web App Libraries.libraryclasspath"/>
<path id="EAR Libraries.libraryclasspath"/>
<path id="myapp.classpath">
<pathelement location="web/WEB-INF/classes"/>
<pathelement location="web/WEB-INF/lib/ant.jar"/>
<pathelement location="web/WEB-INF/lib/jaxen.jar"/>
<pathelement location="web/WEB-INF/lib/xalan.jar"/>
<pathelement location="web/WEB-INF/lib/xerces.jar"/>
<pathelement location="lib/commons-logging-1.1.3.jar"/>
<pathelement location="lib/commons-logging-1.1.3-javadoc.jar"/>
<pathelement location="lib/commons-logging-1.1.3-sources.jar"/>
<pathelement location="lib/commons-logging-1.1.3-test-sources.jar"/>
<pathelement location="lib/commons-logging-adapters-1.1.3.jar"/>
<pathelement location="lib/commons-logging-api-1.1.3.jar"/>
<pathelement location="lib/commons-logging-tests.jar"/>
<pathelement location="web/WEB-INF/lib/jstl-1.2.jar"/>
<pathelement location="web/WEB-INF/lib/commons-logging-1.1.3.jar"/>
<pathelement location="web/WEB-INF/lib/commons-logging-1.1.3-javadoc.jar"/>
<pathelement location="web/WEB-INF/lib/commons-logging-1.1.3-sources.jar"/>
<pathelement location="web/WEB-INF/lib/commons-logging-1.1.3-test-sources.jar"/>
<pathelement location="web/WEB-INF/lib/commons-logging-adapters-1.1.3.jar"/>
<pathelement location="web/WEB-INF/lib/commons-logging-api-1.1.3.jar"/>
<pathelement location="web/WEB-INF/lib/commons-logging-tests.jar"/>
<pathelement location="web/WEB-INF/lib/servlet-api.jar"/>
<pathelement location="web/WEB-INF/lib/jsp-api.jar"/>
<path refid="Web App Libraries.libraryclasspath"/>
<path refid="EAR Libraries.libraryclasspath"/>
</path>
<target name="init">
<mkdir dir="web/WEB-INF/classes"/>
<copy includeemptydirs="false" todir="web/WEB-INF/classes">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="web/WEB-INF/classes"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="web/WEB-INF/classes" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="myapp.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
<target name="GetBI">
<java classname="myapp.plotters.GetBI" failonerror="true" fork="yes">
<classpath refid="myapp.classpath"/>
</java>
</target>
<target name="GetPlotGIF (2)">
<java classname="myapp.plotters.GetPlotGIF" failonerror="true" fork="yes">
<classpath refid="myapp.classpath"/>
</java>
</target>
</project>