Create datasource and deploy application

2019-09-01 01:57发布

问题:

I have a maven project which creates a war file and this should be deployed to a bundled wildfly server via wildfly:run.
That works so far, but I need to create a datasource before deploying.

I have tried to bind the add-resource goal to different phases like deploy, install or package. None of them worked.

What is wrong?

An idea would be to use the wildfly:start attach an execution to add the datasource and deploy the application, but I don't know how.

<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<executions>
    <execution>
        <id>add-datasource</id>
        <phase>deploy</phase>
        <goals>
            <goal>add-resource</goal>
        </goals>
        <configuration>
            <address>subsystem=datasources,data-source=java:jboss/testDB</address>
            <resources>
                <resource>
                    <properties>
                        <jndi-name>java:jboss/testDB</jndi-name>
                        <enabled>true</enabled>
                        <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
                        <driver-class>org.h2.Driver</driver-class>
                        <driver-name>h2</driver-name>
                        <user-name>sa</user-name>
                        <password>sa</password>
                    </properties>
                </resource>
            </resources>
        </configuration>
    </execution>
</executions>

回答1:

My solution is to use the run goal and the beforeDeployment goal:

<plugin>
    <groupId>org.wildfly.plugins</groupId>
    <artifactId>wildfly-maven-plugin</artifactId>
    <configuration>
        <beforeDeployment>
            <commands>
                <command>data-source add --jndi-name=java:jboss/datasources/OracleDS --name=testDB --connection-url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 --driver-name=h2 --user-name=sa --password=sa</command>
            </commands>
        </beforeDeployment>
    </configuration>
</plugin>