How to automatically change Eclipse build path when I check out Git branches - eclipse

I'm using Eclipse Helios and EGit 0.11.3.
I have a project where different branches use different versions of some of the libraries on my Java build path. For example, branch_old uses foo_lib_v1.0.jar while branch_new uses foo_lib_v2.0.jar.
If I'm working on branch_new and I need to checkout branch_old to test something, it's a bit of a hassle: I must manually reconfigure the build path in Eclipse before the project can build successfully.
Is there a way to store Eclipse's Java build path configuration for my project in git, so that when I check out a branch the build path is automatically modified? If not, is there another way to achieve the same result?
Thanks.

I see several choices here:
store project files (.classpath and .project) in Git as part of your branch. This way you would have to store all your dependencies in Git too, which is a hassle if you have a lot of them.
use Maven (m2eclipse plugin) and store project definition (pom.xml) file as part of your version control. Maven will greatly simplify your project dependency configuration
More information on m2eclipse plugin can be found at http://www.sonatype.com/books/m2eclipse-book/reference/

Should work: Put the .buildpath under version control. So its checkouted every time you switch the branches.

AFAIK Eclipse stores his build classpath in a file like .classpath(?) You can just add the file to git and have an own config for each branch.

Define several user libraries in your workspace, such as FooLib1, FooLib2, FooLib2. Then in the project's build path at a given branch reference the appropriate version of the library. Make sure you include project metadata files such as .classpath in your git repository and you should be set.

Related

How can I create a project in the Git repository root using Eclipse EGit?

I would like to add the project files of a brand-new Eclipse project to the root-level of a git repo. I would like it to look like something like this:
./projectrepo/.git
./projectrepo/.project
./projectrepo/src
...
However, the Eclipse Egit wizard provides no obvious way to do that, and insists on creating an undesired subdirectory (screenshots below):
./projectrepo/project/.git
./projectrepo/project/.project
./projectrepo/project/src
...
How can I get Eclipse to add my files at the repository root?
I understand that the Egit manual advises against this, but I do not plan to ever have multiple projects in this repository, so the extra directory is unnecessary (and counts against filesystem path limits on Windows). The fact that it's advised against tells me it must be possible somehow (or the documentation's wrong).
System information:
Windows 7 64-bit
Eclipse Oxygen Java EE (4.7.0 Build id: 20170620-1800)
Eclipse EGit 4.8.0.201706111038-r
Screenshots of Eclipse's current undesired behavior
Configure Git Repository wizard. Most of the things I'd like to change, like Target Location, are uneditable. Path within repository can only be used to add more extra folders. Use or create repository in parent folder of project has a freetext repository path input, but only parent folders will validate and can be used.
Resulting undesired Git repository structure. I would like my project files to be in this directory, not an extra "TestProject" folder:
Try instead to:
import your project into your Eclipse workspace
then use Team/Share project: it should detect the Git repo in it.
That way, you should avoid the double-folder creation.

How can I setup Eclipse to avoid missing library projects when switching git branches?

I'm in an annoying situation with Eclipse and I want to know if there's a better way I can setup my project / git repository to avoid having to constantly manually fix my project to compile.
Background:
While developing our Android App we had a version 1.1.x live and in git.
We're now working on version 1.2.x in development, which is a fork of 1.1.x. At the same time, we have to switch back to the 1.1.x line to fix bugs in what's currently live.
The issue is that the 1.2.x line has a new Library Project in it, which is only present in the 1.2.x branch. I have to switch between both branches a lot to do development for both, but Eclipse isn't handling it very well.
When switching to the 1.1.x branch:
Problem: Eclipse complains e.g. "AndroidManifest.xml file missing!", because the folder for the library project no longer exists on the file system (git has deleted it - remember it's not in the 1.1.x branch).
Current Fix: Click on the folder in Package Explorer, delete it.
When switching to the 1.2.x branch:
Problem: I deleted the folder for the 1.1.x branch, so now that dependency is missing when Eclipse goes to build the project, and there's a bunch of build errors. To be clear, project.properties lists the dependency and it's in the Build Path - but (I don't know the terminology) the Eclipse whatever is missing from the workspace, and the dependency is listed as missing (shows as a red X under Project Properties > Android > Library).
Current Fix: Because of two different Eclipse bugs (Eclipse "Invalid Project Description" when creating new project from existing source, Some projects cannot be imported because they already exist in the workspace error in Eclipse), I have to Import > General > Existing ..., Browse... for the root directory, uncheck Copy, click refresh, and finally select the project and click Finish.
What can I do so that this process is automated when jumping across branches?
If everybody is working in Eclipse then I you can commit your eclipse resources with your project
.project
.classpath
.settings/*
You might be ignoring these files in your .gitgignore, so simply remove them. This will not help for past commits unless you are willing to do a rebase with edit or filter-branch to add these files retroactively.

Setting Project specific maven repository

I have multiple projects. I want to create different maven repository paths for each project. Means, each project should point to different folder as maven repository (by default C:/Users/USERNAME/.m2 is used as repo).
I know that this may increase the download and same files if used in multiple projects will be downloaded/kept multiple times. But i want to separate out the repository for different projects so that i can bundle the repository along with my project's source code to be shared to the other person. I simply dont want to share my whole m2 repository (of size 2 gb) for a very small project sized 50 mb.
Regards,
Vibhav
You can specify repository location via command line option -Dmaven.repo.local=
I'd have just put a wrapper script to start maven in the project location and specified repository in it (probably pointing to location inside the project for your use case).
Thanks everyone for your comments.
I finally did the following:
Setup each project as a separate workspace in eclipse
Created a separate apache maven folder for each project (actually duplicating the maven home even though i am using same version of maven)
From eclipse workspace, I pointed out the appropriate maven home
configured the repository path (localRepository) in MAVEN_HOME/conf/settings.xml file from default ~./m2/repository to something like MAVEN_HOME/repo
This way, I could get separate maven repositories for each project (or I should say workspace).
Thanks,
Vibhav Agrawal
See the following answer:
Prevent Jenkins from Installing Artifact to Local Maven Repository
In a nutshell use a settings file for each build. Enables you to control both the local repository used for that project, but also other settings like repositories and build profiles.
My solution using Eclipse and Maven is to create new workspace, maven repository and settings.xml replacing the default .m2 repository path.
your new settings.xml
Then, try in Eclipse: Preferences>User Settings and browse your settings.xml
replace the User Settings file

Dependency and workspace projects

I have a Maven project called Utils that i'm working on. I also have another project, called A, that uses classes of Utils. So, I've added a dependency to Utils in the POM of A. I precise that the two projects are not installed in the local repository (or another private repository). When I try to package the project A, I get an error because Maven tries to get the Utils project from the local repository and central repository, but it's not there. I don't want to install the project because it's not final, i don't want to give it a version because there is no release.
Thanks
It's safe to install the project into local repository. Just use mvn install in the Utils' project folder. Anytime you do install it will be replaced by newest in your local repo.
But don't deploy it, it means others could use it. In comparison, your local repo is used only by you.
It seems that the Maven plugin on Eclipse provides this possibility. And there is no need to install the artifacts corresponding to the active projects in the local repository. It seems that the plugin considers the workspace as a second local repository (you can see this using the Maven repositories view). First, the Workspace resolution feature must be enabled (Right click on the project then under Maven). When I want to run the project, I use Run configurations wizard, and Resolve workspace must be checked.
Is it a good solution? I don't know if it just seems to work or if it really works.
Thanks

handle OS specific .classpath file in a hg repo

I have a Java project in Eclipse and version-control is a mercurial repo.
From several environments (Linux/Windows/MacOSX) I'm working with this same repo.
Problem is that in the .classpath file I've one line which is specific for the MacOSX environment and differs for the other environments. Still I don't want to put .classpath the ignore flag in version control.
I'm thinking of e.g. a mercurial extension on my MacOSX environment to tell mercurial to ignore this one specific line in .classpath but still sync everything else.
For all other environments (Linux/Windows) it's important that no changes are needed here.
One workflow-changing approach would be to switch your project to use something like Maven to manage your classpath and dependencies. Then you can use m2eclipse to automatically import it into Eclipse on each machine and automatically generate the appropriate project settings and classpath.