corazza
corazza

Reputation: 32344

How am I supposed to work with multi-module Maven projects with EGit?

I tried my best to get a multi-module Maven project synced with GitHub using EGit, but I've failed.

The structure of the project is like this:

I tried this simple approach:

  1. Completely blank copy of Eclipse JDT and an empty workspace
  2. Create a new GitHub repository and initialize it with a README.md file
  3. Clone that repository to Eclipse
  4. Create the parent (pom) project
  5. Create two children (jar) projects, add simple main classes, everything works at this point

Now comes the tricky part. Do I share just my parent project? Or children as well? Well, it seemed logical to me to share the parent only. However when I did, my build path went completely crazy and Eclipse could no longer find main classes. Then I restarted Eclipse. Issues persisted. Then I "disconnected" the project with the repository and logically EGit randomly deleted one of the children on the file system, and made the Eclipse project completely unusable (no classes would display, all structure was gone).

I previosly had an episode where EGit randomly deleted one of my repositories and that time it wasn't just a test, it was my actual latest changes to the codebase.

Is this plugging completely bugged out or am I missing something?

(I followed the basic tutorial on setting it all up, and have my SSH keys configured properly.)

What should I do to properly push a local Maven multi-module project to GitHub, and be able to clone it on a completely different PC out of nothing?

Upvotes: 1

Views: 3680

Answers (3)

corazza
corazza

Reputation: 32344

Actually it's 100% possible to do this!

Here's an even simpler approach than before:

  1. Clone your repository
  2. When creating a new Maven project, don't place it in the default Workspace location, place it in the *subdirectory** of the repository
  3. Done! You can create as many sub-modules as you'd like, Eclipse has automatically detected your Git repository.

* the subdirectory is automatically created for you when you choose the default location, and it is named after your module. It's not automatically created when choosing a different location (which is a bit inconsistent, but makes sense). If you just choose your repo as your location, Eclipse/Maven will then generate all the top-level project files and directories (pom.xml, .project, src/, .settings, ...) in your repo's root directory - which might be what you wanted, but most likely isn't.


For a more complicated explanation of why it wasn't working, read on...


It's just a bit complicated and very unintuitive. Here's a picture of how it looks:

multi-module Maven project with a complex hierarchy in Eclipse using Egit and GitHub

Steps I took (make sure to backup anything important):

  1. Create a new repository on GitHub (with README)
  2. Install Eclipse JDT (the "Java" download, with Maven and Egit preinstalled, but it would work in any Eclipse version I presume)
  3. Clone the repository in Eclipse. At this time, there are no projects to import or anything, there's just the repository. To clone, go to Window -> Open perspective -> Other -> Git Repository Exploring
  4. Create your projects in Eclipse using Maven. I first created one child and one parent. The parent has to have packaging type of pom. The child can have it as a jar and has to be a Maven module, not a project (but it's essentially the same thing). At this point I created a simple main class in this child module, and saved everything
  5. Share your parent project to the repository you cloned before (right click on parent -> Team -> Share project -> Git -> select your repository). Now, things will start falling apart, but this is normal. Wait for Eclipse to finish. What happened is that Eclipse moved the project from the place you saved it previously (for me it was my workspace) to the git repository - and since the parent has the children inside it, it also moved the children repositories with it. However, it didn't update its definitions accordingly, so the child project is now just shown as an empty directory. You can commit now
  6. Delete the missing child project (not the parent!)
  7. Just import the projects again

Upvotes: 3

bmargulies
bmargulies

Reputation: 99993

Note that all the trouble and distress discussed here is specific to using the 'sharing' features of Eclipse in which the code is directly checked in and out of the workspace from source control. You don't have to use those features -- all of this works perfectly, with no magic and no stress, if you do not.

If you do a plain old git (or svn (or whatever)) checkout outside your workspace, and then use Eclipse's feature of opening an existing project leaving its source outside the workspace, everything will be fine. You will still have VCS operations in the resource tree, you can still commit or merge from inside Eclipse if you like, and your multi-module-Maven project will simply work with no special incantations.

Upvotes: 1

Zoltán Ujhelyi
Zoltán Ujhelyi

Reputation: 13858

Sadly, Eclipse does not really support what you are trying to achieve because it assumes the project hierarchy is single-level; while in your case the parent project contains the child projects as well. For details see this long bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=35973 (at the end there are some nice summary comment about the status).

As workaround I can suggest two things:

  1. Do not import your parent project into Eclipse.
  2. Put your parent project as a simple project in the hierarchy, where child projects are referenced with hierarchic paths

The projects I am involved in mostly follow the second approach (see e.g. the EMF-IncQuery project, where the parent is stored in releng/org.eclipse.incquery.parent in http://git.eclipse.org/c/incquery/org.eclipse.incquery.git/tree/), but if your parent pom rarely changes, the first option could work well as well.

Upvotes: 1

Related Questions