【问题标题】:How can I add imported jar files to web-inf/lib in eclipse for my build?如何在 Eclipse 中将导入的 jar 文件添加到 web-inf/lib 以进行构建?
【发布时间】:2014-03-02 17:12:27
【问题描述】:

我知道与此相关的问题已得到解答,但我在搜索中没有遇到此特定问题的答案。

我一直在尝试将我导入的 JAR 文件包含在我的 spring 应用程序的构建(如下所示)中,但是我所做的任何事情都不是解决方案。使用 Tomcat 和 Ant 在 Eclipse 中构建时收到“包不存在”错误,因为它在 web-inf/lib 文件夹中找不到 JAR 文件(显然)。 Eclipse 不允许我将文件从 Referenced Library 实用程序库拖放到任何所需的文件夹中。当我尝试配置构建路径时,我添加了外部 JAR,并且文件所在的目录路径显示在库选项卡中的 JAR 旁边。发布/导出依赖项设置为我读过的 WEB-INF/lib 文件夹,将所有 JAR 文件直接添加到 Tomcat/lib 文件夹(不应该这样做)可以解决问题,但对我来说并非如此。我为 JAR 设置为 /WEB-INF/lib 的部署程序集路径,没有运气。我试过重启 Eclipse,没有运气。我创建了一个新的动态 web 项目,没有运气。我已经解压缩了war 文件,但没有找到预期的JAR 文件。我相信我的环境变量是从

正确添加的

我已经多年没有使用 Eclipse 和 Spring 应用程序了,所以我想我错过了一个重要的步骤。

注意事项: 这是我一直在使用的 Spring 教程,旨在让 Spring 重新站起来。 Eclipse 版本 4.3.0 Tomcat 版本 6 Ant 1.8.4 版 - 作为 Eclipse 下载插件包含在其中

任何帮助都将不胜感激,因为我会再次喜欢玩网络应用程序。

<project name="springapp1" basedir="." default="usage">
<property file="build.properties"/>

<property name="lib.dir" value="C:/Eclipse/Tomcat/lib"/>
<property name="webapps.dir" value="C:/Eclipse/Tomcat/webapps"/>

<property name="src.dir" value="src"/>
<property name="web.dir" value="WebContent"/>
<property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
<property name="name" value="springapp1"/>

<path id="master-classpath">
    <fileset dir="${web.dir}/WEB-INF/lib">
        <include name="*.jar"/>
    </fileset>
    <!-- We need the servlet API classes: -->
    <!--  * for Tomcat 5/6 use servlet-api.jar -->
    <!--  * for other app servers - check the docs -->

    <!--<fileset dir="${appserver.lib}">-->
    <fileset dir="${lib.dir}">
        <include name="servlet*.jar"/>
    </fileset>
    <pathelement path="${build.dir}"/>
</path>

<target name="usage">
    <echo message=""/>
    <echo message="${name} build file"/>
    <echo message="-----------------------------------"/>
    <echo message=""/>
    <echo message="Available targets are:"/>
    <echo message=""/>
    <echo message="build     --> Build the application"/>
    <echo message="deploy    --> Deploy application as directory"/>
    <echo message="deploywar --> Deploy application as a WAR file"/>
    <echo message="install   --> Install application in Tomcat"/>
    <echo message="reload    --> Reload application in Tomcat"/>
    <echo message="start     --> Start Tomcat application"/>
    <echo message="stop      --> Stop Tomcat application"/>
    <echo message="list      --> List Tomcat applications"/>
    <echo message=""/>
</target>

<target name="build" description="Compile main source tree java files">
    <mkdir dir="${build.dir}"/>
    <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true"
           deprecation="false" optimize="false" failonerror="true">
        <src path="${src.dir}"/>
        <classpath refid="master-classpath"/>
    </javac>
</target>

<target name="deploy" depends="build" description="Deploy application">
    <copy todir="${webapps.dir}/${name}" preservelastmodified="true">
        <fileset dir="${web.dir}">
            <include name="**/*.*"/>
        </fileset>
    </copy>
    <echo message="Deploy Completed."/>
</target>

<target name="deploywar" depends="build" description="Deploy application as a WAR file">
    <war destfile="${name}.war"
         webxml="${web.dir}/WEB-INF/web.xml">
        <fileset dir="${web.dir}">
            <include name="**/*.*"/>
        </fileset>
    </war>
    <copy todir="${webapps.dir}" preservelastmodified="true">
        <fileset dir=".">
            <include name="*.war"/>
        </fileset>
    </copy>
</target>

<path id="catalina-ant-classpath">
    <!-- We need the Catalina jars for Tomcat -->
    <!--  * for other app servers - check the docs -->
    <fileset dir="C:/Eclipse/Tomcat/lib">
        <include name="catalina-ant.jar"/>
    </fileset>
</path>

<taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
    <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
    <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
    <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask">
    <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
    <classpath refid="catalina-ant-classpath"/>
</taskdef>

<target name="install" description="Install application in Tomcat">
    <install url="${tomcat.url}"
             username="${tomcat.username}"
             password="${tomcat.password}"
             path="/${name}"
             war="${name}"/>
</target>

<target name="reload" description="Reload application in Tomcat">
    <reload url="${tomcat.url}"
             username="${tomcat.username}"
             password="${tomcat.password}"
             path="/${name}"/>
</target>

<target name="start" description="Start Tomcat application">
    <start url="${tomcat.url}"
             username="${tomcat.username}"
             password="${tomcat.password}"
             path="/${name}"/>
</target>

<target name="stop" description="Stop Tomcat application">
    <stop url="${tomcat.url}"
             username="${tomcat.username}"
             password="${tomcat.password}"
             path="/${name}"/>
</target>

<target name="list" description="List Tomcat applications">
    <list url="${tomcat.url}"
             username="${tomcat.username}"
             password="${tomcat.password}"/>
</target>

【问题讨论】:

  • 在 ant 构建时,您始终可以使用 &lt;copy&gt; 将所需的任何 jar 复制到 WEB-INF/lib

标签: java eclipse spring tomcat ant


【解决方案1】:

将需要位于 Eclipse 项目的 WebContent/WEB-INF/lib 产品下的已部署 webapp 的类路径(在 WEB-INF/lib)中的 jar 删除。这就是你需要做的。

如果您已经将它们添加到项目的构建路径中,请从那里删除它们:WebContent/WEB-INF/lib 下的所有内容都会自动添加到构建路径中。

【讨论】:

  • 我看到了我的问题。我尝试将它们作为外部 JAR 导入并将它们复制到构建路径,当我应该从它所在的目录中复制 JAR 并将其放入 WEB-INF/lib 中时。简单的解决方案,我花了几个小时才解决。感谢您的输入。我很感激
  • @rjohnb4 那么你应该标记为答案,请参阅答案左侧的右勾号。
  • 所以你的意思是如果我把一些罐子放在 /lib/ 项目中会自动编译?
【解决方案2】:

尝试使用类路径的所有 jars

<fileset dir="${lib.dir}">
    <include name="*.jar"/>
</fileset>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多