Java 使用eclipse和Tomcat7服务器使用ant创建war文件时出现问题
我正在编写一个ant构建文件,以使用eclipse和Tomcat7服务器创建我的第一个war文件。如果您能让我放松一点,我将不胜感激,因为这是我第一次创建war文件。我已经获得了构建文件来创建war文件,但是当我解压缩war文件时,我发现它并没有包含它需要包含的所有内容。因此,当我试图以war文件的形式将应用程序部署到tomcat时,web容器在应用程序中找不到任何servlet。我在下面发布我的输入、输出和代码。有人能告诉我如何修复下面的代码,以便war文件中的输出与eclipse中的输入匹配吗 以下是eclipse项目的结构: 以下是解压缩的war文件的内容: 下面是ant构建文件的代码:Java 使用eclipse和Tomcat7服务器使用ant创建war文件时出现问题,java,eclipse,tomcat,ant,war,Java,Eclipse,Tomcat,Ant,War,我正在编写一个ant构建文件,以使用eclipse和Tomcat7服务器创建我的第一个war文件。如果您能让我放松一点,我将不胜感激,因为这是我第一次创建war文件。我已经获得了构建文件来创建war文件,但是当我解压缩war文件时,我发现它并没有包含它需要包含的所有内容。因此,当我试图以war文件的形式将应用程序部署到tomcat时,web容器在应用程序中找不到任何servlet。我在下面发布我的输入、输出和代码。有人能告诉我如何修复下面的代码,以便war文件中的输出与eclipse中的输入匹配
<?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>
Eclipse会自动为您生成一个ant构建文件。你试过用它吗?这通常是一个很好的稳定起点,您可以开始修改它。右键单击项目->导出->键入Ant->导出。如果您已经在eclipse中安装了tomcat或其他服务器,并且eclipse能够在那里部署它,那么这将工作得更好。@Shaunak谢谢。我尝试了你的方法,并将结果发布在我的原始帖子上面。你在使用插件吗?我正在使用EclipseJuno版本1。右键单击项目文件夹并选择“导出”将提供war选项,但不是ant选项。我选择了war选项,并在编辑中报告了结果。还有其他建议吗?是的,我用朱诺。选择“导出”时,在文本输入中键入ant并按enter键。它应该给你一个选择。如果没有,请告诉我,然后我们可以调试它further@Shaunak谢谢我找到了ant选项,它使eclipse能够创建一个200多行的ant构建文件。但是当我运行ant文件时,我找不到生成的war文件。在windows开始菜单中键入myapp.war不会产生任何结果。有什么建议吗?我认为默认情况下,它会将war文件放入您的web服务器。您必须检查在eclipse中配置服务器的路径。