Eclipse buildpath 自动获取内部目录的所有 JAR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/483563/
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
Eclipse buildpath automatically taking all JARs of a internal directory
提问by
How do I configure my project buildpath to have a set of .jar files located in the same directory automatically included in the buildpath ? Meaning that adding a new .jar file to this directory (and refreshing the project) updates the buildpath ? Rem : I am not working in a Webapp but in a standalone Java app. I know that it is possible in a Dynamic Web Project to have all the .jars located in WEB-INF/lib to be included in the build path. Is it possible to do kind of the same include but in standalone app ?
如何将我的项目构建路径配置为在构建路径中自动包含一组位于同一目录中的 .jar 文件?这意味着将新的 .jar 文件添加到此目录(并刷新项目)会更新构建路径?Rem :我不是在 Web 应用程序中工作,而是在独立的 Java 应用程序中工作。我知道在动态 Web 项目中可以将位于 WEB-INF/lib 中的所有 .jar 包含在构建路径中。是否可以在独立应用程序中执行相同的包含?
I am using Eclipse 3.4
我正在使用 Eclipse 3.4
回答by Yoni Roit
AFAIK, there's no normal way to do this.
AFAIK,没有正常的方法可以做到这一点。
If you really want, there's a little hack. Eclipse .classpath file is a very simple XML. You can write a script or ant task that goes over a directory, updates .classpath xml and refreshes the project.
如果你真的想要,有一个小技巧。Eclipse .classpath 文件是一个非常简单的 XML。您可以编写脚本或 ant 任务来遍历目录、更新 .classpath xml 并刷新项目。
回答by Yoni Roit
Using a homemade "ClassPath Container" solves the problem but needs you to build an Eclipse-plugin : http://www.ibm.com/developerworks/edu/os-dw-os-eclipse-classpath.html
使用自制的“ClassPath Container”解决了这个问题,但需要你构建一个 Eclipse 插件:http: //www.ibm.com/developerworks/edu/os-dw-os-eclipse-classpath.html
回答by Mauli
My colleague implemented a classpath container which recursivly looks for jars in a given directory within the workspace, have a look at http://openscada.org/2010/05/31/adding-a-directory-as-class-path-to-eclipse/
我的同事实现了一个类路径容器,它递归地在工作区中的给定目录中查找 jar,请查看http://openscada.org/2010/05/31/adding-a-directory-as-class-path-to -蚀/
The update site can be found at http://repo.openscada.org/p2/bob/R
更新站点可以在http://repo.openscada.org/p2/bob/R找到
The plugin is licensed unter LGPL V3 and you can find the source code under https://github.com/ctron/org.openscada.bob
该插件已获得 LGPL V3 许可,您可以在https://github.com/ctron/org.openscada.bob下找到源代码
回答by Ben Hardy
Use Maven2, and use the Eclipse Maven2 plugin.
使用 Maven2,并使用 Eclipse Maven2 插件。
回答by Kevin
Meaning that adding a new .jar file to this directory (and refreshing the project) updates the buildpath
这意味着将新的 .jar 文件添加到此目录(并刷新项目)会更新构建路径
Sorry, Eclipse doesn't support this.
抱歉,Eclipse 不支持此功能。
回答by Kevin
On a windows machine you could do what uzhin said with a one liner on the command line.
在 Windows 机器上,您可以在命令行上使用 one liner 执行 uzhin 所说的操作。
Supposing you had set up a variable in Eclipse called AXIS2_HOME that points to c:\javalibs\axis2-1.4.1, this example uses the "for" command to iterate all of the .jar files in the c:\javalibs\axis\lib directory and writes the .classpath nodes a file called addto.classpath. (Note: The carat character ^ escapes the < and > on the command line so they don't do what they usually do.)
假设您在 Eclipse 中设置了一个名为 AXIS2_HOME 的变量,该变量指向 c:\javalibs\axis2-1.4.1,本示例使用“for”命令迭代 c:\javalibs\axis\ 中的所有 .jar 文件lib 目录并将 .classpath 节点写入一个名为 addto.classpath 的文件。(注意:克拉字符 ^ 转义了命令行上的 < 和 >,因此它们不会执行通常的操作。)
for %i in (c:\javalibs\axis2-1.4.1\lib\*.jar) do @echo ^<classpathentry kind="var" path="AXIS2_HOME/lib/%~ni%~xi"/^> >> addto.classpath
You end up with something like this in the file...
你最终在文件中得到了这样的东西......
<classpathentry kind="var" path="AXIS2_HOME/lib/activation-1.1.jar"/>
<classpathentry kind="var" path="AXIS2_HOME/lib/annogen-0.1.0.jar"/>
<classpathentry kind="var" path="AXIS2_HOME/lib/axiom-api-1.2.7.jar"/>
<classpathentry kind="var" path="AXIS2_HOME/lib/axiom-dom-1.2.7.jar"/>
...
So then you can copy those lines and paste them in the obvious place in the actual .classpath file.
因此,您可以复制这些行并将它们粘贴到实际 .classpath 文件中的明显位置。
回答by calavera.info
I'm using workaround which I developed after reading similar question. I'm posting it here, because that question has a little bit different scope and it might not be obvious how to use suggestions written there.
我正在使用我在阅读类似问题后开发的解决方法。我在这里发布它,因为这个问题的范围有点不同,而且如何使用那里写的建议可能并不明显。
- Create new dynamic web project (default settings is ok so it is fast)
- Delete directory "lib" under WebContent/WEB-INF/
- Recreate that directory, but create it as link to the directory in your main project where your jars are located
- Check Web App Libraries option on Order and Export tab in Java Build Path settings of your project
- Add this newly created project as a dependency to your main project
- 创建新的动态 web 项目(默认设置可以,所以速度很快)
- 删除WebContent/WEB-INF/下的lib目录
- 重新创建该目录,但将其创建为指向主项目中 jar 所在目录的链接
- 在项目的 Java 构建路径设置中的 Order 和 Export 选项卡上检查 Web App Libraries 选项
- 将此新创建的项目作为依赖项添加到您的主项目
It's irritating to do this for every lib directory in your project but it's better than adding jars manually every time you do svn update (from your luckier colleagues using Idea). It's a shame that Eclipse don't have this feature.
为项目中的每个 lib 目录执行此操作很烦人,但这比每次执行 svn update 时手动添加 jars 更好(来自使用 Idea 的幸运同事)。很遗憾 Eclipse 没有这个功能。
回答by Murali
You can use a User Library in Eclipse can be a useful way to organize a set of jar files. If you have a set of jar files that you use in several projects, you can create a User Library to reference the set of jar files. As a result, all you would need to include in your project's build path is the User Library rather than all the individual jar files.
您可以使用 Eclipse 中的用户库来组织一组 jar 文件。如果您有一组在多个项目中使用的 jar 文件,您可以创建一个用户库来引用这组 jar 文件。因此,您需要在项目的构建路径中包含的只是用户库,而不是所有单独的 jar 文件。
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/User Library name"/>
回答by Jon Stevens
I built a tool which generates the .classpath (and .project), but it requires you to specify the list of jars in your ant build. It could probably be extended to read a list of jar files.
我构建了一个生成 .classpath(和 .project)的工具,但它要求您在 ant 构建中指定 jars 列表。它可能可以扩展为读取 jar 文件列表。
回答by workmad3
Have you tried using a wildcard in the classpath? So you would use something like:
您是否尝试过在类路径中使用通配符?所以你会使用类似的东西:
javac MyApp.java -cp /some/path/to/libraries/*.jar
I've not used java for a while so not sure if the above is possible, but it's what I would expect to be the syntax.
我有一段时间没有使用 java,所以不确定上述是否可行,但这是我期望的语法。