Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从EclipseWeb项目创建war文件_Java_Eclipse_Tomcat_Servlets_Ant - Fatal编程技术网

Java 从EclipseWeb项目创建war文件

Java 从EclipseWeb项目创建war文件,java,eclipse,tomcat,servlets,ant,Java,Eclipse,Tomcat,Servlets,Ant,我一直在适应制作war文件,并创建了上图中显示的BuildWar.xml文件的以下内容: <?xml version="1.0" encoding="UTF-8"?> <project name="myproject" default="default"> <target name="default" depends="setup,compile,buildwar,deploy"></target> <target nam

我一直在适应制作war文件,并创建了上图中显示的BuildWar.xml文件的以下内容:

<?xml version="1.0" encoding="UTF-8"?>

<project name="myproject" 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}/myapp/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="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>

    <target name="deploy">
        <copy file="My.war" todir="${tomcat.deploydir}" />
    </target>

</project>
但是,当我更换。。在myapp的第10行,我收到一条错误消息说

BUILD FAILED
C:\mypath\myapp\BuildWar.xml:10: srcdir C:\mypath\myapp\myapp\web\WEB-INF\lib does not exist!
如何修复代码,使其不再发出构建失败消息

我认为这句话:

应该是:


${basedir}引用C:\mypath\myapp,如果Ant是从C:\mypath\myapp运行的(或者BuildWar.xml所在的位置)

将其作为项目标记的属性声明在顶部

<?xml version="1.0" encoding="UTF-8"?>

<project basedir="C:\......." name="myproject" 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}/myapp/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="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>

<target name="deploy">
    <copy file="My.war" todir="${tomcat.deploydir}" />
</target>

</project>

将web复制到dist

声明基本目录后,请写入与基本目录相关的所有路径。

首先,请指定
myapp
项目文件夹在文件系统中的位置?我看到您正在使用
myapp/myapp
路径(您在上次失败的消息中有3次这样做)我没有看到“basedir”的声明,也没有看到构建war文件用户target name=war。您似乎缺少关于如何运行java和编译文件的关键信息。a) 您的
JAVA\u HOME
变量应指向
C:\mypath\JAVA\jdk1.7.0\u 17
。您还应该找到
PATH
变量并添加
C:\mypath\Java\jdk1.7.0\u 17\bin
。如果所有设置都正确,并且您从命令行运行
java-version
,您应该看到
java版本1.7.017
b),然后转到Eclipse
Windows->Preferences->java->Installed JREs
,并添加一个指向上述JDK的新运行时(如果还没有)。如果是,只需选择它作为默认值。然后确保您的项目使用的JDKYour类
myapp.web.HomeServlet
未找到。如果这应该转到
WEB-INF/classes
,那么ant脚本没有完成它的工作。war文件实际上是一个zip文件。将它解压缩到某个地方,并检查是否所有的类和jar都存在于它们应该存在的地方。与您的web文件夹进行比较。然后检查ant脚本为什么没有复制丢失的项C:\Program Files\Java\jre7是Java运行时环境,也就是说,它足以运行Java代码;但是要编译Java代码,您需要一个JDK。如果您安装了一个,那么将JAVA_HOME设置为指向JDK而不是JRE;否则,请访问java.oracle.com并下载/安装一个。要设置java_Home环境变量,请参阅此链接。这很容易。computerhope.com/issues/ch000549.htm
<?xml version="1.0" encoding="UTF-8"?>

<project basedir="C:\......." name="myproject" 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}/myapp/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="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>

<target name="deploy">
    <copy file="My.war" todir="${tomcat.deploydir}" />
</target>

</project>