2

I'm trying to convert a multi-module project to wildfly-swarm. However I've no idea how to set it up properly.

When I run mvn wildfly-swarm:run from core directory, it starts the server and an error says it doesn't find my persistence unit. persistence.xml is in the DBlayer module so it makes sense.

I tried to run it from the root directory but it says the plugin swarm is not defined. So I tried defining the plugin in the root pom.xml instead of core module pom.xml, but the -swarm.jar cannot be created.

So how am I supposed to set it up?

This is my root 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>

Then I added to 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>

core 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>

I didn't specify any main method though. I just have a persistence.xml in DBLayer under :

DBLayer/src/main/resources/META-INF

So I'm kinda assuming that swarm takes care automatically to define the driver and datasource. What I mean is that in wildfly full you have to define the driver and the datasource in standalone.xml. Here I did nothing of that, so maybe that's actually the issue. However in their example they don't do it either. https://github.com/wildfly-swarm/wildfly-swarm-examples/blob/master/jpa/jpa-war/src/main/resources/META-INF/persistence.xml

Here is my persistence unit:

<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>
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Ced
  • 15,847
  • 14
  • 87
  • 146

1 Answers1

1

In a multi-module Maven project, remember to launch Maven commands directly from the root POM.

First of all, keep the wildfly-swarm-plugin declaration inside the WAR module, it would be inappropriate to have it anywhere else, since it applies to a WAR project. Then, you need to install the module DBLayer so that the module core (which is your WAR) can see it (and thus, can see its persistence.xml). Finally, you need to make sure to invoke the goal wildfly-swarm:run only in the WAR module by using the -pl command-line switch.

As such, from the root POM, invoke the command:

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

This will correctly build your multi-module project and run the WAR with the Wildfly Swarm Plugin. You can find a working example of such a set-up on this GitHub repo.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • I didn't read the stacktrace well enough, My bad that didn't do it. but I'm onto something I think – Ced Jul 03 '16 at 19:04
  • @Ced Is the datasource correctly created in `standalone.xml`? Do you have a `faces-config.xml` (I see you're using the JSF dependency)? At least, with a minimal set-up, removing `jta-data-source`, `exclude-unlisted-classes` and `shared-cache-mode` in the `persistence.xml` makes the WAR starts fine. – Tunaki Jul 03 '16 at 19:38
  • No I didn't put any standalone.xml anywhere so my password and user to access mysql is not defined anywhere, neither is my datasource (neither do they in the doc/examples). As far as I understand it's not possible to do with a war file because it uses the default dataSource ExampleDS. So I'm now trying to deploy it as a .jar but I think it fails auto discovery of fractions (jsf and such) so it looks like it needs heavy setup just to define a datasource.. – Ced Jul 03 '16 at 20:59