How to use Mercurial, Maven and Eclipse together? - java

I am currently working on a Java project within a team of 5 colleagues for university. Actually, we will finish the planning phase within the next few days and then start implementing.
For the project, we have to use Mercurial (via Bitbucket.org) and Apache Maven. We'd like to use Eclipse as IDE. I know how to use Mercurial, and I've read some articles and guides about Maven. The thing I don't understand is how we should use those tools in collaboration.
What should be placed on the repository? The whole Eclipse project? Or just the source files and Maven's pom.xml? If the latter, how would a working session look like? Pull the files, create an Eclipse project with mvn eclipse:eclipse, code a while and commit/push them to the repo?
I am responsible for creating the project structure, so my colleagues - and me too - can start developing. But I really don't know how to start now. If the use of Maven wasn't obligatory, I would just place the Eclipse project into the repository. But having to use it, I'm quite confused now how if affects everything.

I use a similar setup, except I am using Dropbox for my repository (just two developers). Just follow these steps, with the m2eclipse (update site: http://m2eclipse.sonatype.org/sites/m2e) and the MercurialEclipse (update site: http://cbes.javaforge.com/update) plugin you can do all in eclipse.
Create a maven project:
File -> New -> Other -> Maven project
In Project explorer right click on the new created maven project
Team -> Share Project... -> Mercurial -> (leave the folder as it is) Finish
Then create an .hgignore file in the project root:
In Project explorer right click on the new created maven project
New -> File -> File Name: .hgignore and Finish
.hgignore:
syntax: regexp
target # maven output
\.classpath # eclipse
\.project # eclipse
\.settings # eclipse
test-output # eclipse junit/testng plugin output
Then you can make your first commit, but beware, hg does not store empty folders in it's repository, so to keep the maven folder structure with no source code in it you have to put a (empty) file in each empty folder, for a simple maven project without any sources it would be in the folders:
myproject/src/main/java
myproject/src/main/resources
myproject/src/test/java
myproject/src/test/resources
For example I put a file with the name .empty in it with the content "this is a placeholder file, remove if other files are in this folder"
If some of this folders are missing your colleagues would see errors in eclipse if they import your project.
First commit:
In Project explorer right click on the new created maven project
Team -> Commit... -> Select all and enter a commit message -> Finish
Now you can clone it to bitbucket (I don't have experience with bitbucket).
Tell your colleagues to install the two eclipse plugins and then they can get the repo via
File -> New -> Other -> Check out Maven Project from SCM -> enter url to your repo -> Finish
And then you are good to go.

Here are alternatives based on practices at the ASF (though we don't use Mercurial).
To begin with, set up a source tree with Maven build that you check into Hg. Don't touch eclipse yet. You'll want to find some Eclipse plugin for Hg.
Then, look at two alternatives: the maven-eclipse-plugin and M2eclipse.
The maven-eclipse-plugin is a maven plugin. You run it from command line, and it generates .project and .classpath and some of .settings. It's not currently being worked on, but it's quite stable and functional in many common cases. You then import 'existing Eclipse project' and you are good to go.
M2Eclipse is an Eclipse plugin that integrates Maven builds. It's quite ambitious. However, it's current avatar is quite new, and can be full of surprises.

Related

why do we need to build java code before importing to eclipse for methods autosuggestion

I have a fundamental question, but something which bothers me a lot. We sync code from perforce in my company. and then we are taught to build the code compatible to eclipse. Then we import the code in eclipse as existing project. Then when we hit ctrl-space from an object, we get suggestions of methods.
My question is: Why do we need to build the code for suggestions of methods? . After syncing from perforce, what I have is still an existing java project (right?). So eclipse should be smart enough to be able to index and find out from source code, the methods, when I hit ctrl-space on an object?.
Why do I need to build? What purpose the build serves?
The code does not have to be built, but to be able to import a directory as project via File > Import...: General > Existing Projects into Workspace into Eclipse, at least the file .project must exist.
There are several ways to import, for example, a Maven project:
Execute mvn eclipse:eclipse on the command line and in Eclipse do File > Import...: General > Existing Projects into Workspace
File > Import...: Maven > Existing Maven Projects without a command line call uses by default the in Eclipse embedded Maven
File > Open Projects from File System... (or in Git Repositories view right-click + Import Projects...) detects not only Maven projects but also plain Java projects and automatically configures them accordingly.
In all three ways the following files are created, which are required in Eclipse for the Java content assist (Ctrl+Space) to work:
.project - says whether it is e. g. a Java, a PHP or a C/C++ project (to be more precise, the project name, builders and natures are specified here, e. g. the project folder icon of a Java Maven project will be decorated with a M and a J based on the Java and Maven project natures)
.classpath - tells Eclipse where the source and output folders are and which JARs should be added to the classpath (in Maven projects, the JARs are not listed, but it refers to the Maven dependencies which are computed from the pom.xml file)
.settings/org.eclipse.jdt.core.prefs - contains Java compiler and optional formatter settings (e. g. which Java version; which problems should be ignored or shown as infos, as warnings or as errors, etc.)
With Eclipse Oomph you can automate even more: you select a project and based on a project-specific configuration, for example, a Git repository is automatically cloned and plug-ins necessary to edit the project are automatically installed and configured.
Eclipse uses relfection for the suggestion, it can be possible only if the .class file generated

How do I import gradle projects with with EGit in Eclipse? (libGDX)

I am quiet new to git but for now I have set up a repository on Bitbucket. The repository contains my libGdx Project which is gradle project containing 3 sub-projects (Android, Desktop and Core).
So now it is my aim to clone this to eclipse using EGit. Everything works until i have to choose a wizard for the Import of the projects.
After i added the repo i use Import > git to Import a Project from the added repository
Here i have to choose the wizard
If i choose "Import existing Projects" the program tells me that no Projects are found.
After that i tried the "Use the New Project wizard" but it confuses me and actually i want to Import a gradle Project and not create a new one.
The last Option "Import as General Project" gives me just Folders and files, so eclipse doesn't notice the existing Projects. Like i said i'm quiet new to git so i appreciate every help ! :)
Thanks :)
The wizard intends to create the project settings files for Eclipse (.project and .settings/).
It seens like you don't have them in your git repository (I don't think you should). That why Eclipse asks you to create a new "project".
the best way is "Import -> Gradle project" (you need to have gradle plugin installed in eclipse first)
But is you don't want to use gradle plugin you need first to generate eclipse file. To do so, type "gradle eclipse" in your working directory (see https://docs.gradle.org/current/userguide/eclipse_plugin.html) and then import existing project into your eclipse workspace.
Another thing, it seems that your workspace is in the same folder as sources (.metadata folder), you should create your eclipse workspace in another directory.

How do I properly organize jars into one folder in Eclipse?

I used Maven to generate a Jersey project from command line, and I also used mvn eclipse:eclipse so that I can import my project to eclipse.
After I opened the project from Eclipse I see all the jars were in the root folder, how do I create a folder or package so that I can move all these jars into it?
Don't use mvn eclipse:eclipse.
Just do File -> Import -> Maven -> Existing Maven projects and browse the folder where your pom.xml is and Eclipse will organize everything for you.
It looks like you aren't using Maven eclipse integration - m2eclipse. You shouldn't have to install it, it comes already with more recent versions of Eclipse, though follow the instructions in that link if you don't yet have it.
If you just generated the project, try deleting it and then generating it again inside eclipse with New -> Other -> Maven Project. If you have a lot of code written already, instead right click on the project and do Configure -> Convert to Maven Project. Once you've done that, you may have to do Maven -> Update Project project to straighten out your jars, and possibly remove them manually with Build Path -> Configure Build Path.
However, I recommend creating the project from within Eclipse, if possible.
Addendum: #cahen's answer is also correct; don't use mvn eclipse:eclipse.

Can I convert eclipse source folders into packages?

I've forked a Github project, used Eclipse to clone it locally, and imported that as a general project into Eclipse.
The accompanying .project file is an Eclipse .project file with the proper "nature" and "buildCommand" xml tags.
So it seems like a valid Eclipse project. However, I'm unable to specify a run configuration or configure the build path.
When I select the Build Path popup menu item, it says "no actions available" in grayed out text. When I select "Run/Run As" from the main menu,
it shows (none applicable). I'm wondering if the problem is that the project imported completely as source folders:
There are no packages to speak of. I created another project from scratch, and created the proper source folders and packages as needed
to match the package statements in the source code. After manually importing the source from the git repo, I can build and run that project.
If the lack of packages is indeed the problem, is there a quick way in Eclipse to convert source folders to packages?
What you did is you have probably cloned the repo in Eclipse and then Imported this project through a New Project Wizard, because in GitHub there is no existing .project (and no .classpath) files.
The "New Project Wizard" will create a set of defaults for a java project (I suspect that you selected just that), but is anaware of Maven structure, so all source folders will not be recognized and you will end up having to define them on your own. Worse, you will be unaware of any special parts of the Maven build that might be configured within pom.xml.
Because this project uses Maven for building, it would be better to use M2Eclipse while importing it. Install it using Help->Install new software.
Then there are a couple of steps required to make it use all Eclipse features.
Keep your cloned copy of the repository or clone again if you want to start from scratch. Then use File->Import feature to import a maven project into the workspace. Select Exisiting Maven Projects and point to the directory containing pom.xml file in the cloned repo. This will use Maven integration in Eclipse to generate .project and .classpath files based on pom.xml contents, so you will be able to more closely mimic Maven build in Eclipse. All source folders should be properly discovered this way. Eclipse might want to install some additional integrations for Maven features that this particular project uses. Let it, if that is the case.
Now, you will have the project operational and compiling in Eclipse, but it will not be aware that it is managed by Git... This is because M2Eclipse and Git Team provider are not integrated (at least they weren't when I last checked). In order to be able to commit to the repository in Eclipse, remove the project from workspace, but without deleting contents. Then, import from Repository view using Import Projects/Import exsisting Eclipse projects. Since necessary .project file is already generated, Eclipse will autodiscover the project and will use the right configuration prepared earlier by M2Eclipse.
In the end you will have a properly configured Maven project with Git as a team provider for it.
If this is a project meant to be built by Maven that contains a pom.xml file, install M2E before importing the project from your local cloned repository. It will handle this.
Right click at root of project select properties, in the sources tab add the folder "src/main/java" as source folder
Other way is to configure facet as java

How to import a GIT non-Eclipse Java project into Eclipse?

I have some problems importing a Java project into my workspace. I am following this tutorial - however I can not use the final Import existing projects step because the GIT repository I use does not include the Eclipse specific .project and .classpath files.
Use the New Projects wizard
Therefore the project is not recognizes as project and hence can not be imported. Therefore I tried my luck using the option Use the New Projects wizard and select "Java Project" in the next dialog. The problem is that this creates a new Java project without any content!
The project is also not connected to the GIT repository.
Edit: This is a known bug of eGIT: Bug 324145 - Project import doesn't work for abitary project types - if you want this problem fixed vote for it...
Import as general Project
If I use Import as general Project Eclipse always wants to use the external repository directory as project directory which is not what I want and additionally the created Project is not Java-enabled.
Therefore I am asking why it is so complicated to import a Java project into Eclipse using eGIT?
It is possible by first cloning the repository and then creating a General project based on that. Then you can convert it to Java project. Here is how:
First go to File>Import...>Projects from GIT.
In the Select a Git Repository view you first press Clone. And follow instructions. This will create a local "checkout" of the repository to your computer. You can set the folder to be your workspace so it looks like any other of your eclipse projects.
After you have cloned the repository you get back to Import-view. Now you can select the repository you just cloned from the list.
Click Next and select Import as General Project. Now you have a git repository to eclipse.
Convert it to Java project: Add nature and buildCommand elements from other Java project to your .project file:
Relevant sections from .project:
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
Then from Project>Properties>Java Build Path>Source add your source folders (and possible libraries).
Edit: Added the conversion to Java project.
With Git (especially EGit) your 2 best options are:
1) Create a java project in eclipse, and then create a linked folder to where the source lives in your git repository (mentioned by #mattb). I don't think EGit will connect to your git repo easily in this mode, but your eclipse specific project files will be in a different location than your source tree.
2) Create your java project and let it point to the external git repo (which you mentioned). It will create a .project and .classpath file where your source lives. Then using Team>Share Project will allow you to connect EGit to the already existing git repo.
Option 2 (which I use) allows the tools to work with java projects in a git repo reliably.
May not be applicable to your project but if you are using Maven in the project, you can import it as Maven Project from Eclipse if you have m2e installed, this way all the needed files like .project, .classpath will be generated. I think that is a good approach because if your pom.xml is well-written, it can contain all the needed information about the project such as build target directory, classpath, java version etc., and it will probably work with most of the populer IDEs.
I suggest to get used to use Maven on every java project, even for a simple hello world application because I see it as some sort of "standardization" for Java projects.
Create a new project in eclipse and just point the source directory at the existing sources, rather than the default path.
here I saw all methods to import a non eclipse project into eclipse(open source git projects into eclipse) ... no need to copy .project or .classpath file to copy ... here is the step by step process-
Step 1. import the project from git. Paste the URL and import the project as a general project.
Step2. If you want to import it as a maven project you should have m2e plugin installed in your eclipse(eclipse marketplace).
Step3. Now we should convert this general project into eclipse project.So right click on project ->properties->ProjectFacets. click on ConverttoFacetedform
Step4. Now if your project is a web project.See the left side window.. there is an option DynamicwebModule ... check it (leave it if it's not a webproject).... On the rightside window .. go to runtimes tab. Now check the server and JDK version both.. apply Ok.
Step5. If it's a maven project .. then right click on project congigure->convert to maven project->Finish
Steps:
Import project as a normal git project
Right click on the project and select Configure -> Convert to Faceted From
Now Select Faceted whichever you want to choose for simple java project just select Java from the Check box List
you can add more configurations based on your requirement under runtime tab on left and under "Further configuration available.." link below the pane. you can skip this step if no additional configuration required and you just need a simple java project.
click on apply and then apply and close.
It will convert your simple git project to simple java project of Eclipse.
-- Happy coding :)

Categories