Maven JBoss AS 7 plugin tutorial

The Maven JBoss plugin can be used to perform some common tasks like deploy/undeploy or add resources as part of your Maven goal. In the latest release of this plugin there is even the ability to perform the execution of Command Line Interface commands using it. This tutorial shows how to do all these things.

The Maven JBoss plugin, known as jboss-as-maven-plugin can be used to perform automatically some activities by connecting to a JBoss AS 7 running instance via the management port of the application server. If you don’t provide any configuration detail about your server, the plugin will attemp connecting to the server located on localhost at port 9999.

So here’s a minimal jboss-as-maven-plugin snippet which can be included into the plugin section of your Maven’s pom.xml file:


<project>
    ...
    <build>
        ...
        <plugins>
            ...
            <plugin>
                <groupId>org.jboss.as.plugins</groupId>
                <artifactId>jboss-as-maven-plugin</artifactId>
                <version>7.3.Final</version>
            </plugin>
            ...
        </plugins>
        ...
    </build>
...
</project>

With this file in place, you can deploy your Maven application by executing simply:


mvn jboss-as:deploy

The application can be also redeployed using the


mvn jboss-as:redeploy

In order to undeploy issue the following command:


mvn jboss-as:undeploy

If your server is not running on the default address/port, you can use the following configuration parameters in order to connect to it:


<plugin>
                <groupId>org.jboss.as.plugins</groupId>
                <artifactId>jboss-as-maven-plugin</artifactId>
                <version>7.3.Final</version>

                    <configuration>
                      <hostname>192.168.1.1</hostname>
                      <port>9999</port>

                      <name>useradmin123</name>

                      <password>password123</password>
                </configuration>

</plugin>

The configuration stanza can be used also to specify domain deployment. In this case, we are deploying our application to the other-server-group:


<plugin>

. . . . .

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

</plugin>

By using the Maven JBoss plugin enables you also to start and stop the application server. In order to do that the jboss-as:start goal will start a JBoss Application Server. If the jboss-as.home property is not set, the server will be downloaded. The server will be shutdown when the maven process ends unless an explicit shutdown was invoked.

To execute the start goal type the following on the command line:


mvn jboss-as:start

To execute the shutdown goal type the following on the command line:


mvn jboss-as:shutdown

How to deploy an application to multiple servers ?

Using the configuration parameters it’s possible to parametrize the plugin and then publish the application to a different set of servers:


<plugin>
                <groupId>org.jboss.as.plugins</groupId>
                <artifactId>jboss-as-maven-plugin</artifactId>
                <version>7.3.Final</version>

                    <configuration>
                    <hostname>${hostname}</hostname>
                    <port>${port}</port>                    

                </configuration>

</plugin>

Then pass the host and port to the maven shell:


mvn jboss-as:deploy -Dhost=192.168.0.1 -Dport=9999

mvn jboss-as:deploy -Dhost=192.168.0.1 -Dport=10199

How to add JBoss AS 7 dependencies to your artifacts ?

As you probably know, with JBoss AS 7 you need to explicitily declare your dependencies (except for core libraries) using the Dependencies element in the MANIFEST.MF file. For example:

Dependencies: org.infinispan export

You can achieve the same thing using the Maven plugins adding some configuration parameters. For a Web application:


<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-war-plugin</artifactId>
   <configuration>
      <archive>
         <manifestEntries>
            <Dependencies>org.infinispan export</Dependencies>
         </manifestEntries>
      </archive>
   </configuration>
</plugin>

And for an EJB application:


<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-ejb-plugin</artifactId>
   <version>${version.ejb.plugin}</version>
   <configuration>
      <ejbVersion>3.1</ejbVersion>
      <archive>
         <manifestEntries>
            <Dependencies>org.infinispan export</Dependencies>
         </manifestEntries>
      </archive>
   </configuration>
</plugin>


 

By adding the executions element in your plugin configuration, you will be able to perform some advanced activities.

For example, supposing that you want to issue your deployment automatically when you execute the maven install phase:


mvn install

Then, you need to bind the install phase to the deploy goal of your plugin. This can be done with a little add-on to your configuration::


<build>
      <plugins>
         <plugin>
            <groupId>org.jboss.as.plugins</groupId>
            <artifactId>jboss-as-maven-plugin</artifactId>
            <version>7.3.Final</version>

            <executions>
               <execution>
                  <phase>install</phase>
                  <goals>
                     <goal>deploy</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>

Quite interestingly, you can also add resources to your application server by using the deploy-artifact and add-resource goal. Here’s for example how to deploy a MySQL JDBC Driver and install a Datasource using this driver.


<build>
      <plugins>
         <plugin>
            <groupId>org.jboss.as.plugins</groupId>
            <artifactId>jboss-as-maven-plugin</artifactId>
            <version>7.3.Final</version>
            <executions>

               <execution>
                  <phase>install</phase>
                  <goals>
                     <goal>deploy</goal>
                  </goals>
               </execution>
              
               <execution>
                  <id>deploy-mysql</id>
                  <phase>package</phase>
                  <goals>
                     <goal>deploy-artifact</goal>
                  </goals>
                  <configuration>
                     <groupId>mysql</groupId>
                     <artifactId>mysql-connector-java</artifactId>
                     <name>mysql-connector-java-5.1.18-bin.jar</name>
                  </configuration>
               </execution>

               <execution>
                  <id>add-datasource</id>
                  <phase>package</phase>
                  <goals>
                     <goal>add-resource</goal>
                  </goals>
                  <configuration>
                     <address>subsystem=datasources,data-source=java:jboss/datasources/MySqlDS</address>
                     <resource>
                        <enable-resource>true</enable-resource>
                        <properties>
                           <jndi-name>java:jboss/datasources/MySqlDS</jndi-name>
                           <enabled>true</enabled>
                           <connection-url>jdbc:mysql://localhost:3306/mysqlDB</connection-url>
                           <driver-class>com.mysql.jdbc.Driver</driver-class>
                           <driver-name>mysql-connector-java-5.1.18-bin.jar</driver-name>
                           <security.user-name>root</security.user-name>
                           <security.password />
                           <pool-name>mypool</pool-name>
                        </properties>
                     </resource>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>

As you can see, the plugin contains two additional executions: a deploy-mysql execution which will deloy the MySQL JDBC Driver to the application server, and an add-datasource execution which will configure a datasource using this driver.

In order to run this example, don’t forget to add a dependency to MySQL JDBC driver in your pom.xml:


<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.18</version>
</dependency>

The Maven plugin can be used to create JMS resources such as a JMS queue:


<execution>
   <id>add-queue</id>
   <phase>install</phase>
   <configuration>
      <resource>
         <address>subsystem=messaging,hornetq-server=default,jms-queue=queue1</address>
         <properties>
            <durable>true</durable>
            <entries>!!["java:jboss/queue1", "java:jboss/queue1Alias"]</entries>
         </properties>
      </resource>
   </configuration>
   <goals>
      <goal>add-resource</goal>
   </goals>
</execution>

Finally, you can add to your configuration stanza also generic CLI commands such as the following example:

<configuration>
   <hostname>localhost</hostname>
   <port>9999</port>
   <execute-commands>
      <batch>true</batch>
      <commands>
         <command>/system-property=foo:add(value=bar)</command>
      </commands>
   </execute-commands>
</configuration>
Found the article helpful? if so please follow us on Socials