How do Maven and m2e-Eclipse-Plugin cooperate - java

I have some questions about the m2e-Eclipse-Plugin. Version of m2e is 1.6 and the version of Eclipse is 4.5 (Mars).
In my Eclipse-workspace I have a Maven-Project (Java web application) that is deployed on a local Tomcat server.
I really would like to know how m2e-Plugin and Maven work together. If I change a Java-file and save it, the corresponding Class-file in the mvn-target-directory is immediately updated.
Is this compilation done by the default Java-Builder in Eclipse or is it done by Maven ? (i.e. the mvn-compile-plugin does the compilation). If the latter case is true, how is the compile-goal executed ?
Can I configure which mvn-goals are executed if a Java-file is changed and saved? If the compilation is done by the Java Builder, how does the Builder know that it has to copy the compiled
file to the mvn-target-directory
?
I found two posts:
How do Java and Maven builders work together in eclipse?
How do I start Maven "compile" goal on save in Eclipse?
that deal with similar issues. But most of the proposed configuration settings didn't work in my Eclipse.

The compilation in your scenario is done by the Eclipse compiler and not by Maven nor the maven-compiler-plugin. M2E Maps all things to Eclipse internals to handle most of the things needed to work inside Eclipse.
During an initial import of a Maven project into Eclipse using M2E (creating .project/.classpath/.settings) the life cycle will be run till generate-resources and therefore some plugins are run and can create other stuff (things like ANTLR/JAXB generators etc.).
Eclipse handles the correct target folders by reading the pom.xml files and by M2E it is translated into configuration for Eclipse etc. The things you are referencing about configuration is simply not working cause those posts are too old...

Related

Not all Gradle projects are created equal, according to Netbeans

I'm checking whether we'd be able to migrate from Ant to Gradle, but got confused right at the very beginning of these checks - Apache Netbeans 12 LTS (+ Gradle plugin from official repo) refuses to properly open Gradle projects that were created by a another Netbeans instance, which is a major pain.
I tried to open (in Netbeans) one of the Java library project examples from Gradle docs, only to find out the IDE immediately spews out errors (missing imports for tests) that are unjustified and offers a very limited amount of IDE integration - forget running specific tests, even debugging is all grayed out. Gradle and Netbeans also see different classpaths.
If I create a Gradle project inside Netbeans, everything works fine - no errors, Projects Tree shows an additional tree node, called "Configurations", like in the image below, I'm able to debug and everything just works.
However, even if I just copy/paste this project's directory to a different location, everything breaks after the project is reopened (I also get this issue, same thing happens for example projects found in Gradle docs).
What is going on here? Netbeans seems to know more about a project it created, than about projects that were created outside it or were just relocated. How do I force it to treat all Gradle projects equally (so that they work as expected)?
I used Gradle 7.0.1 and let the New Projects wizard "Initialize the Gradle wrapper" for the project created inside Netbeans 12 LTS. The setting to prefer existing wrappers is enabled in settings if relevant.
Enabling an "experimental" option in Gradle options, called Enable 'lazy' Source Group Initialization does help with simple projects that were created by Netbeans, so they open as expected.
But this does not work for Gradle projects that contain subprojects, such as the example project from Gradle docs:
The issue tracker for this Netbeans plugin has been quite active recently, mentioning issues like this, so perhaps there is hope.
At least part of the problem is that the LTS release of Netbeans (at the time of this writing) doesn't support gradle 7. The latest release, Netbeans 12.4, is the first version that supports gradle 7.

gradle eclipse dependencies, skipping testCompile/providedRuntime etc

Gradle is used to build a simple web application, split into handful of eclipse projects within the same workspace. Gradle script runs tests/creates deployable EAR file just fine. EAR file runs without any issues on a Liberty Profile server.
However, I have problems running it from eclipse (with gradle plugin). The root of the problems seems to be automatic "gradle depencency" management (bunch of dependencies that get injected by "classpass container" org.springsource.ide.eclipse.gradle.classpathcontainer). This thing seems to pick up any dependency, be it compileOnly, testCompile, providedCompile etc. This leads to libs with classes like javax.persistence., javax.inject that are needed for standalone build, but are provided by the application server and are not needed when running from eclipse, being deployed to the app server as part of the application and cause all kinds of errors/warnings.
Is there a way to filter dependencies that gradle eclipse plugin picks up for deployment?
This is a long standing issue with STS gradle tooling. It stems from the fact that gradle-tooling-api doesn't distinguish between provided / test etc. dependencies in the model it produces for the 'eclipse classpath'. At least it was the case when STS gradle tooling was implemented. The tooling-api model has evolved since then, but STS gradle tooling is being phased out and this is unlikely to get a real fix.
That being said, there is a workaround that was specifically implemented for this exact situation.
Go to Window >> Preferences >> Gradle (STS) >> WTP. There you will see a list of regular expression labeled Gradle Dependencies Deployment Exclusions. You can add more expressions there. These act as a kind of global filter. Any jar matching one of these expression will be excluded from WTP deployment assembly.
This may work for you.
However, it is a bit of a crude workaround.
So... I hear that BuildShip which is STS Gradle-tooling's official successor now has WTP support. So perhaps you want to try and switch over to BuildShip. I would assume it deals with provided, test etc. dependencies correctly w.r.t to WTP deployment assembly (if it doesn't you should consider filing a bug report against BuildShip).

Breakpoint not registered in eclipse in maven multi-module web application

I'm currently working with a maven multi-module application in eclipse.
In some classes I can set breakpoints, and after starting tomcat (via eclipse) in debug mode, they get registered (meaning, a small tick icon is displayed next to the blue round breakpoint icon), and the debugger stops there.
In some other classes, the breakpoint is not registered, and the debugger doesn't stop there.
Why? And what's the mechanism underneath, like, are breakpoints only registered for classes that were already loaded? Or how does that work?
Update:
Using Eclipse 4.5.2 under Linux, Tomcat started under Oracle Java 1.6.0_45
It is hard to provide answer for such generic issue... however here are some hints. Let's assume the structure of your multi-module is following:
foo-project (parent, POM packaging)
foo-library (JAR packaging)
foo-webapp (WAR packaging, depends on foo-library)
Now from Eclipse's point of view, you have 3 "separate" projects. You are running your foo-webapp on Tomcat and that is what you are debugging. If you place breakpoint inside foo-library that is a different project, potentially unconnected to foo-webapp.
So what you need to do is to make sure your foo-webapp has project dependency on foo-library so that Eclipse knows when you run one project, the other is used as well. This is usually done manually automatically but by the m2e plugin. So I hope you are using that and not the obsolete maven-eclipse-plugin. Next thing you should make sure is that your dependency is correctly defined in the pom.xml... if the required version is not the same as the version of the library, m2e might link you JAR and not the project itself. And last not least you need to Enable Workspace Resolution for m2e to actually start connecting projects.
If you are sure all the things above are correct in your case, you might try to update the project according to the POM via Right click on project > Maven > Update Project....
When everything is in place you should see your project dependencies under Maven Dependencies in your Java Build Path tab.

Setting different compiler levels for different source folders in eclipse in same project

I have a project that is build using ant and now i am trying to convert into one that is build in eclipse.
The ant files describes different compiler versions for different course folders i.e few are compiled using jdk 5, few using 6 and still few using 4.
How can we configure the project in eclipse to use different compiler levels in eclipse for different folders in same project.
I noticed that they had this bug lined up in eclipse and they said to release it on 3.5 and i am using 4.3. So it think the feature should be there.
Such feature does not exist. You will need to create multiple Eclipse projects.
I would suggest that you think twice before making a change in that direction.
ANT might not be the best build tool, but switching to Eclipse is way worse as you tight yourself not only to an IDE, but also to a specific configuration of both your instance and your project, making it less portable and more fragile than before.
I would rather suggest that you split your project into different modules/projects and use maven (integration with Maven and Eclipse is ok for simple builds) to organize both you CLI and Eclipse build procedures.

Eclipse won't believe I have Maven 2.2.1

I have a project (built from an AppFuse template) that requires Maven 2.2.1. So I upgraded to this (from 2.1.0) and set my path and my M2_HOME and MAVEN_HOME env variables.
Then I ran mvn eclipse:eclipseand imported the project into Eclipse (Galileo).
However, in the problems list for the project (and at the top of the pom.xml GUI editor) it says:
Unable to build project
'/export/people/clegg/data/Workspace/funcserve/pom.xml;
it requires Maven version 2.2.1
This persists whether I set Eclipse to use its Embedded Maven implementation, or the external 2.2.1 installation, in the Preferences -> Maven -> Installations dialog.
I've tried closing and reopening the project, reindexing the repository, cleaning the project, restarting the IDE, logging out and back in again, everything I can think of! But Eclipse still won't believe I have Maven 2.2.1.
I just did a plugin update so I have the latest version of Maven Integration for Eclipse -- 0.9.8.200905041414.
Does anyone know how to convince Eclipse I really do have the right version of Maven? It's like it's recorded the previous version somewhere else and won't pay any attention to my changes :-(
Eclipse allows you to specify an external Maven installation. But there's a catch. :(
Have a look at Windows > Preferences > Maven > Installations.
You will see a message like this:
Note: Embedded runtime is always used for dependency resolution, but does
not use global settings when it is used to launch Maven.
To learn more, visit the maven web page.
Translating to English, it means that Eclipse will continue to employ its internal Maven instance in order to perform some tasks.
When you "Run As > maven install" it will run your pom.xml script employing the external Maven instance you specified but when Eclipse performs some of its internal stuff, it will continue to use the embedded Maven instance, whatever version it is.
(edited)
As far as I know, when you install M2Eclipse you are implicitly defining which "internal Maven instance" you will have, which is exactly that one packaged by the plugin.
In the plugin configuration, you can add external Maven instances by telling where they are installed.
If you are using a recent m2eclipse version, you can try this too:
<prerequisites>
<maven>>=2.2.1</maven>
</prerequisites>
Notice the greater than in >=2.2.1. It works fine for me.
A common source of trouble (of this kind) is if you change global preferences and don't remember or know that you have enabled some project specific settings. At least it's a chance that the global setting is now set to use the external maven but the project setting is still set to 'embedded'.
M2Eclipse uses an embedded maven instance, not the maven instance you have installed on your system.
Have you tried switching workspaces?
You can still reference the same project in the new workspace. This would provide about as much of a reset as you can do in terms of eclipses internal settings.
Try creating your eclipse project with mvn project:m2eclipse or use the maven import option under import project.
Using m2eclipse, my fallback in these situations is to do Maven > Update Dependencies and then Maven > Update Project Configuration. The first is just me being supersticious, but the second will rewrite .project and .classpath.
As Pascal says about, m2eclipse can also be pointed at an external instance. I've done this in the past, though not at 2.2.1.
Dan
I got the same issue. Resolved it by restarting the system because every time you change the 'Path' in environment variables you should restart the system or else if would not pick the changes.

Categories

Resources