IntelliJ Community with 2 maven projects in same window - java

I have a maven project project-a and project-b wo depends on project-a.
How can I work with this 2 projects in same IntelliJ window?
I want to be able to make changes on project-a and reflect them to project-b without deploy/install the project-a. I already do this on Eclipse using m2e plugin.
EDITIED
After setting the Build, Execution, Deployment | Build Tools | Maven | Importing >> Import Maven projects automatically the IDE was able to build part of my projects.
Now I'm facing another problem. This project-a has a dependency tohibernate-entitymanager who has a dependency to hibernate-core who has a dependency tohibernate-jpa-2.1-api.
My project uses some classes from this hibernate-jpa-2.1-api jar, but seems that the IntelliJ can not resolve sub-dependencies, forcing me to redeclare this dependency (hibernate-jpa-2.1-api) on theproject-a / pom.xml. Is this the expected behavior?

You should add you maven projects as modules to Intellij IDEA as already said CrazyCoder.
And then do 'Add As Maven Project' by right click on the every module's pom.xml in the Project tab.
This way you will be able to use cross-module searches, refactorings etc. Compilation will also be performed against the added Maven Projects without a need to install artifacts into the repository.

Related

IntelliJ - Resolve Maven Artifacts

With IntelliJ IDEA, is it possible to "Resolve Workspace Artifacts"?
Meaning say I have a project
- root_folder
-->module1
---->pom.xml
-->module2
---->pom.xml
-->module3
---->pom.xml
-->project1
---->pom.xml
-->project2
---->pom.xml
-->project3
---->pom.xml
This is an Eclipse project that I opened in IntelliJ and with Eclipse when I run jetty:run on any of the 3 projects it can resolve the modules that is it dependent on because of the "Resolve Workspace Artifacts" of the Eclipse IDE, however for IntelliJ this is a bit of a challenge.
There is no parent POM, each module are quite stand-alone maven projects.
Using IntelliJ when I run project1 which depends on module1; it is unable to resolve module1 unless I mvn install the module1 which is very cumbersome, I have to mvn install for every changes in the module. Not really productive at all.
Additionally, as mentioned the project is actually an forked Eclipse-based project that is why I cannot modify the maven structure to work-around the project, the only option I have is a solution at IDE level for the projects to be able to do something similar to Resolve Workspace Artifacts.
Similar to m2Eclipse plugin: What does 'Resolve Workspace Artifacts' do?

maven-assembly-plugin and eclipse Workspace resolution

I have a project, which uses the maven-assembly-plugin to compile one JAR out of a number of projects.
I want to run the build with eclipse (Neon 4.6.3). When I run the build, I select the option Resolve Workspace Artifacts.
Resolving workspace artifacts seems to work fine for compiling the classes and testing. However, when it comes to executing the assembly into a JAR, maven does not use the projects from the eclipse workspace but only projects in the local maven repository.
Is there a way to make the maven-assembly-plugin also resolve the projects in the eclipse workspace?
maven-assembly-plugin does resolve workspace artifacts.
I just had configuration issues in the workspace. Specifically, not ALL dependencies for the project were available in the workspace. After I had assured, that ALL dependencies can be resolved from the workspace, the maven-assembly-plugin collected the correct versions.
(You can check this by extending the Maven Dependencies under the project and assert that no *.jar dependencies are left to things which should be resolved from the eclipse projects).
For any maven plugin errors in eclipse IDE, right click on project then select Maven -> Update Project -> select "Force Update of Snapshots/Releases" -> Ok.

How to use Maven with EAR

If i want to convert an EAR project a maven project , do i need to add the module in the deployment assembly as maven dependency or just use the convert in m2eclipse without any further configuration.
Me personally I wouldn't attempt any kind of conversion of an existing project. I would add the poms, make sure that mvn clean install works on the command prompt and then create a new mavenized Eclipse project from the poms.
The main reason is that you current project settings are effectively wrong when you switch to Maven - the Maven poms are the truth and what feeds the Eclipse project setup, so you really do not want to make your life difficult and work against m2eclipse - let it do the project creation for you. Fresh.
You can install m2eclipse and then do the following as well.
Go to the project menu (right click on Package Explorer) > Configure > Convert to Maven Project
Open the pom.xml and right-click and choose Run As -> Maven Clean. Similarly Choose Run As -> Maven Install.
Note : Please ensure that your eclipse project settings are correct and classpath libraries are not absolute and you don't have any project specific environment variables defined in your workspace. Please take a backup of your project before you do this.This is to ensure we don't mess up the current stable project configurations. Once m2eclipse generates the pom.xml for your project, you can update and make changes to it to
fully obtain a mavenized ear build. hope this helps
You can also try creating new maven project with archetype selection of "jboss-javaee6-ear" and follow the similar structure for your project. Most probably you will need parent Pom and child poms per each module (ejb, war, jar etc). There are other few similar approach but almost all of them requires you to have mulitple POMs
maven-ear-plugin and JBoss AS 7
You can also go through all the examples for maven ear plugin to find settings suitable for you
http://maven.apache.org/plugins/maven-ear-plugin/
I ended up ditching ear for war :) single POM and even ditched the JBOss for tomcat/jetty :)
If you want to convert your existing eclipse dependencies into Maven dependencies, you can try the JBoss Tools (JBT) Maven integration feature, which contains an experimental conversion wizard, plugged into m2e's conversion process : http://docs.jboss.org/tools/whatsnew/maven/maven-news-4.0.0.Beta1.html.
So, all you have to do is, as Keerthi explained, right-click on your project and Configure > Convert to Maven...
If your dependencies already are maven artifacts, it should work easily. If not, you'll need to convert them to Maven (if they're workspace projects) or make them available in your maven enterprise repository, before converting the EAR project.
JBT (requires Eclipse JavaEE) can be installed from http://download.jboss.org/jbosstools/updates/stable/kepler/ or from the Eclipse Marketplace (See https://marketplace.eclipse.org/search/site/jboss%2520tools)

How do I manually add a project to the build path of another project?

I have a maven project which won't compile due to an unresolvable reference from another project's artifact.
When I run eclipse:eclipse and open it up it shows an auto fix suggestion of "Add project 'project2' to build path of 'project1'". If I click this everything works. So project1 can clearly see my project2 reference, but doesn't quite use it as expected.
However, once I delete all the files generated for eclipse the error resumes because whatever reference eclipse created has been removed. How can I get this project2 in the project1 build path manually. I already have it listed in the pom as below:
<dependency>
<groupId>group.id</groupId>
<artifactId>project2</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
In "Project properties > Maven" menu you should check "Resolve dependencies from Workspace projects".
When I run eclipse:eclipse[...]
This sounds like you are using the Maven Eclipse plugin. You should probably rather use m2e, which is the recommended way to integrate with Maven now. This will also obviate the need to run mvn eclipse:eclipse. Instead, just import your Maven project into Eclipse, and it will pick up everything automatically.
After you set "Resolve dependencies from Workspace projects" as described in polypiel's answer, things should just work (TM).
If you have maven nature to both your projects, you should do mvn clean install to both the projects, so they will be installed in your local repository and will be available for other projects to use as dependencies. Or just right click to both the projects in eclipse and choose Run As --> Maven install

Maven Update Project Configuration issue

Whenever I perform a Maven Update Project Configuration, I loose ay of the projects I have listed in eclipse under the Java Build Path/Projects. Is there a plugin or something that I need to add to the pom.xml to make it reconfigure that whenever I update the project configuration?
Thanks
When using Maven-based dependency management, you are not supposed to have any projects under Java Build Path/Projects. Instead, those projects should be Maven projects with Maven nature enabled. Then all you need to do is to have them declared as dependencies of your project and enable option to resolve Maven dependencies from Eclipse workspace (it is enabled by default).

Categories

Resources