Java 如何将导入的 jar 文件添加到 eclipse 中的 web-inf/lib 以进行构建?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22130667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add imported jar files to web-inf/lib in eclipse for my build?
提问by rjohnb4
I understand questions relating to this has been answered, but I have not come across this particular question answered in my search.
我知道与此相关的问题已得到解答,但在我的搜索中我还没有遇到这个特定问题的答案。
I've been trying to get my imported JAR files included in my build (shown below) of the spring application, but anything and everything I've done has not been the solution. I receive 'package does not exist' error when building in Eclipse using Tomcat and Ant because it cannot find the JAR files in the web-inf/lib folder (obviously). Eclipse does not allow me to drop and drag the files from the Referenced Library utility library to any desired folder. When I attempt to configure the build path, I add the external JARs and the directory path from which the file is located shows up next to the JAR in the libraries tab. The publish/export dependency is set to the WEB-INF/lib folder I've read adding all JAR files directly to the Tomcat/lib folder (which should not be done) could solve the issue but that's not the case for me. The deploy assembly path I set to /WEB-INF/lib for the JARs, no luck. I've tried restarting Eclipse, no luck. I created a new dynamic web project, no luck. I've unzipped the war file and no JAR files found, which is expected. I believe my environment variables are added correctly from
我一直在尝试将我导入的 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 文件,这是意料之中的。我相信我的环境变量是从
It's been years since I've played with Eclipse and Spring applications, so I expect I'm missing an important step.
我已经好几年没有使用 Eclipse 和 Spring 应用程序了,所以我希望我错过了重要的一步。
NOTES: This is a Spring tutorial I've been working with to get back on my feet with Spring. Eclipse version 4.3.0 Tomcat version 6 Ant version 1.8.4 - was included as a plugin for Eclipse download
注意:这是一个 Spring 教程,我一直在使用它来重新使用 Spring。Eclipse 版本 4.3.0 Tomcat 版本 6 Ant 版本 1.8.4 - 作为 Eclipse 下载的插件包含在内
Any help will be greatly appreciated, since I would enjoy playing with web applications once again.
任何帮助将不胜感激,因为我会喜欢再次玩网络应用程序。
<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>
采纳答案by JB Nizet
Drop the jars that need to be in the classpath of the deployed webapp (in WEB-INF/lib
) under the WebContent/WEB-INF/lib
product of the Eclipse project. That's all you need to do.
将需要在 Eclipse 项目产品WEB-INF/lib
下部署的 webapp 的类路径中的 jars(in )WebContent/WEB-INF/lib
。这就是你需要做的。
If you already added them to the project's build path, then remove them from there: everything under WebContent/WEB-INF/lib
is automatically added to the build path.
如果您已经将它们添加到项目的构建路径中,则从那里删除它们:下面的所有内容WebContent/WEB-INF/lib
都会自动添加到构建路径中。
回答by Roman C
Try to use all jars to the classpath
尝试将所有 jars 用于类路径
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>