【问题标题】:Multi module Maven project with wildfly-swarm-plugin带有 wildfly-swarm-plugin 的多模块 Maven 项目
【发布时间】:2016-07-03 17:10:08
【问题描述】:

我正在尝试将一个多模块项目转换为 wildfly-swarm。但是我不知道如何正确设置它。

当我从核心目录运行mvn wildfly-swarm:run 时,它会启动服务器,并出现错误提示它找不到我的持久性单元。 persistence.xmlDBlayer 模块中,所以有意义。

我试图从根目录运行它,但它说插件群未定义。所以我尝试在root pom.xml 中定义插件而不是core 模块pom.xml,但是无法创建-swarm.jar

那我应该怎么设置呢?

这是我的根 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>xxx</groupId>
    <artifactId>x</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <version.wildfly.swarm>1.0.0.Final</version.wildfly.swarm>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.wildfly.swarm</groupId>
                <artifactId>bom</artifactId>
                <version>${version.wildfly.swarm}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>backgroundServices</module>
        <module>core</module>
        <module>DBLayer</module>
        <module>userArea</module>
    </modules>

    <dependencies>    
        <!-- Java EE 7 dependency -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- Wildfly Swarm Fractions -->
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>cdi</artifactId>
        </dependency>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>jpa-mysql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>jsf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>keycloak</artifactId>
        </dependency>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>logging</artifactId>
        </dependency>
    </dependencies>
</project>

然后我添加到core.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>wildfly-swarm-plugin</artifactId>
            <version>${version.wildfly.swarm}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>package</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

核心pom:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>xxx</groupId>
        <artifactId>x</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>core</artifactId>

    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
                <plugin>
                    <groupId>org.wildfly.swarm</groupId>
                    <artifactId>wildfly-swarm-plugin</artifactId>
                    <version>${version.wildfly.swarm}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>package</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
        </plugins>
    </build>


    <dependencies>
        <dependency>
            <groupId>xxx</groupId>
            <artifactId>DBLayer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>xxx</groupId>
            <artifactId>backgroundServices</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

DBlayer pom:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>xxx</groupId>
        <artifactId>x</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <artifactId>DBLayer</artifactId>
</project>

虽然我没有指定任何主要方法。我在 DBLayer 下只有一个 persistence.xml:

DBLayer/src/main/resources/META-INF

所以我有点假设 swarm 会自动定义驱动程序和数据源。我的意思是,在wildfly full 中,您必须在standalone.xml 中定义驱动程序和数据源。在这里我什么也没做,所以也许这实际上是问题所在。然而,在他们的例子中,他们也没有这样做。 https://github.com/wildfly-swarm/wildfly-swarm-examples/blob/master/jpa/jpa-war/src/main/resources/META-INF/persistence.xml

这是我的持久化单元:

<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="my-pu" transaction-type="JTA">
        <jta-data-source>java:/walbangDbDS</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>

        <properties>
            <!--<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>-->
            <!--<property name="javax.persistence.sql-load-script-source" value="sql_scripts/loading_script.sql"/>-->
            <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

【问题讨论】:

    标签: maven jboss wildfly wildfly-swarm


    【解决方案1】:

    在多模块 Maven 项目中,请记住直接从根 POM 启动 Maven 命令。

    首先,将wildfly-swarm-plugin 声明保留在WAR 模块中,将它放在其他任何地方都不合适,因为它适用于WAR 项目。然后,您需要安装模块DBLayer,以便模块core(这是您的WAR)可以看到它(因此,可以看到它的persistence.xml)。最后,您需要确保使用-pl 命令行开关仅在WAR 模块中调用目标wildfly-swarm:run

    因此,从根 POM 调用命令:

    mvn clean install && mvn -pl core wildfly-swarm:run
    

    这将正确构建您的多模块项目并使用 Wildfly Swarm 插件运行 WAR。您可以找到此类设置的工作示例on this GitHub repo

    【讨论】:

    • 我没有很好地阅读堆栈跟踪,我的错误是没有这样做。但我正在做一些我认为的事情
    • @Ced 数据源是否在standalone.xml 中正确创建?你有faces-config.xml(我看到你正在使用 JSF 依赖项)?至少,通过最小的设置,删除persistence.xml 中的jta-data-sourceexclude-unlisted-classesshared-cache-mode 可以使WAR 正常启动。
    • 不,我没有在任何地方放置任何standalone.xml,所以我的密码和访问mysql的用户没有在任何地方定义,我的数据源也没有定义(文档/示例中也没有)。据我了解,无法使用war文件,因为它使用默认的dataSource ExampleDS。所以我现在尝试将它部署为 .jar,但我认为它无法自动发现分数(jsf 等),因此看起来它需要大量设置才能定义数据源..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 2014-07-01
    相关资源
    最近更新 更多