error while including external JARs in ant script

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

This is my first attempt at writing an ANT script. This is my build.xml for a simple Hello World app using Spring.

<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> 

when i try to run this the following error message is displayed.

Problem: failed to create task or type classpath Cause: The name is undefined. Action: Check the spelling. Action: Check that any custom tasks/types have been declared. Action: Check that any / declarations have taken place.

Can someone please tell me what is to be done?

The first time i ran the ANT script, the JAR file was generated. When i tried running it again , it gives the below mentioned error.

clean: [delete] Deleting directory 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 

why did this not happen the first time? can someone please help?

回答1:

The <classpath> inside your compile target should be nested inside the <javac> element.

It's also worth noting that a lot of the power of ant (and, indeed, most build systems) comes from declaring dependencies between targets. In your example, it doesn't make any sense to run the jar target without first having run the compile target, otherwise, there are no class files to jar - or worse, you'll run jar by hand, but forget to run compile first and end up with out-of-date classes in your JAR file. So, the jar target should depend on the compile target. That way, whenever you run the jar target, any changed class will automatically be compiled first. Similarly, the run target should depend on the jar target.



回答2:

Use classpathref attribute instead like this

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!