Blog

Importing Java code into Git on Visual Studio Online from Eclipse

01 Jul, 2015
Xebia Background Header Wave

Last time I wrote about Creating a Build.vNext agent on Linux. I ended that post with the idea to build a Java application on Linux. The first thing that we’ll need is some Java code in VSO to build. Being a Java newbie, it took me quite a bit of figuring out on how to do that. I thought I’d share my experiences here, for you to enjoy.

Install Eclipse and Team Explorer Everywhere

The first thing you’ll need to do when working with Java is installing a Java IDE. You can download it from here. I chose to download the “Eclipse IDE for Java EE Developers”, since that already includes some things that are usefull later (like Maven integration). Installation is as simple as unzipping the files to a folder of your choice. However, I did have a strange issue when using the default Windows unzipper, resulting in Eclipse not starting up. When using 7-Zip, all is fine. Once you get Eclipse running, you’ll see a nice empty interface.

imageNext we’ll install Team Explorer Everywhere.  Go to “Help”, “Install New Software…”

imageThen click the “Add” button to add a repository.

imageEnter the details for the Microsoft Eclipse repository and click OK.

image   Next, make sure the Microsoft repository is selected, check the TFS plug-in and click Next.

imageAfter finishing the wizard the TFS plug-in will be installed and you’ll need to restart Eclipse. You can open the plug-in by clicking “Window”, “Show View”, “Team Explorer”.

imageFinally, click “Connect to Team Foundation Server” and select your VSO account and project.

image Voilà, you are connected to VSO from Eclipse!

image

Upload code into repository

Before we can build something, we’ll need something to build, obviously. I’ve put a demo application on my GitHub, which you can download here. Unzip the file and put the “Demo/hellomaven” folder somewhere on your hard drive. In my case, I used “D:Demo”.

image In my VSO project, I have created an empty Git repository to hold the code. We can use Eclipse to push the code there. First, we’ll need to clone the repository. In Eclipse, go to Team Explorer and click “Git Repositories”. Then right-click your repository and click “Import repository”.

imageIn the wizard, select your repository and click “Next”.

imageOn the next page, select a local folder where you want to clone the repository. A subfolder with the name of your repository will automatically be created. Don’t forget to provide your credentials. If the account you used to log-in to Windows is something different than what you’re using for VSO, you’ll need to provide your alternate credentials here.

image Since there is nothing in the repository yet, we can’t select an initial branch…

imageNext, the wizard will ask if you want to import or create Eclipse projects from this repository. Since I’ll import my demo project into this repository later, I’ll select “Import detected Eclipse projects”.

image Since the Git repository is empty, there will be no projects to import. That’s OK, we’ll add a project later. Click “Finish” to start cloning the repository. It’ll appear as if nothing happens, but you will get a (empty) copy of the repository on your local disk.

imageNow we can import our code! First, we’ll open the project in Eclipse. To do that, right-click in the Package Explorer (“Window”, “Show View”, “Package Explorer” if it’s not visible) and select “Import…”.

imageSelect “Existing Projects into Workspace” and click “Next”.

image   Browse to the directory where you unzipped the demo project (or your own code of course), make sure your project is checked and click “Finish”.

imageThis will import your project into your workspace and make it visible in the “Package Explorer” window. From there, we can move it into our Git repository. Right-click the project and select “Team”, “Share project”.

image  Select “Git” as the repository type and click “Next”.

imageSelect your repository in the dropdown list, make sure your project is checked and click “Finish” to move the project into the Git repository.

imageNow all we need to do is commit and push our code to VSO. To do that, right click your project and choose “Team”, “Commit”.

imageType a meaningful commit message (you always do this, right?), select all files and click “Commit and Push” to start pushing everything to VSO.

image Since this is the first time we’re pushing, we’ll need to select a remote branch to push to. In this case, I’ll go with the default “master”.

imageFinally, click “Finish” to start the magic…

imageIf everything went smoothly, you’ll get a result window without any errors.

imageYou should also see your code in the “Code” tab of the VSO web interface.

imageSo that’s it! You now have your Java code in a Git repository, hosted on Visual Studio Online! You can now start using Git from Eclipse, where all the usual Git stuff (commit, branch, merge, history, rebase, etc) is available under the “Team” submenu in the “Package Explorer”.

imageYou can also use the “Git Staging” view, which will give you a nice and easy way to select the files that you want to commit. Just drag files from “Unstaged Changes” to “Staged Changes” to include them in your commit.

image

Hopefull this will get you up and running with Eclipse, Git and VSO quickly! My next post will be about using VSO Build.vNext to actually build the code.

Happy Gitting!

b.gif?host=realalm

Kees Verhaar
Welcome to my blog! I am an ALM consultant for Xpirit Netherlands B.V. Sounds fancy, doesn’t it? Basically it means I help people to do their work as a software engineer better, faster and easier. In the past couple of years of working in the field, I have encountered many things of which I thought: “I should share this!”. This blog is my way of doing that. You’ll find real life stories, tips & tricks, examples and probably even some code here. Hope you enjoy! If you have any suggestions, please feel free to drop me a line.
Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts