TinusSky
TinusSky

Reputation: 1737

Multi module maven project in a new local git repository?

Yesterday and today i converted my eclipse juno project to multi module maven project in eclipse. I have 1 maven project of type 'pom-root' and all my other projects are modules. The modules have the root project as parent. This works correctly.

A few hours ago i did try to add the root project to egit, by simply right clicking the root project and picking share project -> egit.

I first created a new repository in a new directory, the result was that all my module project became empty and all source files were copied to the repository directory. Gladly i did see that and could recover easily by copying the files back.

Next after the failed attempt i tried to create the repository in the parent folder of project. This failed somehow and the root project became corrupt. After a lot of fiddling around i deleted the corrupt root project and created a new one of type 'pom-root', and somehow i managed to get everything running again.

Next i surfed to stackoverflow ( :-) ) and ask my question:

How can i put my multi module maven project in a new local git repository? (without losing or corrupting everything)

Upvotes: 2

Views: 2070

Answers (1)

tm2
tm2

Reputation: 46

Summary: Delete your Eclipse projects (just their metadata, NOT their contents), then re-create them by importing them from the local repo.

Caveat: I'm at the "a little knowledge is a dangerous thing" stage, so treat this with suspicion.

I just went through this, and I think I have a solution/workaround.

The problem, I believe, is that most of Eclipse still doesn't grok projects nested inside other projects. When you shared your root project, EGit moved all of your project's files out of the Eclipse workspace into the local git repo and then updated Eclipse's metadata for the root project to reflect the new location of the files (good), but (since it is clueless about nested projects) that means it treated all the files in your child modules as though they were part of your root project and hence moved them into the git repo (that's OK) but didn't update the Eclipse project metadata for those child modules (that's bad). So Eclipse shows that you now have a bunch of (child) projects that don't have any files (not even a .project file, nor pom.xml). But if you ignore Eclipse and look directly in the working directory of the git repo, they're all sitting there happily (i.e. EGit copied the directory structure and all the files, including the child modules) and from git's perspective they are all currently untracked.

The solution (I believe) is: delete your Eclipse projects. No, really. But very carefully; see below. [Just to be safe, maybe use e.g. zip to take a backup copy of your lovely files that are sitting in the working directory of the repo. If they were already staged or committed this would be moot, but presumably at this point they aren't].

When you select each Eclipse project and right-click Delete, make sure that the "Delete project contents on disk (cannot be undone)" checkbox is NOT checked -- in other words, you are deleting the Eclipse metadata for each project (I did this for all the child modules and even for the root project) but you aren't deleting the files that are sitting in the working directory of the repo.

(I was worried that m2e might be too "helpful" and, as I deleted each child module, it might remove it from the parent's pom.xml, but thankfully that didn't seem to happen).

In the Eclipse Preferences for Team - Git - Projects, I do have "Auto share projects located in a git repository" enabled. That makes life easier but I assume that you could use less automagic approaches if you preferred.

Once all the relevant Eclipse projects have been deleted, use the Git Repositories view to Import Maven Projects; this creates new Eclipse projects (for the root project and for the child modules) with valid metadata that points to the files in the repo. Hallelujah.

I believe this works because one part of Eclipse that isn't clueless about nested projects is the import-maven-projects logic in m2e.

At this point all the files are still untracked, so next you probably want to select the root project and right-click Team --> Add to Index (which will also add all the child modules, I assume because it's oblivious to the nested projects and just treats them as extra files in the root project).

I wondered what would happen if I then added a new child module -- would it "just work" (and be shared in the git repo) or would it require more gyrations? I was pleased to find that (as far as I can see) it "just works": the Eclipse project metadata shows the files in the right place (in the git repo, as a child under the root project) and marks the project as shared in git.

So my guess is that if you or I had a time machine, the easy way to do this would have been to create the root Maven project, share that to EGit, and only then start creating the child Maven modules.

My environment (though it probably doesn't matter): Eclipse 4.3.1 (Kepler SR1) with the usual versions of its components: m2e 1.4.0, EGit 3.0.3, m2e EGit connector 0.14.0.

Upvotes: 3

Related Questions