The right way to deal with external libraries in java (using eclipse)

寵の児 提交于 2019-12-04 04:12:55

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 having NoClassDefFoundException.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!