Configuring Maven WildFly plugin

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

The WildFly Maven plugin buys you the ability to deploy/undeploy your applications on WildFly plus some advanced administration grips. 

In order to use it, here is the build section that should be included in every of your WildFly projects:

<plugin>
	<groupId>org.wildfly.plugins</groupId>
	<artifactId>wildfly-maven-plugin</artifactId>
	<version>1.0.2.Final</version>
	<configuration>
		<filename>${project.build.finalName}.war</filename>
	</configuration>
</plugin>

As you can see, we have set the final artifact name to be the same as the project’s artifactId. This argument will be passed as well to WildFly plugin which is declared in the plugin section. The configuration section contained into WildFly plugin allows some kind of customization such as deploying the application to a remote host or to a domain of servers.

Once that you have configured the WildFly plugin, you can use it to deploy/undeploy or redeploy your applications by passing the appropriate Maven goal. In order to deploy your project, make sure at first that you are in the same directory where your pom.xml is placed and issue:

mvn wildfly:deploy

Deploying to a remote server

Here, for example, how to deploy your application to a remote host named “myhost” on the default port 9990 using the included credentials:

<configuration>
   <hostname>myhost</hostname>
   <port>9990</port>
   <username>administrator</username>
   <password>Password1!</password>
</configuration>

Deploying an artifact

The following example shows how to deploy the MySQL JDBC Driver. It is first defined as a dependency in your pom.xml, then the artifact is referenced in the configuration block:

. . . .
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.29</version>
      </dependency>
. . . .
         <plugin>
            <groupId>org.wildfly.plugins</groupId>
            <artifactId>wildfly-maven-plugin</artifactId>
            <version>1.0.2.Final</version>
            <configuration>
               <groupId>mysql</groupId>
               <artifactId>mysql-connector-java</artifactId>
               <fileName>mysql-connector-java-5.1.29-bin.jar</fileName>
               <executions>
                  <execution>
                     <id>deploy-driver-MySQL</id>
                     <phase>package</phase>
                     <goals>
                        <goal>deploy-artifact</goal>
                     </goals>
                     <configuration />
                  </execution>
               </executions>
               <filename>${project.build.finalName}.war</filename>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

In order to deploy the JDBC driver simply execute:

mvn wildfly:deploy-artifact 

Starting and stopping the application server

The Maven WildFly plugin also allows to start and stop the application server. In the following configuration example we show how to customize the startup of the server by including a configuration file and JVM arguments:

<plugin>
	<groupId>org.wildfly.plugins</groupId>
	<artifactId>wildfly-maven-plugin</artifactId>
	<version>1.0.2.Final</version>
	<configuration>
		<serverConfig>standalone.xml</serverConfig>
		<jvmArgs>-Djboss.socket.binding.port-offset=100</jvmArgs>
		<filename>${project.build.finalName}.war</filename>
	</configuration>
</plugin>

Use mvn wildfly:start to start the application server and mvn wildfly:stop to stop it. 

Executing CLI commands before deployment

Sometimes it is necessary to create/delete some resources before executing a deployment. You can include a CLI command to be executed before deployment in the following way:

<configuration>
   <beforeDeployment>
     <commands>
       <command>/subsystem=messaging-activemq/server=default/jms-queue=demoQueue:add(entries=["java:/jms/queue/testQueue"])</command>
     </commands>
   </beforeDeployment>
</configuration>

Executing a CLI script from the plugin

If you need to execute a more complex list of CLI commands, then you can use the execute-commands goals to execute some commands which can be contained in the pom.xml file or maintained in a separate file. In this example we are executing CLI commands contained into a file named commands.cli:

<plugin>
	<groupId>org.wildfly.plugins</groupId>
	<artifactId>wildfly-maven-plugin</artifactId>
	<version>1.0.2.Final</version>
	<configuration>
		<execute-commands>
				<scripts>
					<script>commands.cli</script>
				</scripts>
		</execute-commands>
		<filename>${project.build.finalName}.war</filename>
	</configuration>
</plugin>

 Execute the CLI commands with:

mvn wildfly:execute-commands

Deploying to a domain

If you are going to deploy your applications on a domain of servers, include a domain element with the list of server groups where you will deploy your applications:

<configuration>
   <domain>
      <server-groups>
         <server-group>main-server-group</server-group>
      </server-groups>
   </domain>
</configuration>

Follow us on Twitter