简体   繁体   中英

Eclipse - External JARs and git

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?

I already googled and searched for it, but I could not find something about it.

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.

I would recommend Gradle , but Maven is also a very popular choice.

In gradle, you would create a file build.gradle , and define your dependencies in it:

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 exactly what you need.

Also, there are instructions for using LWJGL with maven .

add jar files to a lib folder inside your project like this : 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

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.

You can simply use -f flag on add command.

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? (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.

PS: Looks like i'm a minute too late. Dave basically said the same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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