在 ant 脚本中包含外部 JAR 时出错 [英] error while including external JARs in ant script
问题描述
这是我第一次尝试编写 ANT 脚本.这是我使用 Spring 的简单 Hello World 应用程序的 build.xml.
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?
我第一次运行 ANT 脚本时,生成了 JAR 文件.当我再次尝试运行它时,它给出了下面提到的错误.
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.
干净:[删除] 删除目录 F:\shil\JAVA\Spring Workspace\myfirstapp1\build
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?
推荐答案
<classpath>
目标中的 compile
应该嵌套在 <javac>
元素.
The <classpath>
inside your compile
target should be nested inside the <javac>
element.
还值得注意的是,ant(实际上,大多数构建系统)的很多功能都来自于声明目标之间的依赖关系.在您的示例中,在没有先运行 compile
目标的情况下运行 jar
目标没有任何意义,否则,没有要 jar 的类文件 - 或者更糟,您将手动运行 jar
,但忘记先运行 compile
并最终在您的 JAR 文件中得到过时的类.所以,jar
目标应该依赖于 compile
目标.这样,无论何时运行 jar
目标,任何更改的类都将首先自动编译.同样,run
目标应该依赖于 jar
目标.
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.
这篇关于在 ant 脚本中包含外部 JAR 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!