Jeune
Jeune

Reputation: 3538

How to set build paths automatically in Eclipse

Whenever I update our project's code from the svn I have to set the build paths of the libraries I import again because the last person who made a commit has a different classpath from mine.

Is there a way in Eclipse to automatically put in the imported libraries after updating from the SVN? I have heard of build files (Ant,Maven) but I really am not sure if I am looking at the right thing.

Upvotes: 1

Views: 6027

Answers (5)

Rich Seller
Rich Seller

Reputation: 84028

It is fine to not add .classpath/.project files if your project is simple and has few external dependencies. In practice you often have a number of relationships to maintain, so the overhead of making your .classpath portable is much less than the cumulative overhead of redefining it each time you bring the project into a workspace. The more involved your configuration, the more subtle errors you'll be fighting against attempting to reconstitute the project configuration.

You should be able to set up your classpath so it is portable by using Variables and Libraries, this avoids the need to hard-code paths. Your team can agree on a standard set of variables (and/or Libraries) to be used. A variable would then need to be defined on each developer's machine pointing to the common resources you use in your path.

Even better than Variables are Libraries. You can define custom libraries (under Window->Preferences->Java->Build Path->User Libraries) to reference your common components, then reuse those libraries in each project. A configured library can be exported from the User Libraries page and used by your peers.

Plugins such as m2eclipse provide Libraries (also know as Classpath Containers) that can automatically generate content based on some configuration (the Maven POM in m2eclipse's case). This abstracts the paths to underlying resources and allows multiple jars to be added to the path dynamically.

If you're not using Maven, the typical .classpath issue is that you want to add all jars in a folder to the classpath. This answer shows how it is possible to define a custom plugin to contribute all jars in a folder via a Classpath Container, you can also use this approach to automatically attach sources to the discovered elements and avoid having to repeat this effort each time.

Here is a typical before on the .classpath for a project I worked on recently. The library makes the configuration much lsess verbose, more portable, allows for reuse across projects, and defines the source attachments.

Before:

<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/xml-apis.jar" sourcepath="C:/apache-ant-1.7.1/src/main"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant.jar" sourcepath="C:/apache-ant-1.7.1/src/main"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-antlr.jar" sourcepath="C:/apache-ant-1.7.1/src/main"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-apache-bcel.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-apache-bsf.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-apache-log4j.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-apache-oro.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-apache-regexp.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-apache-resolver.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-commons-logging.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-commons-net.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-jai.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-javamail.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-jdepend.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-jmf.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-jsch.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-junit.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-launcher.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-netrexx.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-nodeps.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-starteam.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-stylebook.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-swing.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-testutil.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-trax.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/ant-weblogic.jar"/>
<classpathentry kind="lib" path="C:/apache-ant-1.7.1/lib/xercesImpl.jar"/>

After:

<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<!--all ant jars and source attachments defined here -->
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/ant"/>

Upvotes: 5

Thomas Jung
Thomas Jung

Reputation: 33092

Ant + Ivy is another solution. As far as I know there is no ivy -> eclipse task. We used ant + ivy in one of our projects and created the .classpath and .project files from the ivy metadata. (This was ivy 1.x maybe ivy 2 is capable to do this already.) This is analogous to the maven approach.

Upvotes: 0

Thomas Jung
Thomas Jung

Reputation: 33092

The maven dependency management and maven eclipse plugin would be one solution.

Once you have a running maven setup you can create the .project and .classpath files with the eclipse plugin. These files won't be checked in (all information is in the maven pom file).

Upvotes: 3

Dean J
Dean J

Reputation: 40298

Either don't check in classpath files, or use the same directory structure on each developer's machine.

Upvotes: 0

CPerkins
CPerkins

Reputation: 9008

How about this: Don't commit local classpath files to the repository as ".classpath".

I don't mean to be a smartass, but it's ridiculous to allow people to check things in which break other developers' environments.

Upvotes: 9

Related Questions