在ant脚本中包含外部JAR时出错

时间:2011-09-17 08:55:28

标签: java ant

这是我编写ANT脚本的第一次尝试。这是使用Spring的简单Hello World应用程序的build.xml。

<project name="MyFirstApp" default="jar" basedir="..">  
    <property name="src" location="src"/>
    <property name="build" location="build"/>
    <property name="lib" location="/WebContent/WEB-INF/lib"/>   
    <path id="classpath-example">   
        <fileset dir="${build}" includes="*.jar"/>
    </path>
    <target name="clean">
        <delete dir="build"/>
    </target>
    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="${src}" destdir="build/classes" includeantruntime="false"/>
        <classpath refid="classpath-example"/>
    </target>
    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/MyFirstApp.jar" basedir="build/classes"/>          
    </target>
    <target name="run">
        <java jar="build/jar/MyFirstApp.jar" fork="true"/>
    </target>
</project>

当我尝试运行时,会显示以下错误消息。

问题:无法创建任务或输入类路径 原因:名称未定义。 行动:检查拼写。 操作:检查是否已声明任何自定义任务/类型。 操作:检查是否已发生任何/声明。

有人可以告诉我该怎么做吗?

第一次运行ANT脚本时,生成了JAR文件。当我再次尝试运行它时,它会给出下面提到的错误。

清洁:    [删除]删除目录F:\ shil \ JAVA \ Spring Workspace \ myfirstapp1 \ build

compile:
     [echo] compiling
    [mkdir] Created dir: F:\shil\JAVA\Spring Workspace\myfirstapp1\build\classes

    [javac] Compiling 5 source files to F:\shil\JAVA\Spring Workspace\myfirstapp
1\build\classes
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:4: package javax.servlet does not exist
    [javac] import javax.servlet.ServletException;
    [javac]                     ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:5: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServlet;
    [javac]                          ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:6: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServletRequest;
    [javac]                          ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:7: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServletResponse;
    [javac]                          ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:12: cannot find symbol
    [javac] symbol: class HttpServlet
    [javac] public class HelloWorldServlet extends HttpServlet {
    [javac]                                        ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:26: cannot find symbol
    [javac] symbol  : class HttpServletRequest
    [javac] location: class my.hello.servlet.HelloWorldServlet
    [javac]     protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
    [javac]                          ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:26: cannot find symbol
    [javac] symbol  : class HttpServletResponse
    [javac] location: class my.hello.servlet.HelloWorldServlet
    [javac]     protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
    [javac]                                                      ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:26: cannot find symbol
    [javac] symbol  : class ServletException
    [javac] location: class my.hello.servlet.HelloWorldServlet
    [javac]     protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
    [javac]
                      ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:33: cannot find symbol
    [javac] symbol  : class HttpServletRequest
    [javac] location: class my.hello.servlet.HelloWorldServlet
    [javac]     protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
    [javac]                           ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:33: cannot find symbol
    [javac] symbol  : class HttpServletResponse
    [javac] location: class my.hello.servlet.HelloWorldServlet
    [javac]     protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
    [javac]                                                       ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:33: cannot find symbol
    [javac] symbol  : class ServletException
    [javac] location: class my.hello.servlet.HelloWorldServlet
    [javac]     protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
    [javac]
                       ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:4: package javax.servlet does not exist
    [javac] import javax.servlet.RequestDispatcher;
    [javac]                     ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:5: package javax.servlet does not exist
    [javac] import javax.servlet.ServletException;
    [javac]                     ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:6: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServlet;
    [javac]                          ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:7: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServletRequest;
    [javac]                          ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:8: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServletResponse;
    [javac]                          ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:15: cannot find symbol
    [javac] symbol: class HttpServlet
    [javac] public class WelcomeServlet extends HttpServlet {
    [javac]                                     ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:29: cannot find symbol
    [javac] symbol  : class HttpServletRequest
    [javac] location: class my.hello.servlet.WelcomeServlet
    [javac]     protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
    [javac]                          ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:29: cannot find symbol
    [javac] symbol  : class HttpServletResponse
    [javac] location: class my.hello.servlet.WelcomeServlet
    [javac]     protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
    [javac]                                                      ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:29: cannot find symbol
    [javac] symbol  : class ServletException
    [javac] location: class my.hello.servlet.WelcomeServlet
    [javac]     protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
    [javac]
                      ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:36: cannot find symbol
    [javac] symbol  : class HttpServletRequest
    [javac] location: class my.hello.servlet.WelcomeServlet
    [javac]     protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
    [javac]                           ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:36: cannot find symbol
    [javac] symbol  : class HttpServletResponse
    [javac] location: class my.hello.servlet.WelcomeServlet
    [javac]     protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
    [javac]                                                       ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:36: cannot find symbol
    [javac] symbol  : class ServletException
    [javac] location: class my.hello.servlet.WelcomeServlet
    [javac]     protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
    [javac]
                       ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:41: cannot find symbol
    [javac] symbol  : class RequestDispatcher
    [javac] location: class my.hello.servlet.WelcomeServlet
    [javac]             RequestDispatcher welcomeDispatcher = getServletContext(
).getRequestDispatcher("/Welcome.jsp");
    [javac]             ^
    [javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:41: cannot find symbol
    [javac] symbol  : method getServletContext()
    [javac] location: class my.hello.servlet.WelcomeServlet
    [javac]             RequestDispatcher welcomeDispatcher = getServletContext(
).getRequestDispatcher("/Welcome.jsp");
    [javac]                                                   ^
    [javac] Note: F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\HelloWo
rldApp.java uses or overrides a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] 25 errors

BUILD FAILED
F:\shil\JAVA\Spring Workspace\myfirstapp1\ant\build.xml:19: Compile failed; see
the compiler error output for details.

Total time: 1 second

为什么这不是第一次发生?有人可以帮忙吗?

2 个答案:

答案 0 :(得分:5)

<classpath>目标中的compile应嵌套在<javac>元素中。

值得注意的是,ant(以及大多数构建系统)的很多功能来自于声明目标之间的依赖关系。在您的示例中,在没有先运行jar目标的情况下运行compile目标没有任何意义,否则,没有类文件可以运行 - 或者更糟糕的是,您将运行{ {1}}手动,但忘记先运行jar,最后在JAR文件中使用过时的类。因此,compile目标应取决于jar目标。这样,无论何时运行compile目标,任何更改的类都将首先自动编译。同样,jar目标应取决于run目标。

答案 1 :(得分:0)

使用classpathref属性,就像这样

<javac path="src" destdir="build/classes" classpathref="classpath-example"/>