The right way to deal with external libraries in java (using eclipse)
This is in a way a follow-up of a previously unanswered question of mine (link) which excalated over the past weeks, and now it has come to a point where I cant really develop anymore...
So here's the deal; I have more of a mathematics/engineering background than pure CS, so I dont have a lot of experience with proper/large-scale software development, but rather scripting and algorithms. Now that I am working on a large project, on my own, I am confused with some of the aspects of development. One of them being handling libraries/dependencies
I have initially created a folder called lib
under my project folder (in Eclipse workspace) and copied my external libraries in there and then added them to the build path. However later on I needed some other stuff like JCommons
, JFreeChart
, Apache Commons Math
etc.. According to instructions these libraries can/should be included as user libraries, allowing the developer to see the documentation and source code from within IDE (such as Eclipse). I have gotten it right so far, I hope...
So where is the problem? Well first off it appeared as the user libraries mentioned above are not included on the SVN copy of the project, meaning that my colleagues who wanted to test-drive my project couldn't do so, by just simply acquiring the project from SVN. Now the second phase of the problem unsurfaced when I changed my workstation at work, I wanted to import my project into Eclipse from my backup, but then everythin开发者_高级运维g except those user libraries are there. To make things more complicated, when this software is done, it will be implemented on a server so it would be absolutely best if everything could be packed into one single library or a even better a runnable jar
file.
I have been previously advised to take a look at Maven, or Ivy, but my initial understanding after checking them both is that they are used from the beginning and mostly for more complicated projects. Honestly I am completely puzzled as to how I am supposed to manage my dependencies. Any ideas?
(Sorry for keeping it long, but I figured better complete and long then inadequate information)
EDIT: I have managed to sort out the problem; the problem has apparently originated from the simple fact that eclipse doesn't copy all resources to one place, and the installation instructions for some libraries doesn't really warn you about how to manage the libraries in the best way.
Thanks to everyone, who took their time to try and help me. I will in time look more into projects like Maven and Ivy, it's definitely interesting stuff. However for now I just need to get back to the software into running state, been wrestling with new stuff for too long.. :)
At the risk of committing an act of heresy, I'd say Maven and wot-not are overkill here. You need your dependencies in your SVN project, simple as that. You've added your lib folder to your build path in Eclipse, and that's fine. But unless you specifically add the contents of the lib folder to your project (as Fred describes), those items won't be eligible for committing to SVN -- they will simply be referenced in your set-up's classpath. That's good for you, no good for anyone looking to check out your project (as you have found).
I appreciate that we could have endless discussions about best practice, the validity of committing third party libraries to version control, and so on and so forth... but I think most people have work to do ;-)
Invest some time to learn how to create a project using Maven. It just does so many things for you that it's amazing.
Okay I think this can be solved..
In Eclipse in the java build path screen there is also an import jar libraries choice that allows you to choose the project lib folder..in fact there is a choice labeled folder I think..
The other thing since it will be deployed to a server you will have to have every jar in the lib folder.
There is an ant technique where you check for a class and a property in the jar to verify its the correct jar to import from the lib folder before proceeding with that sequence of the build.. do a Google search and you will find the posts about it..
The three options are:
- Maven (refcard) - a very powerful tool, but at the same time very easy to use. I use it in all my projects, no matter how small they are. It is dependency management + build tool in one
- Ivy - much like maven, but it is only a dependency management tool. You'd have to do your builds with ant
- committing jars & eclipse project files - this is not portable between IDEs, but actually isn't that horrible and is used in many projects
Update: a few words explaining maven ideology:
- convention over configuration - you structure your project in a predefined way. That doesn't have to do with dependency management, so just mentioning it.
- repositories - that's where the jar files actually reside. They don't reside in your SVN, because they have a separate mechanism of versioning and because they take up unnecessary space.
- IDEs integrate with maven. For example m2eclipse gets the maven dependencies and appends them to your eclipse build path, thus making the usage transparent
- dependency resolution - in
pom.xml
you define multiple<dependency>
tags, with name and version, and maven fetches all the required jars from the remote repositories. It also fetches jars on which your dependencies depend (transitive dependencies). Thus you don't end up havingNoClassDefFoundException
.
To me this is extremely straightforward: you define what you need, and maven decides how to fetch it, and how to add it to your classpath.
You don't have to have a complicated project or a greenfield project to benefit from Maven so I would take the advice you've already been given to try and give it a go.
If you really don't want to, then you should really be checking your libraries into SVN along with your code. If a version of your code relies on a specific library at a certain point in time then there is a contract there and by not including the libraries in SVN you're breaking that contract- something you've already found as you can't recreate your application from scratch elsewhere.
As for packing up in a single JAR- this is possible with Ant, but why would you need to?
At our company we have a separate SVN Repository for third party libraries, and we have a company rule that on every development workstation this repository is checked out to C:\dev. So every one, has the libraries in the right place and the projects works fine.
精彩评论