Making Eclipse play well with JPMS and Maven - java

Given a Maven project created in Eclipse from archetype maven-archetype-quickstart ( with junit upgraded to 5.5.2, which has JPMS support) , how can it be modularized to a JPMS project and still behave well in Eclipse?
When I tried this, I first created the module folder in the java folder and moved the package structure into that. The project structure then looks like this:
jpms (project folder) -> src/main/java/org.myorg.mymodule/org/myorg/myproject/jpmspackage
. So far so good. The project builds with mvn clean install in console and the unit test is executed. Then the module folder (containing the module-info.java file + packages) was set to source folder ("use as source folder" in Eclipse).
Performing a Maven build in Eclipse now fails with
Cannot nest'jpms/src/main/java/org.myorg.mymodule' inside 'jpms/src/main/java'. To enable the nesting exclude 'org.myorg.mymodule/' from 'jpms/src/main/java'
How can the source folder or build path be set to appease Eclipse?
Module descriptor:
module org.myorg.mymodule {
requires org.junit.jupiter.api;
}
Updated JUnit dependency (the project was originally generated with JUnit 3-something)
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
</dependency>
Versions: Eclipse 2019-09, Java 12, Maven 3.6.2

Keep the project structure, just add the module-info.java file into the default package:
Right-click the project folder and choose Configure > Create module-info.java
Please note, that for each module a separate project is required as it does not make sense to have multiple modules (which have different dependencies) in one project (if required, use nested projects instead).
See also this Java modules Maven example.

Related

Pomless Tycho build in Eclipse with inherited Maven dependencies

I'm currently working on creating a Maven-Tycho build environment for a relatively large RCP project. The Maven build is already completing successfully, but I'm having trouble removing all the errors from the projects when opening all the sub-projects in the Eclipse IDE.
One of the problems that is most common is, that a MANIFEST.MF contains a Require-Bundle line that cannot be found. This is mostly because I'm using the tycho-pomless extension to reduce the number of pom files I need to maintain.
So the error that happens is, that it cannot find a dependency in the Manifest, because the project should inherit it from the parent-pom. Because it is a pomless build though, the temporary pom for the project is only existing while building the project with tycho-maven. This means that eclipse does not seem aware of the inheritance of the dependency.
I've created a minimal example HERE.
If you open this workspace with an Eclipse IDE you can see, that there are errors and the following lines in App.java have errors:
import com.github.oxo42.stateless4j.conversion.ParameterConversion;
ParameterConversion.validate(null,null);
The required dependency is in the parent pom in the root directory of the workspace:
<dependency>
<groupId>com.github.oxo42</groupId>
<artifactId>stateless4j</artifactId>
<version>2.5.0</version>
</dependency>
The dependency is also in the Manifest of module 1, but it cannot be found in the Eclipse IDE.
If you try to build the project with Maven from a shell, you will notice that the dependency inheritance is honored, even without test.module1 having a pom file and the build succeeds.
So the current state is: CLI Maven build is working, but Eclipse IDE cannot resolve the dependencies correctly making development a bit of a mess.
Is there a way around this, or will I just have to create a pom file for those specific modules?

Maven Eclipse plugin, WAR overlays, and dependencies on project vs. Maven repo

I have a Maven web project that depends on other projects that build WAR targets, and then uses the other web projects as overlays. All of these projects are part of a single parent project, imported into Eclipse as a Maven project.
The main web project's POM has dependencies and overlays with maven-war-plugin defined for the other modules.
My problem
in Eclipse, for some reason, when I update the Eclipse project from the Maven project, some of the modules are recognized as coming from other Eclipse projects, and others are expected to come from my Maven repository.
On closer inspection org.eclipse.wst.common.component shows a difference in the dependent-module for some modules.
<dependent-module ... handle="module:/overlay/prj/module1" />
vs.
<dependent-module ... handle="module:/overlay/var/M2_REPO/group/id/module2" />
Why would some modules be recognized differently than others?
Both are equally included as dependencies and overlays in the POM.
My workaround has been to edit this file by hand to get Eclipse to recognize that it should use the other Eclipse project directly (module:/overlay/prj) rather than look to the Maven repo.

Eclipse Maven: How to build the added dependency first from the project itself?

I am using Eclipse with m2e plugin.
The steps I followed are as follows:
1. Created a sample Maven project
2. Imported an another Maven project in same workspace
3. Used imported project as dependency in my sample project
Now the problem is that since the imported project is not built and does not have target jar file, the sample project is showing error(jar not found).
So the question is it possible to run the build from sample project, which will build the dependency first, which in turn will be used in sample project build?
Run sample project build -> Build dependency -> Create dependency jar -> Dependency jar used in sample project build further
If this is possible, then how to do the same?
Thank you for any suggestion/feedback. :-)
m2e has a feature named Workspace Resolution that can be enabled/disabled for every project (right click on the project in the project explorer, then Maven). This allows you to directly use the classes of another project that you declared as dependency.
Note that is only works if your dependency (in your POM) and the other project have the same coordinates, including version number.

Get jars defined in pom.xml

I have received a J2EE project which has some servlets defined.
I have imported this project in Eclipse [Version: Mars Release (4.5.0)] as a Java project.
This project has a pom.xml defined which contains a list of dependencies. How can pull the dependencies defined in pom.xml.
When I open pom.xml it opens as a raw xml.
Do I need to import this project as a Maven project?
If you have the m2e plugin (http://download.eclipse.org/technology/m2e/releases) installed into eclipse , you can import the project as a maven project and then run the dependencies by running as a maven build. Guess that will be the easiest to do.

Integrating a maven project into Eclipse

I have a maven module for validation which I must pass to a old version of Eclipse which has the Jrules API within. However there is not a maven plugin for this eclipse IDE. So I figured I would do a maven:install on the module and move over the created jar.
However when I try to import->Existing Projects into Workspace->Select archive file:
and point it to the jar no projects appear. I'm at a loss as to how I can move my maven module to the outdated eclipse, without having to grab the 101 jars required for the project and non mavenise it...
Surely their has to be an easy way to this or is maven will monolithic
Use the maven-eclipse-plugin to generate the .project and .classpath files for you:
mvn eclipse:eclipse
This will create the IDE metadata files which reference all of the JARs your project depends on from within your local maven repository folder.
Attempting to import the JAR that is built by the Maven build process into Eclipse using the " import->Existing Projects into Workspace->Select archive file" doesn't work because Eclipse expects to find a .zip/.jar file with the .project metadata files and the source code. Your compiled JAR likely contains neither.
I would recommend using the M2Eclipse maven plugin. Right click the project -> Enable Dependency Management -> Update Project Configuration
I have used eclipse:eclipse extensively and my experience is that M2Eclipse is not only better supported but works better overall.

Categories

Resources