Eclipse - 外部 JAR 和 git
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17008230/
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 - External JARs and git
提问by M0rgenstern
I am currently using eclipse for working with Java. Additionaly I use git to synchronize my project between my laptop and my desktop PC.
The problem is now the following: I added external JARs to the project (Slick-Util, LWJGL).
But the path to each library is another on each device. So everytime I start working on the other device, I have to change the path to the jar files and the javadocs.
The libraries are all stored in my eclipse workspace. So the libraries and the projects are all in the same folder. And this folder is also commited with git.
Is there a way to change the eclipse settings (or do something else) so I do not have to change the path to the libraries and javadocs everytime?
我目前正在使用 eclipse 来处理 Java。另外,我使用 git 在我的笔记本电脑和台式电脑之间同步我的项目。
现在的问题如下:我向项目(Slick-Util、LWJGL)添加了外部 JAR。
但是每个库的路径在每个设备上都是另一个。因此,每次我开始在另一台设备上工作时,我都必须更改 jar 文件和 javadocs 的路径。
这些库都存储在我的 eclipse 工作区中。所以库和项目都在同一个文件夹中。而且这个文件夹也是用git提交的。
有没有办法更改 eclipse 设置(或做其他事情),这样我就不必每次都更改库和 javadoc 的路径?
I already googled and searched for it, but I could not find something about it.
我已经用谷歌搜索并搜索了它,但我找不到关于它的东西。
回答by jmruc
Just don't add the libraries' jars to git. There are multiple build tools for java, which manage dependencies for you - you just state the libraries you're going to use, and the build tool downloads it for you at build time.
只是不要将库的罐子添加到 git 中。有多种用于 Java 的构建工具,它们为您管理依赖项 - 您只需说明要使用的库,构建工具会在构建时为您下载它。
I would recommend Gradle, but Mavenis also a very popular choice.
我会推荐Gradle,但Maven也是一个非常受欢迎的选择。
In gradle, you would create a file build.gradle
, and define your dependencies in it:
在 gradle 中,您将创建一个文件build.gradle
,并在其中定义您的依赖项:
apply plugin: 'eclipse'
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.lwjgl.lwjgl:lwjgl:2.9.0'
compile 'org.lwjgl.lwjgl:lwjgl_util:2.9.0'
}
Then you would run gradle eclipse
from the command line - that would add the libraries you use to the classpath in eclipse. And when you want to compile and package your project you would run gradle build
from the command line. You should read about it if you're going to use it, what I describe may not be exactlywhat you need.
然后您将从命令行运行gradle eclipse
- 这会将您使用的库添加到 eclipse 中的类路径。当你想编译和打包你的项目时,你可以从命令行运行gradle build
。你应该了解它,如果你要使用它,我形容可能不准确,你所需要的。
Also, there are instructions for using LWJGL with maven.
此外,还有将LWJGL 与 maven 结合使用的说明。
回答by MoienGK
add jar files to a lib folder inside your project like this : D:\Workspace\myproject\lib\your-jar-file.jar
将 jar 文件添加到项目中的 lib 文件夹中,如下所示:D:\Workspace\myproject\lib\your-jar-file.jar
then go to your projects build path select libraries tab and click on add jarsand NOTadd external jarsthis way your jar files path will be relative to your project
然后转到您的项目构建路径选择库选项卡并单击添加罐子而不是添加外部罐子这样您的罐子文件路径将相对于您的项目
EDIT :I highly recommend to use a build tool as Kiril Raychev described. it will look a bit confusing to start with but after a while and after a normal growth in your application that will lead to using different frameworks, controlling and managing dependencies and their conflicts without a build tool will literally kill you.
编辑:我强烈建议使用 Kiril Raychev 描述的构建工具。一开始看起来有点混乱,但经过一段时间后,在您的应用程序正常增长后,将导致使用不同的框架,控制和管理依赖项及其冲突而无需构建工具,这实际上会杀死您。
回答by thestar
You can simply use -f flag on add command.
您可以简单地在添加命令上使用 -f 标志。
git add -f test.jar
And, then commit and push to your repo.
然后提交并推送到你的仓库。
回答by Akunosh
Up until now i usually use svn so i'm not entirely sure how it works out in git, but have you tried to store the JARs in the lib folder of the project they are used in? (Eclipse displays the lib folder so you can easiely add them to the buildpath with a right click on the library in the package explorer.) That way the relative location/path of the libraries to the project should stay the same. Furthermore if you plan to pack the project into a JAR later you ship the libraries inside that JAR without having to worry whether the user of that file even has them on his computer.
到目前为止,我通常使用 svn,所以我不完全确定它在 git 中是如何工作的,但是您是否尝试将 JAR 存储在使用它们的项目的 lib 文件夹中?(Eclipse 会显示 lib 文件夹,因此您可以通过在包资源管理器中右键单击库轻松地将它们添加到构建路径。)这样,库与项目的相对位置/路径应该保持不变。此外,如果您打算稍后将项目打包到 JAR 中,您可以在该 JAR 中传送库,而不必担心该文件的用户甚至在他的计算机上是否有这些库。
PS: Looks like i'm a minute too late. Dave basically said the same thing.
PS:看起来我迟到了一分钟。戴夫基本上说了同样的话。