测试数据源未部署到wildfly

时间:2015-05-23 13:16:48

标签: maven wildfly jboss-arquillian

我使用wildfly maven插件来部署mysql驱动程序,数据源和* .ear项目。
通常我正在运行Wildfly然后:mvn clean install(跳过测试标志已启用)并且一切都在运行。由于某些原因,运行测试无效:Wildfly关闭,然后mvn clean test -Denv=test --->
DeploymentException: cannot deploy: test.war

引起的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]"]}

家长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>

耳朵

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

由于传递了env=test值,test配置文件应该启动,从而导致属性的值不同。运行mvn help:effective-pom并且属性具有不同的值时,似乎没什么特别的。

1 个答案:

答案 0 :(得分:1)

我猜你用arquillian运行的测试是进入ejb或war模块的(因此对于Maven那些测试对应/属于/测试它的模块)。然后在模块的测试阶段部署 test.war ,但之后执行ear的测试阶段(Maven以这种方式解析,你的耳朵取决于它的模块)。 因此,在执行模块测试时没有任何数据源。

您可以准备arquillian的部署以包含测试所需的资源。 Here是在test.war中发布名为 jbossas-ds.xml 的数据源描述符的示例。