Strange issue between Eclipse Mars and Maven - eclipse

Some context
I am creating a Spring Boot base code application that is in fact separated in three projects/modules:
springbase-core
contains:
custom annotations
custom exceptions
custom generic Spring components
model (entities)
metamodels (generated by maven through the maven-processor-plugin)
utils
depends on: nothing
springbase-data
contains:
data related annotations
services
repositories
data related util classes
depends on: springbase-core
springbase-web
controllers
form classes
filters
listeners
interceptors
JSPs
JSP Tags
etc.
depends on: springbase-data, springbase-core
The problem I am having is that if I have springbase-core in my Eclipse workspace, I'm not able to import metamodels in repository classes in springbase-data. If I remove the springbase-core project (where metamodels reisde) from my Eclipse workspace, it works.
I have also tried copying springbase-core-0.0.1-SNAPSHOT.jar from my .m2 repository, directly at the root of my project (and taking care of adding it in .classpath), and I was able to import metamodels.
When opening springbase-core-0.0.1-SNAPSHOT.jar with WinZip, I can see the metamodels. Building springbase-core with Maven also shows the generated metamodels under /target/metamodel as configured in my pom.xml.
Looks like a glitch between Eclipse and Maven.
Any thoughts?

Here is a solution to my problem: Right click on all projects, click on Maven, and then Disable Workspace Resolution.
That way Maven is going to look for the dependency only from .m2, regardless of what's in my workspace.

Related

In Eclipse, how do I exclude test folder of one maven project from another project that has as a dependency?

I have two maven projects imported into Eclipse in the same workspace. Both have a class with the same name and package, but different implementations and in different locations in each project. Let's call this class com.namespace.Factory
Project A has Factory under its test folder, i.e: /src/test/java/com/namespace/Factory.java
Project B has Factory under its main source folder, i.e: /src/main/java/com/namespace/Factory.java
There is also a Project C which is dependent on both. Project C also uses Factory from Project B for some of its unit tests. Problem now is Eclipse can't compile Project C because it can't differentiate between the two Factory classes. If I build all projects in command line, they don't have issues.
You would think that Eclipse would ignore the Project A Factory class since it is in test.
I am using the m2e plugin. My current work around is to setup m2e to not resolve Project C's dependencies within the workspace. This forces it to download the jar that will not have test in it. However, this means I have a change in either A or B, I have to manually install A or B push the latest jar to the local repo, and update Project C's dependencies to pull down the latest jars.
Is there a way to exclude the Project A test folder from the build path in Project C so that I can continue resolving everything within the workspace? It feels like Eclipse is breaking something that is fundamental to maven projects.
I think you're just another user affected by the upstream bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=376616
To sum it up, the bug report discussion includes:
JDT implements just one buildpath per Eclipse project. This is very unlikely to change, since this was fundamental design choice and many APIs and implementation details rely on that.
Well, but that doesn't really answer your question I guess.
So I see multiple options here, depending on how much influence you might have on the projects:
either try to rename one of the classes => names would be unique
or if the classes contain basically the same functionality, play with dependencies between projects, or even create new one, that the other 2 would depend upon
that's pretty much what comes to my mind right now
Perhaps you can try this:
In project A's Properties dialog (get there by right clicking the project and then click Properties),
click Deployment Assembly on the left.
Eclipse will show all source folders.
Select the test folder (/test) and click Remove.

Maven integration with IDEs

I thought I understood Maven as I've worked on a few projects using it, but I seem to be having trouble creating my own project with it, so can someone correct me wherever I may be wrong?
Assuming I create a new Maven project (we'll say I did it through Eclipse or IntelliJ):
If I add a dependency to my POM file, assuming I add a public repository where that dependency can be found, shouldn't the IDE download the specified JAR and make it so that it is recognized when I use it in my code? Currently, my projects do not recognize any classes that should be found in JARs via my POM dependencies.
Assuming #1 does work, how can I determine via maven which transient dependencies I have? For example, I'm using classes from Pentaho Data Integration, and some of the plugins for it reference things like org.mozilla.javascript.*. Could maven automatically tell me that without me having to run the code, see it fail, and manually fix the dependency? There will be hundreds of those instances here, so manual fixing really isn't viable.
Here are my IntelliJ two cents:
1 - Adding a dependency in pom.xml of your project
Should indeed download the depended jar(s). You may need to approve "Import Changes" dialog if it pops in, or enable auto import.
2 - Seeing transitive dependencies
It can be achieved via Maven Dependencies Diagram - unfortunately only in IntelliJ Ultimate edition. You can use Maven Dependencies Plugin to see the dependencies tree in your favorite CLI.
Question 1: Adding a dependency
In Eclipse, depending on how you created the project, you should be able to add dependencies that are automatically recognized using the maven context menu.
Note that you should have created the project using the eclipse maven plugin so that it has maven nature.
To add dependencies/plugins from a remote repository, you can search in the resulting UI for a dependency if you know the artifactId or groupId. The plugin will pull up the deps whether the repo url is specified in the pom.xml or not.
After adding a dependency to the POM, the IDE will start downloading it and all transient dependencies as soon as you save the file.
If something goes wrong, you can try to "Update Project" from the context menu.
Question 2: Determining transitive dependencies
Transient dependencies are visible in the "Dependency Hierarchy" tab of the POM editor.
I usually default to the command line because it allows much more flexibility and functionality when tracking the dependency graph.
I am sorry but I have not worked with IntelliJ

What's a good Maven project structure when developing in 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.

Inter-module class usage for a Maven project in Eclipse

We're using Maven for a large project that is broken up into eight or so modules. The structure of Maven projects in Eclipse makes it difficult to understand class usage between modules - if module 1 defines class Widget, and module 2 uses it, then you can't just look for references to class Widget, because from the Eclipse perspective the Widget in module 1 is different than the Widget used by module 2.
Is there a good solution to this, other than "don't use Maven" or "use the global Java search"?
m2eclipse has an option in the Maven project context menu called "Enable Workspace Resolution" which controls whether the Maven artifacts or the workspace projects are used to resolve intra-project dependencies. Turning this on set Eclipse to look for the workspace version of the class and solved my problem.
I daily work in Eclipse on a large project split into several maven artifacts using Eclipse. I use the m2eclipse and I have no issue to navigate from a module to another. Eclipse is even able to use a opened projects as an artifact rather than fetching it from the repository.
The only restriction I saw is that you should have a one artifact to one Eclipse project mapping and it works flawlessly.

Can someone explain Maven 2 (or 3) dependency resolution to me?

I used Maven before in quite a few projects but they we already configured by someone else. I quickly understood the main concepts (groups, artifacts and versions for jars, local and remote repositories) so I assumed that if I'm asked I'll be able to set up a new project from scratch easily.
That turned out not to be the case when I deal with multiple modules which depend on one another. I poked around maven docs but they are either way too concise or way too technical (sometimes I get a feeling that Maven people wrote it for themselves and not for Maven users). So I'm asking for community help.
Here's a typical scenario:
Three repositories:
My local repo at <User>/.m2
My company repo at http://maven.mycompany.com/repository
Maven Central at http://repo1.maven.org/maven2/
I'm building a normal Java EE project consisting of one EJB jar with a corresponding client jar, a web module and a JPA module (I like keeping my entities and all db-connectivity separated from business logic). That gives me 5 project in my IDE:
myapp-ear
myapp-ejb
myapp-ejb-client
myapp-jpa
myapp-web
myapp-jpa is used by myapp-ejb and myapp-web. myapp-ejb-client contains business interfaces for my EJBs.
How should I set all that up? I suspect that I need some kind of parent project but I'm not sure how should I organize inter-project dependency resolution. For example: currently when I specify
<dependency>
<groupId>com.mycompany.myapp</groupId>
<artifactId>myapp-jpa</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
in myapp-ejb's pom.xml maven tries to search for it in my repository and tells me that the .jar can't be found. Even if I build and install all app components into my local repository one by one Maven always fails to build the ear file itself :(
I'd love to get it working under Eclipse or NetBeans.
I think you're on the right path. You need a parent project to pull it all together. In the parent project (and here is the important part) you need your dependencies laid out NOT in the dependency section of your parent pom, rather, you need them in your dependencyManagement section. Then, in your child pom's you can declare dependencies that are in the parent.
The only other gotcha (that I can think of) is that when you check the project using a version control system in Eclipse you need to remember to check the parent project out "as Maven project" otherwise the m2Eclipse plugin tends to freak out and not resolve things properly.
I hope this helps.