简体   繁体   English

Eclipse - 外部JAR和git

[英]Eclipse - External JARs and git

I am currently using eclipse for working with Java. 我目前正在使用eclipse来处理Java。 Additionaly I use git to synchronize my project between my laptop and my desktop PC. 另外我使用git在我的笔记本电脑和台式电脑之间同步我的项目。
The problem is now the following: I added external JARs to the project (Slick-Util, LWJGL). 问题现在如下:我在项目中添加了外部JAR(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. 因此,每当我开始处理其他设备时,我都必须更改jar文件和javadocs的路径。
The libraries are all stored in my eclipse workspace. 这些库都存储在我的eclipse工作区中。 So the libraries and the projects are all in the same folder. 所以库和项目都在同一个文件夹中。 And this folder is also commited with git. 这个文件夹也是用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设置(或做其他事情)所以我不必每次都改变库和javadocs的路径?

I already googled and searched for it, but I could not find something about it. 我已经用Google搜索并搜索了它,但我找不到它的相关信息。

Just don't add the libraries' jars to git. 只是不要将库的jar添加到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. 有多个用于java的构建工具,它们可以为您管理依赖项 - 您只需说明您将要使用的库,构建工具将在构建时为您下载它。

I would recommend Gradle , but Maven is 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. 然后你将从命令行运行gradle eclipse - 这会将你使用的库添加到eclipse中的类路径中。 And when you want to compile and package your project you would run gradle build from the command line. 当您想要编译和打包项目时,您将从命令行运行gradle build You should read about it if you're going to use it, what I describe may not be exactly what you need. 如果您打算使用它,您应该阅读它,我所描述的可能并不完全是您所需要的。

Also, there are instructions for using LWJGL with maven . 此外,还有使用LWJGL和maven的说明

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 jars and NOT add external jars this way your jar files path will be relative to your project 然后转到你的项目构建路径选择库选项卡并单击添加jar并且 添加外部jar这样你的jar文件路径将相对于你的项目

EDIT : I highly recommend to use a build tool as Kiril Raychev described. 编辑:我强烈建议使用构建工具,如Kiril Raychev所述。 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. 开始时看起来有点令人困惑,但经过一段时间后,在应用程序正常增长之后,将导致使用不同的框架,控制和管理依赖关系以及他们的冲突而不使用构建工具将会真正杀死你。

You can simply use -f flag on add command. 您可以在add命令上使用-f标志。

git add -f test.jar

And, then commit and push to your repo. 然后,提交并推送到您的回购。

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? 到目前为止,我通常使用svn所以我不完全确定它是如何在git中运行的,但是你是否尝试将JAR存储在它们所使用的项目的lib文件夹中? (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. (Eclipse显示lib文件夹,因此您可以通过右键单击包资源管理器中的库,轻松地将它们添加到构建路径中。)这样,库与项目的相对位置/路径应保持不变。 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. 此外,如果您计划将项目打包到JAR中,那么您可以将该库发送到该JAR中,而不必担心该文件的用户是否在他的计算机上拥有它们。

PS: Looks like i'm a minute too late. PS:看起来我已经太晚了。 Dave basically said the same thing. 戴夫基本上说了同样的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM