Maven EAR multimodule project not packaging persis

2019-09-08 15:53发布

问题:

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:

Parent pom project and modules

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.

Configuration files in modules

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:

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

Here are my pom files:

pom.xml[sigea-app] (parent project)

<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] 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.

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?

Thanks in advance for your answers.

回答1:

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>

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>

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.