【问题标题】:Intellij IDEA includes other module that is already included by MavenIntellij IDEA 包含 Maven 已包含的其他模块
【发布时间】:2013-08-06 22:53:33
【问题描述】:

我有一个基于 Maven 的 2 模块 Intellij 项目,其组织方式如下:

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

我可以使用 Maven 构建项目,生成的 WAR 包含 MyAppDomain.JAR 作为依赖项:

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

此 WAR 在 Tomcat 中有效。但是,如果我使用 Intellij Idea Ultimate 的整洁 Tomcat 集成构建它,WAR 会失败并出现此错误(为便于阅读而格式化):

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

因此,IDEA 似乎包含了 Maven 指定的 JAR (MyAppDomain-1.0.0.jar),还包含了它自己的包含所有相同文件的 JAR (MyAppDomain.jar) 版本。

我在 Intellij IDEA 中寻找关闭此功能的方法是徒劳的 - 有人知道如何阻止这种行为(包括共同驻留项目)吗?

【问题讨论】:

  • 你在war文件中检查过,存在两个名称不同的MyAppDomain jars ??
  • 是的,他们都在那里。请参阅下面的解决方法。

标签: java maven tomcat intellij-idea


【解决方案1】:

我无法找到关闭此行为的方法,但我找到了正确构建 WAR 的解决方法。

  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.

通过这样做,Intellij 将使用您设置的 Maven 构建任务来创建您的 WAR,而不是使用它自己的“Make”命令。

【讨论】:

    【解决方案2】:

    我在 IDEA 中遇到了同样的问题。

    出现这个问题是因为你在启动 Tomcat 之前在 IDEA 中运行了mvn clean package(or install)

    mvn clean package 将生成 Maven 指定的 JAR (MyAppDomain-1.0.0.jar),但启动 Tomcat 将生成没有指定版本的 JAR (MyAppDomain.jar)。这就是你的类路径中有两个相同的 jar 的问题。

    要解决此问题,如果您曾经运行过mvn clean package,则只需在启动 Tomcat 之前在您的 IDEA 中手动运行 mvn clean

    【讨论】:

      【解决方案3】:

      我也遇到了这个问题。生成的战争包含两个版本的相同依赖项(这是一个模块),这会导致问题。

      我必须在 IntelliJ 中编辑我的 tomcat 启动配置:

      1. 编辑启动配置
      2. 单击您的 tomcat 配置,然后转到 部署 选项卡
      3. 在窗口底部,单击加号图标添加“启动前”任务
      4. 选择运行 Maven 目标
      5. 将“clean”写为命令行并接受
      6. 在 Build 之前将任务移到顶部

      【讨论】:

        猜你喜欢
        • 2014-02-10
        • 2016-03-29
        • 1970-01-01
        • 2018-02-09
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多