Royi Freifeld
Royi Freifeld

Reputation: 659

Test data source not being deployed to wildfly

I'm using wildfly maven plugin in order to deploy mysql driver, datasource and *.ear project.
Usually I'm running Wildfly and then: mvn clean install (skipping tests flag is on) and everything runs. Running tests doesn't work for some reason: Wildfly off, then mvn clean test -Denv=test --->
DeploymentException: cannot deploy: test.war, Caused by java.lang.Exception: {"JBAS014771: Services with missing/unavailable dependencies" => ["jboss.persistenceunit.\"test.war#MyProjectTestPU\".__FIRST_PHASE__ is missing [jboss.naming.context.java.jboss.datasources.MyProjectTestDS]"]}

Parent pom

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupid</groupId>
    <artifactId>myProject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>myProject</name>

    <modules>
        <module>myProject-ear</module>
        <module>myProject-ejb</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <!-- JBoss dependency versions -->
        <version.wildfly.maven.plugin>1.0.2.Final</version.wildfly.maven.plugin>

        <version.jboss.bom>8.2.0.Final</version.jboss.bom>
        <version.wildfly>8.2.0.Final</version.wildfly>
        <version.arquillian.container>8.2.0.Final</version.arquillian.container>

        <!-- other plugin versions -->
        <version.compiler.plugin>3.2</version.compiler.plugin>
        <version.ear.plugin>2.9.1</version.ear.plugin>
        <version.ejb.plugin>2.4</version.ejb.plugin>
        <version.war.plugin>2.5</version.war.plugin>
        <version.surefire.plugin>2.18.1</version.surefire.plugin>
        <version.war.plugin>2.1.1</version.war.plugin>
        <version.git.commit>2.1.12</version.git.commit>
        <version.dependency.plugin>2.10</version.dependency.plugin>

        <!-- maven-compiler-plugin -->
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>

        <!-- Libraries version -->
        <testng-version>6.9.4</testng-version>
        <!-- <arquillian-version>1.1.8.Final</arquillian-version> -->
        <arquillian-version>1.1.3.Final</arquillian-version>
        <arquillian-wildfly-version>8.2.0.Final</arquillian-wildfly-version>
        <arquillian-transaction-version>1.0.1.Final</arquillian-transaction-version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Define the version of the EJB jar so that we don't need
                to repeat ourselves in every module -->
            <dependency>
                <groupId>groupid</groupId>
                <artifactId>myProject-ejb</artifactId>
                <version>${project.version}</version>
                <type>ejb</type>
            </dependency>

            <dependency>
                <groupId>org.wildfly.bom</groupId>
                <artifactId>jboss-javaee-7.0-with-tools</artifactId>
                <version>${version.jboss.bom}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.wildfly.bom</groupId>
                <artifactId>jboss-javaee-7.0-with-hibernate</artifactId>
                <version>${version.jboss.bom}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- testing dependencies -->
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>${testng-version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>${arquillian-version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>org.jboss.arquillian.testng</groupId>
                <artifactId>arquillian-testng-container</artifactId>
                <version>${arquillian-version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.jboss.arquillian.extension</groupId>
                <artifactId>arquillian-transaction-bom</artifactId>
                <version>${arquillian-transaction-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.wildfly</groupId>
                <artifactId>wildfly-arquillian-container-embedded</artifactId>
                <version>${arquillian-wildfly-version}</version>
            </dependency>
            <dependency>
                <groupId>org.wildfly</groupId>
                <artifactId>wildfly-embedded</artifactId>
                <version>${arquillian-wildfly-version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${version.compiler.plugin}</version>
                    <configuration>
                        <fork>true</fork>
                        <verbose>true</verbose>
                        <source>${maven.compiler.source}</source>
                        <target>${maven.compiler.target}</target>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.wildfly.plugins</groupId>
                    <artifactId>wildfly-maven-plugin</artifactId>
                    <version>${version.wildfly.maven.plugin}</version>
                    <inherited>true</inherited>
                    <configuration>
                        <skip>true</skip>
                        <force>true</force>
                        <jbossHome>${JBOSS_HOME}</jbossHome>
                        <javaHome>${JAVA_HOME}</javaHome>
                        <modulesPath>${JBOSS_HOME}/modules</modulesPath>
                        <username>user</username>
                        <password>pass</password>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>${version.dependency.plugin}</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${version.surefire.plugin}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Ear 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">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>groupid</groupId>
        <artifactId>myProject</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>myProject-ear</artifactId>
    <packaging>ear</packaging>
    <name>myProject EAR module</name>

    <properties>
        <!-- db properties -->
        <jdbc.datasource.start>java:jboss/datasources</jdbc.datasource.start>
        <jdbc.url.start>jdbc:mysql://localhost:3306</jdbc.url.start>

        <datasource.name>MyProjectDS</datasource.name>
        <database.name>MyProject</database.name>
        <deploy.phase>install</deploy.phase>

        <jdbc.datasource.name>${jdbc.datasource.start}/${datasource.name}</jdbc.datasource.name>
        <jdbc.url>${jdbc.url.start}/${database.name}</jdbc.url>

        <version.mysql.connector>5.1.34</version.mysql.connector>
    </properties>

    <dependencies>
        <!-- Project dependencies -->
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>myProject-ejb</artifactId>
            <version>${project.version}</version>
            <type>ejb</type>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>myProject-war</artifactId>
            <version>${project.version}</version>
            <type>war</type>
        </dependency>

        <!-- mysql connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${version.mysql.connector}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>${version.ear.plugin}</version>
                <configuration>
                    <!-- Tell Maven we are using Java EE 7 -->
                    <version>7</version>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>

                    <modules>
                        <ejbModule>
                            <groupId>groupid</groupId>
                            <artifactId>myProject-ejb</artifactId>
                        </ejbModule>
                        <webModule>
                            <groupId>groupid</groupId>
                            <artifactId>myProject-war</artifactId>
                        </webModule>
                    </modules>

                    <fileNameMapping>no-version</fileNameMapping>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <configuration>
                    <filename>${project.artifactId}.ear</filename>
                    <skip>false</skip>
                </configuration>

                <executions>
                    <!-- ON CLEAN -->
                    <execution>
                        <id>undeploy</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>undeploy</goal>
                        </goals>
                        <configuration>
                            <ignoreMissingDeployment>true</ignoreMissingDeployment>
                        </configuration>
                    </execution>

                    <!-- ON DEPLOY -->
                    <execution>
                        <id>deploy-driver</id>
                        <phase>${deploy.phase}</phase>
                        <goals>
                            <goal>deploy-artifact</goal>
                        </goals>
                        <configuration>
                            <name>mysql</name>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>${version.mysql.connector}</version>
                        </configuration>
                    </execution>

                    <execution>
                        <id>add-datasource</id>
                        <phase>${deploy.phase}</phase>
                        <goals>
                            <goal>add-resource</goal>
                        </goals>
                        <configuration>
                            <address>subsystem=datasources,data-source=${jdbc.datasource.name}</address>
                            <resources>
                                <resource>
                                    <properties>
                                        <connection-url>${jdbc.url}</connection-url>
                                        <jndi-name>${jdbc.datasource.name}</jndi-name>
                                        <enabled>true</enabled>
                                        <enable>true</enable>
                                        <driver-name>mysql_com.mysql.jdbc.Driver_5_1</driver-name>
                                        <driver-class>com.mysql.jdbc.Driver</driver-class>
                                        <user-name>user</user-name>
                                        <password>111111</password>
                                        <pool-name>${datasource.name}</pool-name>
                                    </properties>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>

                    <execution>
                        <id>deploy</id>
                        <phase>${deploy.phase}</phase>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>test</id>
            <activation>
                <property>
                    <name>env</name>
                    <value>test</value>
                </property>
            </activation>
            <properties>
                <datasource.name>MyProjectTestDS</datasource.name>
                <database.name>MyProjectTest</database.name>
                <deploy.phase>test</deploy.phase>
            </properties>
        </profile>
    </profiles>
</project>

Since the env=test value is passed, the test profile should kick in, resulting in a different value for the properties. Nothing seems special when running mvn help:effective-pom and the properties do have a different value.

Upvotes: 0

Views: 1139

Answers (1)

Guillermo
Guillermo

Reputation: 1533

I'm guessing that the test you run with arquillian are into the ejb or war module (so for Maven those test correspond/belong/test its module). Then the test.war is deployed at test phase of the modules but the ear's test phase is executed after that (Maven resolves that way, you ear depends on its modules). So there isn't any datasources when the module's tests are executed.

You could prepare the arquillian's deployment to include the resources that the test needs. Here is an example of publishing the datasource descriptor named jbossas-ds.xml within the test.war.

Upvotes: 1

Related Questions