user1071914
user1071914

Reputation: 3393

Intellij IDEA includes other module that is already included by Maven

I've got a 2-module Intellij project that is Maven-based and is organized like this:

MyAppProject
    --->MyAppDomain (builds a JAR)
    --->MyAppWAR (builds a WAR, including the JAR above)

I can build the project using Maven, and the resulting WAR contains MyAppDomain.JAR as a dependency:

<dependency>
    <groupId>com.mycompany.myapp</groupId>
    <artifactId>MyAppDomain</artifactId>
    <version>1.0.0</version>
</dependency>

This WAR works in Tomcat. However, if I build it using Intellij Idea Ultimate's neat Tomcat integration, WAR fails with this error (formatted for readability):

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0': 
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/config/data-context.xml]: 
Invocation of init method failed; nested exception is java.lang.IllegalStateException: 
Conflicting persistence unit definitions for name 'caPersistenceUnit': 
file:/C:/JavaSoftware/apache-tomcat-6.0.32/webapps/MyApplication/WEB-INF/lib/MyAppDomain.jar, file:/C:/JavaSoftware/apache-tomcat-6.0.32/webapps/MyApplication/WEB-INF/lib/MyAppDomain-1.0.0.jar

So, it appears that IDEA is including the Maven-specified JAR (MyAppDomain-1.0.0.jar) and also including it's own version of this JAR (MyAppDomain.jar) containing all the same files.

I have searched in vain for a way to turn this off in Intellij IDEA - does anybody know how to stop this behavior (including co-resident projects)?

Upvotes: 1

Views: 3205

Answers (3)

Salvatorelab
Salvatorelab

Reputation: 11873

I just faced this issue too. The generated war contained two versions of the same dependency (which is a module), and that was causing problems.

I had to edit my tomcat launch configuration in IntelliJ:

  1. Edit launch configurations
  2. Click on your tomcat configuration and then go to Deployment tab
  3. On the bottom of the window, add a "Before launch" task clicking on the plus icon
  4. Select Run Maven Goal
  5. Write "clean" as command line and accept maven goal clean
  6. Move the task to the top, before Build

Upvotes: 1

kaka
kaka

Reputation: 41

I was encountered the same problem in IDEA.

This problem was occured because you have ran mvn clean package(or install) in IDEA before you start Tomcat.

The mvn clean package will generates Maven-specified JAR (MyAppDomain-1.0.0.jar), but start Tomcat will generates JAR without version spefified (MyAppDomain.jar).That is the problem you have the two same jars in your classpath.

To solve this problem,just manually run mvn clean in your IDEA before you start Tomcat if you have ran mvn clean package ever.

Upvotes: 1

user1071914
user1071914

Reputation: 3393

I was not able to find out how to turn this behavior OFF, but I found a workaround that builds the WAR correctly.

  1. Select "Run -> Edit Configurations" from the menu. 
  2. Select your"Tomcat Server" job. 
  3. Click the "Deployment" tab. 
  4. At the bottom, where it says "Before Launch:  Another Configuration,"  delete the "Make" option. 
  5. Click the "+" (plus) sign and select your Maven Build task to add it to the "Before Launch" list.

By doing this, Intellij will use the Maven build task you have set up to create your WAR, instead of using its own "Make" command.

Upvotes: 1

Related Questions