EAR多模块项目未打包persistence

EAR多模块项目未打包persistence

本文介绍了Maven EAR多模块项目未打包persistence.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有2个模块的Maven在EAR项目中工作.图片胜于雄辩,所以让我向您展示其结构:

I`m working in a EAR project with Maven which has 2 modules. Images speaks louder than words, so let me show you the structure:

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

sigea-model contains model, repository and service layers (The "M" in MVC). sigea-web contains web pages and controller beans (VC) and sigea-ear is just a wrapper to package the other 2 modules in a EAR package.

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

As you can see, sigea-ear has an empty META-INF folder. Both beans.xml files in sigea-model and sigea-web are just empty marker files because AFAIK, CDI by default search in all annotated classes (but this is not the problem right now). persistence.xml is a simple file which uses JTA transactions with a connection pool (which is working because I ping from the Glassfish's admin console and is successful).

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

Finally, when I package the application I get the following:

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

As you can see, there's no persistence.xml. All this came out because I deployed the application successfully but in the first click I got the Exception

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

这是我的pom文件:

<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中也用于测试目的.

pom.xml[sigea-model] is not important as it just defines some dependencies for test and is configured to generate a package with the test classes, which are used in sigea-web for test purposes also.

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

Finally the question: What's failing in my configuration that doesn't package the persistence.xml file? If that's not the problem for the IllegalStateException with the message shown above: What are posible causes for that exception?

预先感谢您的回答.

推荐答案

我通过更改依赖关系解决了该问题.在sigea-model我有

I solved the problem by changing a dependency. In sigea-model I had

<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 moved hibernate-entitymanager`来编译作用域,像这样:

Which I used to manage the persistence context for my test methods. I didnt 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` to compile scope, like this:

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

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

And now the projects compile with no problem. It was also useful the comment made by khmarbaise, it simplified the project configuration, thank you very much.

这篇关于Maven EAR多模块项目未打包persistence.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:17