由于&#34;包javax.servlet不存在,Ant <javac>失败&#34;

时间:2015-12-03 09:08:43

标签: java ant compilation javac

我正在尝试编译AdminOps.java文件,该文件是软件包的一部分,adminOps

这是文件结构:

  

C:\用户\ HP \ GIT中\ FLS-web的露\ SRC \ adminOps \ AdminOps.java

根据Ant输出,我认为Ant无法找到javax.servlet

我尝试手动导入javax.servlet-api-3.1.0.jar文件,但我仍然收到错误。我使用build.xml文件,以便我可以使用XAMPP创建一个WAR文件并将其部署在localhost上。

有人能帮我找出错误吗?

Ant脚本

<?xml version="1.0" encoding="UTF-8"?>
<project name="friendlease" default="main" basedir=".">
    <property name="src.java" value="${basedir}/src/"/>
    <property name="build.dir" value="${basedir}/build"/>
    <property name="jar.dir" value="${build.dir}/jar"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="lib.dir" value="${basedir}/lib"/>
    <property name="tp.dir" value="${jar.dir}/thirdparty"/>
    <property name="jar.name" value="${ant.project.name}.jar"/>
    <property name="jar.file" value="${jar.dir}/${jar.name}"/>
    <property name="war.file" value="${build.dir}/${ant.project.name}.war"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="setup">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${jar.dir}"/>
        <!--
        <for list="${dependencies}" trim="true" param="dependency">
            <sequential>
                <echo message="___ Building dependency @{dependency} ___"/>
                <ant antfile="@{dependency}/build.xml" inheritall="false"
                    target="jar"/>
                <copy todir="${jar.dir}">
                    <fileset dir="@{dependency}/${jar.dir}"/>
                </copy>
            </sequential>
        </for>
        -->
        <copy todir="${tp.dir}/">
            <fileset dir="${lib.dir}"/>
        </copy>
        <path id="classpath">
            <fileset dir="${jar.dir}" includes="**/*.jar"/>
        </path>
    </target>

    <target name="compile" depends="setup">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.java}" destdir="${classes.dir}"
            classpathref="classpath" includeantruntime="false"
            debug="${compile.debug}" debuglevel="lines,vars,source"/>
    </target>

    <target name="test-compile" depends="compile">
        <mkdir dir="${test.classes.dir}"/>
        <path id="test.compile.classpath">
            <pathelement path="${classes.dir}"/>
            <pathelement location="${test.testng.jar}"/>
            <path refid="classpath"/>
        </path>
        <javac srcdir="${test.src.java}" destdir="${test.classes.dir}"
            classpathref="test.compile.classpath" includeantruntime="false"/>
    </target>

    <target name="test-run" depends="test-compile">
        <path id="test.run.classpath">
            <pathelement path="${test.classes.dir}"/>
            <path refid="test.compile.classpath"/>
        </path>
        <testng classpathref="test.run.classpath"
            outputdir="${test.report.dir}" haltonfailure="true">
            <classfileset dir="${test.classes.dir}" includes="**/*.class"/>
        </testng>
    </target>

    <target name="jar" depends="compile">
        <pathconvert property="runtime.classpath" refid="classpath">
            <regexpmapper from=".+/jar/(.+)" to="\1"/>
        </pathconvert>
        <manifestclasspath property="manifest.classpath"
            jarfile="${broken.on.purpose}">
            <classpath>
                <pathelement path="${runtime.classpath}"/>
            </classpath>
        </manifestclasspath>
        <jar destfile="${jar.file}" basedir="${classes.dir}">
            <manifest>
                <attribute name="Build-Version" value="${build.version}"/>
                <attribute name="Class-Path" value="${manifest.classpath}"/>
            </manifest>
        </jar>
    </target>

    <target name="war" depends="jar">
        <war destfile="${war.file}"
            webxml="${basedir}/WebContent/WEB-INF/web.xml">
            <lib dir="${jar.dir}" includes="*.jar"/>
            <lib dir="${tp.dir}" includes="*.jar">
                <exclude name="servlet-api.jar"/>
                <exclude name="javax.servlet-3.0.jar"/>
            </lib>
            <zipfileset dir="${basedir}/WebContent"
        excludes="WEB-INF/web*.xml"
        />
        </war>
    </target>

    <target name="main" depends="clean, war"/>
</project>

环境变量

ANT_HOME=C:\apache-ant-1.9.6
Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Skype\Phone\;%ANT_HOME%\bin;%JAVA_HOME%\bin

AdminOps.java

package adminOps;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AdminOps extends HttpServlet {
//some code here...
}

Ant输出

Buildfile: C:\Users\Hp\git\fls-web-Lucy\build.xml
clean:
    [delete] Deleting directory C:\Users\Hp\git\fls-web-Lucy\build
setup:
    [mkdir] Created dir: C:\Users\Hp\git\fls-web-Lucy\build
    [mkdir] Created dir: C:\Users\Hp\git\fls-web-Lucy\build\jar
    [copy] Copying 1 files to C:\Users\Hp\git\fls-web-Lucy\build\jar\thirdparty
compile:
    [mkdir] Created dir: C:\Users\Hp\git\fls-web-Lucy\build\classes
    [javac] Compiling 1 source files to C:\Users\Hp\git\fls-web-Lucy\build\classes
    [javac] C:\Users\Hp\git\fls-web-Lucy\src\adminOps\AdminOps.java:7: error: package javax.servlet does not exist
    [javac] import javax.servlet.ServletException;
    [javac]                     ^
    [javac] C:\Users\Hp\git\fls-web-Lucy\src\adminOps\AdminOps.java:8: error: package javax.servlet.annotation does not exist
    [javac] import javax.servlet.annotation.WebServlet;
    [javac]                                ^
                                                        ^
    [javac]   location: class AdminOps
    [javac] 2 errors

BUILD FAILED
C:\Users\Hp\git\fls-web-Lucy\build.xml:43: Compile failed; see the compiler error output for details.

由于Tomcat是XAMPP的一部分,我没有单独安装它。这会产生问题吗?

1 个答案:

答案 0 :(得分:1)

在您的Ant脚本中,代码段...

<javac ... classpathref="classpath" ... />

...配置<javac>以在classpath定义的路径中搜索库。在您的脚本中,classpath被定义为...

<path id="classpath">
    <fileset dir="${jar.dir}" includes="**/*.jar"/>
</path>

所以:

  • javax.servlet-api-3.1.0.jar需要存在于${jar.dir}下。
  • <path id="classpath">需要另一个<fileset>指向javax.servlet-api-3.1.0.jar。