What is the best practice to work with several Java modules from different GitHub sources using IntelliJ?

This might be an odd qeustion but I do not know what the standard workflow is to achieve what I have in mind.

The current situation

I develop a Java project with IntelliJ and I manage my code with GitHub. The project is currently stored in a private repository on GitHuband it consists of several independent Maven modules. This is a sample structure:

The project structure

The plan

Now I plan to publish only ModuleB on a public repository on GitHub. I want to share only this module and allow others to use it. What is the best practice so that I can develop Module B further within my project and all changes get published in the public repository? I do not know how to do this in IntelliJ. I am insecure about the workflow here or is there another pattern which I could follow to achieve this?

Idea

I think I could export ModuleB, import it as a separate public repository into GitHub and then send pull requests when I make changes. But how do I do this with IntelliJ? How can I fork the public repository into my existing project, which is itself shared in a privare repository? Can I nest GitHub projects? Is this a good idea after all? Please let me know if my question is clear.

Further clarification

I know that I can import several "modules" in IntelliJ, but this is a different thing, isn't it? An IntelliJ module is the same as an Eclipse project, which is an independent entity. I think I could solve the problem with this approach, but then I would have to commit the overall project seperately. Could this be solved more elegantly with Git submodules perhaps?

Proposed Solutions

My created a new GitHub repository for ModuleB and uploaded the source code to it. This module is now set as public and independent from the main project.

Use IntelliJ import Module

Instead of trying to integrate the "external" repository directly into my current project, I cloned the ModuleB repository to a different directory. Then I imported the said folder as a new module in IntelliJ. enter image description here

IntelliJ then asks if there sould be a dependency between the newly added ModuleB. Unfortunately this only adds an entry into the .iml file

<orderEntry type="module" module-name="Submodule" />

and not into the pom.xml file. So this information where to obtain the source for the module is only available in IntelliJ. How could this problem be addressed? I only tried it with a toy project so far, but if someone wants to import the whole project into Eclipse for instance, this might cause a problem. How could I reference the other GitHub repository in a useful way?

有帮助吗?

解决方案

Solution: Git Subtrees

After fidelling around with submodules, I found that git subtrees allow me to achieve what I wanted. I mainly used this page to learn about subtrees and followed the following procedure in order to integrate a submodule (submodule-repo.git) into the parent repository (parent.git)

# Start at the root of the parent repository. Add a new remote repository to the  parent. 
# Add a new remote repository location called Submodule-Subtree
git remote add Submodule-Subtree https://github.com/yourname/submodule-repo.git

# Check that the new remote location is there
git remote -v

# Now add the newly added remote location of the submodule to the parent
# by specifying a folder name where you want the submodule to be stored locally.
git subtree add --prefix=NewSubModule/ Submodule-Subtree master

# Save to the parent master
git push origin master

# make changes in the new module
cd NewSubModule/
touch ThisIsANewFile.txt
git add .

# Commit to the parent
commit -m "we added a new file in the submodule from the parent repository"
git push origin master


# So far the changes the changes are only visible in the parent repository, 
# but not in the original location. To share your changes with the original 
# submodule-repo.git, you need this command:
git subtree push --prefix=NewSubModule/ Submodule-Subtree master

# You also need to update the submodule-repository manually
# git subtree pull —prefix=NewSubModule/ Submodule-Subtree master

As a next step you can import this folder as a module via the File | Import Module | Existing Sources.

The honest background is that comming from a SVN and Eclipse world, I need to learn the terminology more precisely and get used to the new concepts. I found the Pro Git Book very useful.

许可以下: CC-BY-SA归因
scroll top