2

I want to create a new Maven project in Eclipse and push it to a new repository on github. I am a bit stuck on the right order of steps to achieve this.

  • To use a Maven archetype, I cannot use a directory with files in it. So I cannot create a Maven project in a git local repository. So using the archetype should probably come first.
  • I also need to create a repository in my github account. If I do this through the github desktop app, this already creates a local repository for me.
  • Now I have an Eclipse project and a local repository, up to now unrelated. How should I "relate" them?

I found a lot of information about cloning from github, but my github repository does not have a Maven project yet, so I cannot import in Eclipse. If I use command line instead and an empty directory, I cannot apply Maven archetypes any more because the directory is not empty.

I am confused. Can somebody de-confuse me?

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142

3 Answers3

2
  • Create a new Maven project (however you want to create it). This, of course, will create a new directory in the file system with all the resources.
  • Create a repository in Github.
  • From the command line (located inside the project directory) do git init. This will create the local repository inside that directory.
  • Next, add a remote to your local repository: $ git remote add origin https://github.com/user/repo.git (optionally replace origin for the name your remote repository is going to be called, also replace user for your github username and repo for the name of your github repository name)
  • Add all files to be commited in the local repository.

For example

$ git add .

# Adds the file to your local repository and stages it for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.
  • Commit changes.

For example:

$ git commit -m "Add existing file"
# Commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.

Just be clear, taking the following structure as reference, What I propose is to have as git repositories any of the directories project1, project2, projectN, not the whole worspace (/roo_dir/workspace/ directory).

/roo_dir/workspace/
            - project1
            - project2
            - projectN

Additional readings:

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
  • So in your workflow, the local repository gets created in the project directory? Then I probably need to create it outside the Eclipse workspace because git repositories in workspaces are not recommended. – J Fabian Meier May 30 '19 at 09:42
  • Indeed, the local repository is in the project directory. That's the usual way to do it. I'm a bit confused about why git repositories in workspaces are not recommended, though. Here is some info about it: https://stackoverflow.com/questions/10531272/is-it-better-to-keep-git-repository-inside-or-outside-of-eclipse-workspace – lealceldeiro May 30 '19 at 09:44
  • @JFMeier see the last part of the (updated) answer. The idea is to have as git repository the directory containing the resources itself, not the whole worspace – lealceldeiro May 30 '19 at 09:52
  • 1
    I usually follow more or less the same step as @JFMeier, I don't really care much if Eclipse dosen't find it agreeable, because the project should not be dependent on the IDE you use... – minus May 30 '19 at 09:52
  • But if Eclipse performance degrades, this is not really good, especially if you have several projects... – J Fabian Meier May 30 '19 at 09:57
  • @JFMeier I have used eclipse with HUGE projects using the workflow I just described and performance has not been an issue, really. I recommend you to try this out and see how it works for your specific use case. If you have some spare time, let me know how it goes :) – lealceldeiro May 30 '19 at 09:59
  • I guess performance issues could be a problem, but I never experienced such problems because of git. I must say that I use git from command line to make commits and push / pull operation, it's less _automatic_, a more _deliberate_ action if you will. Then i also .gitignore all files eclipse related. So I really don't know if EGit has problems dealing with such a layout. – minus May 30 '19 at 10:10
  • @minus, I must say, I use EGit for tasks such as _commits_, and git console for _pull_ / _push_ / _rebase_, etc and have never experienced any performance issue. – lealceldeiro May 30 '19 at 11:24
0

Try this

  • Create a blank project in git hub
  • create a maven project in local
  • clone the git repo
  • this will not have any file
  • copy the maven project
  • add the files to git and push
Subhomoy Sikdar
  • 526
  • 5
  • 19
0

Finally, I did the following:

  1. I created a new Maven project with an archetype in the directory for github local repositories.
  2. I used the github app to create repository with the same name in the same directory.
  3. Then I published this repository to github.
  4. Now I imported the project from the github local repository to eclipse.

The workflow is generally the one suggested by @lealceldeiro, but without using the command line git.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142