What's a good Maven project structure when developing in Eclipse? - eclipse

I am new with the Maven. I have two projects. I want this kind of tree:
project1
--pom.xml
project2
--pom.xml
pom.xml
which means I performed aggregation and inheritance. By doing like this and importing two projects in eclipse, the second project can't see junit dependency which is specified in the outer - common pom.xml.
The problem with the first project is that instead of having packages i see only folders.

Try using Package Explorer in Eclipse in order to see packages instead folders.
Also for this kind of inheritance try to add both projects in one parent project and import this one into Eclipse. In both .pom files of projects you must specify their parent project

As MykoB has said, ensure the common pom.xml has project1 and project2 defined as modules. In each of the child project, specify the common pom.xml as parent.
Choose the folder containing the parent pom.xml when importing the project in Eclipse and you should have the parent and both the child modules imported correctly.
This assumes you have the latest Eclipse Indigo with m2e plugin.

What you've built is the correct default Maven module structure, but Eclipse likes to have all its projects at the same level. That is, your root pom.xml which contains the module entries should be in a folder of its own, like so:
WorkspaceFolder
\_project1
\_pom.xml
\_project2
\_pom.xml
\_Builder
\_pom.xml
\_Parent
\_pom.xml
The Builder pom.xml has module entries that look like <module>../project1</module>. That's your aggregation part. Additionally, if you want to have a parent pom for inheritance (which is not necessarily related to having a pom with modules), you'll have to add the Parent entry to project1, project2 and Builder's pom.xml, and you'll need to have a <relativePath>../Parent</relativePath>.
Note that all relative paths come from the problem that you're adapting your subfolder structure to what eclipse likes.

Related

Importing multimodule maven project from Git into Eclipse/STS

I have been desperately trying to import a multimodule maven project from GIT into my (freshly installed) STS 3.9.4.
During the last hours I tried lots of solutions that were posted on stack overflow, but none of them worked for me.
These are the steps I did:
git cloned the repo from the command line (intentionally, because it didn't work either when I used the GIT integration of STS)
imported the project via File / Import / Maven / Existing Maven Projects
After the project was imported, it looks like this:
(please ignore the red icon at the top left, the problems persists even when this icon is not there)
I can Run As / Maven Install any pom.xml, but when I try to edit java sourcecode Eclipse tells me The resource is not on the build path of a Java project.
To fix this I tried:
convert to faceted form
added Java nature
Configure Build Path / Set as source folder
add buildcommand org.eclipse.jdt.core.javabuilder to .project
Being able to build is fine, but editing sourcecode without support for Organize Imports, Format Source, and so on is a real pain.
From your screenshot I assume the modules are nested within the directory of the reactor pom(=parent pom) and you have imported all of them as projects (reactor and modules).
Don't add a java builder to the "reactor" project.
Make sure not to open the java files from the projects corresponding to the reactor pom, but from src folder within the corresponding module projects.
The solution to this problem was somehow hidden, but in the end I have to say the reason was clear.
After I checked with a different multimodule project (https://github.com/Activiti/Activiti.git), I was sure that my STS and other components don't suffer from a generic problem. So the cause must be located in the project itself.
The multimodule project was setup correctly on the pom.xml level. But the topmost pom.xml was modified: most of the modules had been commented out:
The developer who introduced that change didn't experience any problems because he kept his already existing workspace. And the problem only occurs when a new workspace gets created and maven scans the pom files.

How to import a maven project from SVN tags into eclipse?

(This may be a dumb question but I am not quite familiar with the interesting eclipse importing logic)
I am trying to read the source code of Netty, so I checkout the tag folder for Netty-4.0.24.Final from the Netty SVN (https://github.com/netty/netty).
The files are shown as below:
Since there's a pom.xml files there, I use the "import existing maven project" to import it into Eclipse.
But the imported result looks like this:
I am expecting some well-formatted package layout. What's wrong?
ADD 1
I found this thread: "Source folder is not a Java project" error in eclipse
It seems I need to manually turn on the Java Project Facet for each project in order to see source code organized in packages. But I am wondering does it have to be this tedious? Is there any simpler way to import a maven project and display it in packages?
It doesn't matter if you are using tag or latest branch, Netty project composed from a sub-projects you see in the worskpace
Yes each one is full Maven project on it's own and gets deployed as separate artifact into Maven repo. When you open any of them, there will be your familiar Maven folder structure.
Your highlighted pom.xml is multi-module project for all the sub-projects.
It's used to build all sub-projects in one go.
If want to get familiar with this concept: I would suggest this tutorial:
http://books.sonatype.com/mvnex-book/reference/multimodule.html
BTW, when you import sub-projects into Eclipse M2E plugin names projects according their artifact names, not according folder structure.

Eclipse adding the project overrides maven dependency

I have a maven project that uses other small projects. And I added them to master's pom file.
So my Eclipse project properties looks like following:
>Master project
>Java Resources
>Libraries
>JRE System Library
>Maven dependencies
>smallProj1.jar
>smallProj2.jar
The thing is I do not have the all code for smallProj1 and smallProj2 and whenever add them to my workspace Eclipse overrides the maven dependencies and simply calls the two projects instead the jar files from my local maven repository and then I have all over the code those red underlines. Project properties in Eclipse looks as following after I add them to my workspace:
>Master project
>smallProj1
>smallProj2
>Java Resources
>Libraries
>JRE System Library
>Maven dependencies
>smallProj1 (not jar anymore - folder)
>smallProj2 (not jar anymore - folder)
Since I also want to have a look to smallProj1 and smallProj2 code while I am coding, I want to keep them both in my workspace. But I want my Master project to use the jar files that I provided, not the small projects in the workspace.
Any ideas on that?
You could right click on a project and set "Maven" -> "Disable Workspace Resolution". This will let Maven find the dependencies from Maven and not within the workspace. I don't think it is possible to do this on a dependency basis.
But I think in your case it would be better to fix the broken smallProjX in your workspace (as they exactly contain the code you need), so that the master project could be build correctly.

Multiple versions of the same multi-modules maven project under eclipse

I'd like to work on two different versions of the same multi-modules maven project under eclipse.
Unfortunately, when you import a multi-module maven project under eclipse, you can change the parent module project name yourself, to prevent conflicts, but not the child modules projects names. Child modules are imported as root projects, named after the module name. Apparently there is no way to rename them during import.
In an ideal scenario, I'd like to keep the pom as it is. Obviously I'd like to keep the same eclipse workspace...
Yes, you can. You have to change the name pattern in the m2eclipse importer. All modules will have this pattern and you have a conflict-free env.

m2eclipse multiple module projects

I have one parent parent maven project that defines dependencies.
Then I have my parent project, which inherits from parent parent project. My parent project has multiple modules each of which has a pom file.
From command line I can just build my parent project and it all works, all my modules compile and build, and even tests pass.
But when I create new java project from root of my parent project in eclipse, it never creates Maven Dependencies, and I don't see the jars from the parent parent project. I end up not being able to see any of the jars that my projects use.
I've tried updating project dependencies, I've tried updating project configuration, and numerous other cleans, builds etc, but nothing has worked.
Any idea how I might get this to work?
Thanks
--MB
In order to work in eclipse, each module needs to be its own project. You will have to add each module to your workspace individually. M2Eclipse should still recognize the parent POMs as long as they are either in your local repository, or at the path specified by the relativePath attribute of the parent group.
If you want to include all of the parent project POMs in your workspace (so you can use the POM editor on them), you can add them as projects, then remove all of the source folders from the Project Build Configuration.