在ant脚本中包含外部jar时出现错误



这是我第一次尝试编写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文件。当我试着再次运行它时,它给出了下面提到的错误。

清洁:[delete]删除目录F:shilJAVASpring Workspacemyfirstapp1build

compile:
     [echo] compiling
    [mkdir] Created dir: F:shilJAVASpring Workspacemyfirstapp1buildclasses
    [javac] Compiling 5 source files to F:shilJAVASpring Workspacemyfirstapp
1buildclasses
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletHello
WorldServlet.java:4: package javax.servlet does not exist
    [javac] import javax.servlet.ServletException;
    [javac]                     ^
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletHello
WorldServlet.java:5: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServlet;
    [javac]                          ^
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletHello
WorldServlet.java:6: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServletRequest;
    [javac]                          ^
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletHello
WorldServlet.java:7: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServletResponse;
    [javac]                          ^
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletHello
WorldServlet.java:12: cannot find symbol
    [javac] symbol: class HttpServlet
    [javac] public class HelloWorldServlet extends HttpServlet {
    [javac]                                        ^
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletHello
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletHello
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletHello
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletHello
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletHello
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletHello
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
meServlet.java:4: package javax.servlet does not exist
    [javac] import javax.servlet.RequestDispatcher;
    [javac]                     ^
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
meServlet.java:5: package javax.servlet does not exist
    [javac] import javax.servlet.ServletException;
    [javac]                     ^
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
meServlet.java:6: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServlet;
    [javac]                          ^
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
meServlet.java:7: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServletRequest;
    [javac]                          ^
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
meServlet.java:8: package javax.servlet.http does not exist
    [javac] import javax.servlet.http.HttpServletResponse;
    [javac]                          ^
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
meServlet.java:15: cannot find symbol
    [javac] symbol: class HttpServlet
    [javac] public class WelcomeServlet extends HttpServlet {
    [javac]                                     ^
    [javac] F:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloservletWelco
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:shilJAVASpring Workspacemyfirstapp1srcmyhelloHelloWo
rldApp.java uses or overrides a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] 25 errors
BUILD FAILED
F:shilJAVASpring Workspacemyfirstapp1antbuild.xml:19: Compile failed; see
the compiler error output for details.
Total time: 1 second

为什么第一次没有发生这种情况?有人能帮帮我吗?

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

同样值得注意的是,ant(实际上,大多数构建系统)的很多功能来自于声明目标之间的依赖关系。在您的示例中,在没有首先运行compile目标的情况下运行jar目标是没有任何意义的,否则,没有类文件可以jar—或者更糟的是,您将手动运行jar,但忘记先运行compile,最终在jar文件中出现过时的类。因此,jar目标应该依赖于compile目标。这样,无论何时运行jar目标,任何更改的类都将首先自动编译。类似地,run目标应该依赖于jar目标。

像这样使用classpathref属性

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

相关内容

  • 没有找到相关文章

最新更新