Search code examples
javaeclipsegitmavenegit

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:

  • parent (pom)
  • child-one (jar)
  • child-two (jar)

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?


Solution

  • 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