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

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 :)

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

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 force Eclipse to see java project as java project?

I have downloaded a 3rd party project, which consists of multiple files, including Java ones.
General structure is as follows:
<topfolder>
pom.xml
<subfolder1>
pom.xml
src
main
java
<normalclasspath>
resources
site
apt
index.apt
test
java
<normalclasspath>
Eclipse imports this project normally, but is unable to index its Java content. For example, I can't browse from a variable to its definition, and so on.
Also I can't set my own Build Path since it says No action available.
Of course, I can refactor folder structure myself to suite eclipse needs, but are there any automation means for this?
UPDATE
Yes, this is a Maven project and Eclipse already knows that it has Maven nature. The only option now is to disable it
But this is not a question. The question is how to add JAVA NATURE, so that Eclipse knows consistence of classpath and be able to navigate to class definitions and so on.
UPDATE 2
#75inchpianist's answer about facets helped partially. It was not available to select Java facet immediately, but required turning on facets at all first. Then Java facet was already there.
Now I see, that Eclipse interprets Java, but Maven interpretation is not full. Namely, no Maven dependencies interpreted (no Maven Dependencies node in Package Explorer).
The attempt to add it fails:
Right click on the project and select "Properties"
Within "Project Facets" make sure that Java is selected!
As this is a maven project (because of the pom.xml) you need to install a maven plugin for eclipse.
Next you have to right click on the project and choose "Configure->Convert to Maven Project".
Now you should have your normal java structure.
Assuming that Eclipse recognizes the project as Java project yu can do the following:
If you don't want to install a maven plugin into Eclipse you can make Eclipse recognize the source correctly by adding every src/main/java folder as Source-Folder to your Build-Path.
This is better than modifying the folder structure as you can update the sources without problem later.
Add following to your .project file and refresh the project.
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>

SVN checkout doesn't create packages - java

I'm trying to check out a svn repo. So far so good, the checkout worked but there are no Packages. Is there a way to get the right packages in eclipse?
All the given data I do have is this:
svn co https://jensembl.svn.sourceforge.net/svnroot/jensembl jensembl
Furthermore, I'll add the link to the program, which i want to get working.
link to JEnsembl
/edit added a link to the explorer view of the current project
link
You need to generate eclipse project files using Maven.
See http://maven.apache.org/guides/mini/guide-ide-eclipse.html
Basically:
$ mvn install
$ mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo
$ mvn eclipse:eclipse
You can import as a maven project and I feel that is the better way also
I have gone thru the repo you mentioned.
In Eclipse
File--> Import --> Check out Maven Projects from SCM
select svn and use this url
http://jensembl.svn.sourceforge.net/viewvc/jensembl/trunk/
or any subdirectory which contains the pom.xml
and see you can proceed further
On your harddisk a package is a normal folder. If you have the project in eclipse it has to know which of the folders are source folders. Eclipse will show all folders in a source folder as packages and recognize the classed as part of the project. Open the properties of the project. At first you have to go to the category Project Facets and enable Java. After that you can go to the category Java Build Path and add all source folders in the Source tab.
Furthermore you checked out the whole SVN repository which seems to contain a few projects/modules. You want to check them out (or at least import them to Eclipse) seperatly from the trunk folder. I'm also seeing that your project is a maven project so you might want to read up on maven and how to check out projects in eclipse. Here is a short how-to. Again you only want the trunk folder. If you want to know what that is you can read it up here.

How to use Mercurial, Maven and Eclipse together?

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.

Categories