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