Use IntelliJ Idea module classpath when running Maven goal from IntelliJ Idea - java

Lets say I have two maven projects: moduleA and moduleB.
moduleA has a dependency to moduleB in it's pom.xml.
I use IntelliJ Idea, and I have 'Maven Projects' tool window, where I have both these maven projects.
And it is possible to run maven plugin goals from there: I open 'Maven Projects' -> moduleA -> Plugins -> jetty -> jetty:run.
Java is run, moduleB is in classpath, but it is in the Maven local repository!
So that, if I change something in moduleB sources and execute jetty:run from Idea, I will not see code changes until moduleB is installed with mvn install.
Is there any solution in Idea to deal without mvn install, for instance to run somehow maven goals with idea-module classpath?

I suggest to use Jetty Integration IDEA plugin to run standalone jetty
http://github.com/codebrewer/intellij-idea-jetty-integration-plugin
if you still want to use jetty:run, you could tell IDEA to do mvn install for moduleB:
Run -> Edit Configurations -> select youy maven run config -> Configuration tab -> Before launch (at the bottom) -> Run Maven Goal

That IDEA plugin is not developed anymore because IntelliJ now includes its own Jetty runconfig thing.
Here's what worked for me with a Maven multi-module project:
In File, Project Structure, Modules I have all modules compile to the same output path ("Use module compile output path"). I chose the "...WEB-INF\classes" path within the folder of the Jetty Deployment folder.
Then I also have to tell IDEA to not clear output directories when compiling. Otherwise each module that compiles (one after another) clears the classes from the previous one. Settings, Compiler, uncheck "Clear output directory on rebuild".
Also, I had to put all my external (maven) dependency jars into the "...WEB-INF\lib" folder initially.
Yes, it's hacky, but works. If someone has a better, cleaner solution I'd be interested to know.

Related

Maven how to include a Java project as a dependency

I have the following structure
-->Project1 (Java SE Project)
---->src (My Java Code)
-->Project2 (Maven Project)
---->pom.xml
---->src
------->main
------->test
In Eclipse, I can configure the build path of the maven project to include Project1 and it runs fine in the IDE. However, I realized that it seems when I want to run it as a command
mvn test-compile
the Project2 can't find the project. I am thinking I need to somehow reference it in my pom.xml (which then, I can ignore the step in Eclipse to configure it on the build path).
The error is
[ERROR] package foo does not exist
The Project1 is not really being compiled into a .jar so that isn't the solution. I want the actual entire project as a reference somehow.
Instead what I did was just convert the project1 to a maven project and in project2, added the dependency without needing to generate the jar. Seems like maven is smart enough to find the project and not look into the .m2 version (like if I did a mvn install).

How do I properly organize jars into one folder in Eclipse?

I used Maven to generate a Jersey project from command line, and I also used mvn eclipse:eclipse so that I can import my project to eclipse.
After I opened the project from Eclipse I see all the jars were in the root folder, how do I create a folder or package so that I can move all these jars into it?
Don't use mvn eclipse:eclipse.
Just do File -> Import -> Maven -> Existing Maven projects and browse the folder where your pom.xml is and Eclipse will organize everything for you.
It looks like you aren't using Maven eclipse integration - m2eclipse. You shouldn't have to install it, it comes already with more recent versions of Eclipse, though follow the instructions in that link if you don't yet have it.
If you just generated the project, try deleting it and then generating it again inside eclipse with New -> Other -> Maven Project. If you have a lot of code written already, instead right click on the project and do Configure -> Convert to Maven Project. Once you've done that, you may have to do Maven -> Update Project project to straighten out your jars, and possibly remove them manually with Build Path -> Configure Build Path.
However, I recommend creating the project from within Eclipse, if possible.
Addendum: #cahen's answer is also correct; don't use mvn eclipse:eclipse.

In eclipse, maven dependencies are overriding project dependencies

I have a "master project" that uses several "modules". The modules are in the pom.xml, but I also have the code for the modules as projects in Eclipse. I defined the modules as project dependencies both in "Properties|Project References" and in "Properties|Java Build Path". I also made sure all of the Eclipse projects are at the top of "Order and Export" in Java Build Path.
The master project is not recognizing changes to the modules within eclipse. When I press F3 to see the code, it opens java source from my .m2 directory. It's obviously ignoring my Eclipse projects. Am I missing some setting? What should I do?
You don't need to add the dependent projects into the Project references (at least when using m2eclipse-plugin), just add them as dependencies on your masters' pom.xml. I haven't had the need to touch the Build path since I've started using Maven in Eclipse.
Make sure you have enabled the Workspace resolution of the plugin by right clicking on project root -> Maven -> Enable Workspace Resolution. If it says "Disable Workspace Resolution", it is already enabled. You may also need to run "Update project configuration" under the same menu.
This way when you run your master-project from within Eclipse, any changes made to the other projects will be "visible" to the master (and you can actually use hot swap during runtime even on the other projects).
If you're using Maven, you pretty much want to commit to Maven. Even if you do configure Eclipse with Eclipse-specific dependencies, anytime you update your project configuration using Maven, it will do a complete rewrite of your configuration.
I strongly recommend converting any of your other project build dependencies into Maven-enabled projects that can be used as Maven dependencies.
In your specific case (through using the Maven modules), this means ensuring that each of your modules are imported into Eclipse as m2e-enabled projects. m2e will then automatically wire in the dependencies between modules and other dependent projects.
Im scratching my head as to why you would need your modules defined as dependencies on your top level project. Are you trying to avoid having to define the (cross) dependencies at a module level? In any case, as per other suggestions, first of all put your dependencies in the correct places in your POM, then either run
mvn eclipse:clean eclipse:eclipse
to update your Eclipse settings, or better still, make sure m2eclipse is installed and import your maven projects directly into the IDE.

Maven dependencies not visible in WEB-INF/lib

I'm having this weird problem making my project Maven-based. I created a new Maven project in Eclipse and enabled the Dynamic Web facets. But during runtime now it throws ClassNotFoundException because the Maven dependencies are not copied to WEB-INF/lib directory. I even tried adding the copy-dependencies section in POM, but it didn't help.
Someone here seemed to have solved this the right way, but I guess he forgot to mention the solution ::(
This should have nothing to do with eclipse and m2eclipse, and optionally for better support - m2e-wtp. Also, you don't need copy-dependencies. Here are a few possible reasons:
you should invoke mvn package (or right-click > maven > package) and obtain a war file - the <packaging> (in the pom) must be war
your dependencies should be with the default scope (if they are provided or test they will not be included in the archive)
if you are running the project as dynamic web project on a server within eclipse, then you should open the project properties (right click > properties) and select "Deployment Assembly". There click "add", select "build path entries", and choose "maven dependencies". This will instruct WTP to send the maven dependencies to the server dir.
First install Maven Integration For Eclipse WTP (Help->Eclipse Marketplace)
Then in your web project's pom.xml just add:
<packaging>war</packaging>
After that just right click the project, Maven -> Update Project, which should automagically add Maven Dependencies for you in the Deployment Assembly.
Right-click project > Maven > Update Maven Project > OK fixed it for me
Check the Deployment Assembly property of the project. I have seen some mvn eclipse:eclipse generated Eclipse projects sometimes have the assets being deployed to the wrong location. From what I can remember the new Deployment Assembly had some issues with earlier versions of Eclipse 3.6 (Helios).
I just did maven -> update project worked for me.
On Eclipse, select your Web Project -> right click -> Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Maven Dependencies
With this steps, maven libraries will be included on WEB-INF/lib when full publish.
you want to use the copy-dependencies plugin
see this post and the docs
If you look into your deployment and see that WEB-Inf/lib is empty of your Maven dependencies, then ensuring those Maven dependencies are in your deployment assembly is crucial as mention in the chosen answer.
However be wary of Eclipse then not refreshing everything even with a Maven update. If you clean, rebuild and redeploy and still do not see the lib folder being populated try removing and re-adding the application to the server deployments via Servers>Add and Remove.
This unclogged the system for me.
Right-click project -> Deployment Assembly:
See if you have the Maven Dependencies and all others in there.
If not,
Right-Click project -> Maven -> Enable Workspace Resolution
(maybe then Maven -> Update Project, but then look again into "Deployment Assembly" and see what You've got.)
this solved it (finally!) for me.

Getting m2eclipse to match maven's classpath

I've got project with a few sub modules.
mvn clean install
works absolutely fine. But in Eclipse with the parent project open none of the maven dependencies are available and so all my Java source files are full of indicated compilation errors (they're not listed in the build path for the project). If I open one of the sub modules as a project there are no indicated compilation problems.
I've tried running
mvn clean install eclipse:clean eclipse:m2eclipsemvn clean install eclipse:clean eclipse:m2eclipse
and choosing project / update maven dependencies. Refreshing, closing and opening the project and Eclipse, etc.
Any ideas? Thanks in advance.
Getting m2eclipse to match maven's classpath
Just use Import... > Existing Maven Projects and import your multi-modules Maven project, that's all.
But in Eclipse with the parent project open none of the maven dependencies are available and so all my Java source files are full of indicated compilation errors (they're not listed in the build path for the project).
A parent project (i.e. with a pom packaging) is not a Java project, it's just there as a facility to make editing the parent pom.xml from the IDE possible (and that's already nice, importing nested projects inside Eclipse was initially not even possible).
If I open one of the sub modules as a project there are no indicated compilation problems.
That's how you are supposed to do things.
I've tried running (...)
You are not supposed to use the Maven Eclipse Plugin when using m2eclipse and this is not supported. Either use one or the other. If you decide to use m2eclipse, use Import... > Existing Maven Projects as suggested.

Categories