【问题标题】:Maven EAR multimodule project not packaging persistence.xmlMaven EAR 多模块项目不打包persistence.xml
【发布时间】:2016-06-10 00:08:08
【问题描述】:

我正在使用 Maven 进行 EAR 项目,该项目有 2 个模块。图像胜于雄辩,让我向您展示结构:

父 pom 项目和模块

sigea-model 包含模型、存储库和服务层(MVC 中的“M”)。 sigea-web 包含网页和控制器 bean (VC),sigea-ear 只是将其他 2 个模块打包到 EAR 包中的包装器。

模块中的配置文件

如您所见,sigea-ear 有一个空的META-INF 文件夹。 sigea-modelsigea-web 中的 beans.xml 文件都只是空标记文件,因为 AFAIK、CDI 默认搜索所有带注释的类(但这不是现在的问题)。 persistence.xml 是一个简单的文件,它使用带有连接池的 JTA 事务(因为我从 Glassfish 的管理控制台 ping 并且成功)。

最后,当我打包应用程序时,我得到以下信息:

如您所见,没有persistence.xml。这一切都是因为我成功部署了应用程序,但在第一次单击时我得到了异常

          javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
          ...
Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null

这是我的pom 文件:

pom.xml[sigea-app](父项目)

<project ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>ar.edu.unt.sigea</groupId>
    <artifactId>sigea-app</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <modules>
        <module>sigea-model</module>
        <module>sigea-web</module>
        <module>sigea-ear</module>
    </modules>

    <build>
        <pluginManagement>
            ...
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <!-- I suppress some lines for brevity -->
        <dependencies>
            <dependency>
                <artifactId>sigea-model</artifactId>
            </dependency>
            <dependency>
                <artifactId>sigea-model</artifactId>
                <type>ejb</type>
            </dependency>
            <dependency>
                <artifactId>sigea-model</artifactId>
                <type>test-jar</type>
                <scope>test</scope>
            </dependency>
            <dependency>
                <artifactId>sigea-web</artifactId>
                <type>war</type>
            </dependency>
            <dependency>
                <artifactId>sigea-web</artifactId>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        ...
    </dependencies>
</project>

pom.xml[sigea-ear]

<project ...>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>ar.edu.unt.sigea</groupId>
        <artifactId>sigea-app</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>sigea-ear</artifactId>
    <packaging>ear</packaging>

    <dependencies>
        <dependency>
            <artifactId>sigea-model</artifactId>
            <type>ejb</type>
        </dependency>
        <dependency>
            <artifactId>sigea-web</artifactId>
            <type>war</type>
        </dependency>
        <dependency>
            <artifactId>sigea-web</artifactId>
            <type>pom</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <configuration>
                    <defaultLibBundleDir>lib/</defaultLibBundleDir>
                    <skinnyWars>true</skinnyWars>
                    <modules>
                        <webModule>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>sigea-web</artifactId>
                            <contextRoot>/sigea</contextRoot>
                        </webModule>
                        <ejbModule>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>sigea-model</artifactId>
                        </ejbModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

pom.xml[sigea-web]

<project ...>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>sigea-app</artifactId>
        <groupId>ar.edu.unt.sigea</groupId>
        <version>1.0</version>
    </parent>

    <groupId>ar.edu.unt.sigea</groupId>
    <artifactId>sigea-web</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>sigea-web</name>

    <dependencies>
        <!-- Some dependencies including sigea-model -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

pom.xml[sigea-model] 并不重要,因为它只是为测试定义了一些依赖项,并被配置为生成带有测试类的包,这些测试类也用于sigea-web 中用于测试目的。

最后一个问题:我的配置中没有打包persistence.xml 文件的问题是什么?如果这不是IllegalStateException 的问题,上面显示的消息:什么该异常的可能原因是什么?

提前感谢您的回答。

【问题讨论】:

  • 首先,如果模块sigea-model 仅包含测试依赖项而不是不包含它作为依赖项。此外,为什么你的segea-web 两次是&lt;type&gt;pom&lt;/type&gt;,另一次是&lt;type&gt;war&lt;/type&gt;。我建议删除 &lt;type&gt;pom&lt;/type&gt;.. 啊顺便说一句:你为​​什么没有定义你的依赖项的 groupId?
  • 也许我不太理解你的第一句话。对于这个项目,我不需要任何外部依赖,而是提供 javaee-api(包含在 Glassfish 中)和 hibernate-validator 来验证实体约束。我在 web 模块中读到 pomwar 类型是 Maven 中的一个技巧,当该模块依赖于同一项目的其他模块时,它会导致 skinnyWar。无论如何,问题是缺少persistence.xml 文件。为了简洁起见,我删除了帖子中的 groupId,在真正的 pom 中,在所有情况下都是&lt;groupId&gt;${project.groupId}&lt;/groupId&gt;
  • 瘦身战争由 skinnyWar:true 激活。这些类型的 pom 等不需要任何技巧。如果它没有按预期工作,那么你做错了。你能做一个示例项目并将其放在 github 上吗...

标签: java maven ear persistence.xml


【解决方案1】:

我通过更改依赖项解决了这个问题。在sigea-model 我有

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.1.0.Final</version>
    <scope>test</scope>
</dependency>

我用它来管理我的测试方法的持久性上下文。我没有t inquire very much in the Glassfish JPA provided implementation but maybe it's Eclipse Link instead of Hibernate. Apparently there are some incompatibility issue between those libraries. I movedhibernate-entitymanager`来编译范围,像这样:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.1.0.Final</version>
</dependency>

现在项目编译没有问题。 khmarbaise的评论也很有用,简化了项目配置,非常感谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2016-07-30
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    相关资源
    最近更新 更多