Deploying with Maven2 to a remote JBoss instance

Some time ago I already explained how you could use Maven and the jboss-plugin to deploy your war or ear to your JBoss server. Now this works fine as long as your JBoss is a local installation running on the same machine as the one that is running Maven.
Of course there is a big chance that this is not the case in your development environment. In that case you can do the following in Maven to deploy your ear to the JBoss instance:

  1. Transfer the war or ear file to the remote machine
  2. Run a script on the remote machine

Actually it isn’t doing anything JBoss specific, so you could use it for other application servers too (like Tomcat, at least when they are using hot deployment).
To perform step 1 you have to add the following plugin in your ‘pom.xml’:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>wagon-maven-plugin</artifactId>
      <version>1.0-beta-1</version>
      <executions>
        <execution>
          <id>upload-artifact</id>
          <phase>deploy</phase>
          <goals>
            <goal>upload</goal>
          </goals>
          <configuration>
            <serverId>${project.deploy.serverId}</serverId>
            <fromDir>target</fromDir>
            <includes>*.war</includes>
            <url>${project.deploy.url}</url>
            <toDir>${project.deploy.toDir}</toDir>
          </configuration>
        </execution>
      </executions>
   </plugin>
    ...
  </plugin>
</build>

And this plugin needs the ‘distributionManagement’ section to be filled like this:

<distributionManagement>
   <repository>
     <id>m2-internal-repo</id>
     <url>http://local.pascalalma.net:8888/artifactory/libs-releases-local/</url>
   </repository>
   <snapshotRepository>
     <id>m2-internal-repo</id>
     <url>http://local.pascalalma.net:8888/artifactory/libs-snapshots-local</url>
   </snapshotRepository>
</distributionManagement>

The second step is a bit more complicated. I want to execute a script on the remote server using SSH. This can rather easily be done by using an Ant target, so to get this working in Maven we have to use the plugin to run an Ant target. It will ook like this:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-antrun-plugin</artifactId>
     <version>1.3</version>
     <dependencies>
       <dependency>
         <groupId>org.apache.ant</groupId>
         <artifactId>ant-jsch</artifactId>
         <version>1.7.1</version>
       </dependency>
       <dependency>
         <groupId>com.jcraft</groupId>
         <artifactId>jsch</artifactId>
         <version>0.1.41</version>
       </dependency>
       </dependencies>
         <executions>
           <execution>
             <id>deploy-artifact</id>
             <phase>deploy</phase>
             <goals>
               <goal>run</goal>
             </goals>
             <configuration>
               <tasks>
                  <sshexec host="${project.deploy.host}"
                                  username="${projecte.deploy.username}"
                                  password="${project.deploy.password}"
                                  trust="true"
                                 command="bin/deploy_web.sh"/>
               </tasks>
             </configuration>
           </execution>
         </executions>
      </plugin>

Thta’s it. These two plugins together do the job. For completeness I show the used properties that I have setup in my ‘settings.xml’:

<profiles>
  <profile>
    <id>develop</id>
    <properties>
      <project.deploy.serverId>mule-server</mule.deploy.serverId>
      <project.deploy.url>scp://mule-server</mule.deploy.url>
      <project.deploy.toDir>/home/pascal</mule.deploy.toDir>
      <project.deploy.host>mule-server</mule.deploy.host>
      <project.deploy.username>pascal</mule.deploy.username>
      <project.deploy.password>pascal</mule.deploy.password>
    </properties>
  </profile>
  ...
</profiles>

And the script that is executed at the remote server ‘deploy_web.sh’:

#
# Deploy Mule applications
#
#
source /etc/profile

echo "Start deploying"

for archive in ~/deploy/*.war; do
  echo "Copying to tomcat: web-archive \"$archive\""
  cp $archive /$TOMCAT_HOME/webapps/
done

echo "Deploying finished"

echo "Clean up ~/deploy directory"
rm -rf ~/deploy/*.war

If all this is in place you can deploy the ear/war file by using the command:
mvn clean deploy
Like I said before there is nothing JBoss specific to this and actually I am using the script to deploy my Mule applications to our Tomcat server.

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in JBoss, Maven and tagged , . Bookmark the permalink.

14 Responses to Deploying with Maven2 to a remote JBoss instance

  1. mike says:

    fantastic, I was literally looking for this today, your solution worked great, I could not for the life of me, get cargo’s jboss plugin to deploy remotely, wagon was the trick.

  2. Dea says:

    When I use your setup, I can get maven to run with no errors, but I do not see any file on the remote server.

    I can see maven executing my script, but the file does nothing, because the file are not there.

    [INFO] [antrun:run]
    [INFO] Executing tasks
    [sshexec] Connecting to cssw-sals01:22
    [sshexec] cmd : bin/deploy_web.sh
    Start deploying
    Copying to jboss: web-archive “/css/eServ/temp/*.jar”
    bin/deploy_web.sh: total
    Deploying finished

    My jar files are in my maven repository.

    What is wrong?

  3. Pascal Prins says:

    @Dea
    No error msg’s? Could you share your “deploy_web.sh” and your “maven-antrun-plugin” configuration?

    I noticed you’re copying jar-files iso war-files?

  4. newbea says:

    when i am using wagon-maven-plugin to transfer war file to my server pc,it is asking for password.
    plz tell me how to configure password for SCP transfer.

  5. Pascal Alma says:

    @newbea
    Ah, I realize I forgot to mention that one:
    In my settings.xml I have configured some servers like this:
    [xml]

    mule-server
    pascal
    pascal

    [/xml]
    So you should do that for your server pc also.

    Hope that helps

  6. Dea says:

    the deploy_web.sh file is the same as above, but I am copying *.jar instead of *.war

    echo “Start deploying”

    for archive in /css/eServices/temp/*.jar; do
    echo “Copying to JBoss: web-archive “$archive””
    cp $archive /css/eServices/temp/
    done
    echo “Deploying finished”
    echo “Clean up ~/deploy directory”

    My AntRun-plugin configuration:

    org.apache.maven.plugins
    maven-antrun-plugin
    1.3

    org.apache.ant
    ant-jsch
    1.7.1

    com.jcraft
    jsch
    0.1.41

    deploy-artifact
    deploy

    run

    My setup seems correct to what is listed in the article, the only thing missing is that the files from the target directory are not getting downloaded to to the remote servers.

  7. Dea says:

    Is Wagon only for war file?

  8. Pascal Alma says:

    @Dea, can you show the wagon plugin config and the distributionManagement section? Also note that I commented at 23 December you also have to define the servers in your settings.xml with the username/password if applicable for your situation.
    And to show xml content in the comment you have to enclose it in [ xml] and [ / xml] (without the spaces).

  9. Dea says:

    For my first walk thru of the project. I just hardcoded all the properties into the pom.xml file.

    But here is my DM section and the wagon plugin.

    org.codehaus.mojo
    wagon-maven-plugin
    1.0-beta-1

    upload-artifact
    deploy

    upload

    crm-dals02
    target
    *.jar
    scp://crm-dals02.comp.wepco.com
    /css/eServices/temp

    org.apache.maven.plugins
    maven-antrun-plugin
    1.3

    org.apache.ant
    ant-jsch
    1.7.1

    com.jcraft
    jsch
    0.1.41

    deploy-artifact
    deploy

    run

    I took a step back and tried to get mvn to a local deployment.

    m2-internal-repo
    NameSomething
    file:///home/csstech/.m2/deploy/repository/
    <!– scp://crm-dals02.comp.wepco.com/css/eServices/temp
    –>

  10. Dea says:

    Here it go again:

                                    org.codehaus.mojo                      wagon-maven-plugin
        1.0-beta-1</version
    
    
                upload-artifact
                 deploy
    
                  upload
    
    
          crm-dals02
          target
         *.jar
         scp://crm-dals02.comp.wepco.com
         /css/eServices/temp
    
    
    
    
    
    
    
                            m2-internal-repo
                            NameSomething
                            file:///home/csstech/.m2/deploy/repository/
                    <!--            scp://crm-dals02.comp.wepco.com/css/eServices/temp
                    -->
    
    
    
    
    
  11. Dea says:

    I have to apologize, I can not get the XML tags to display in the posting.

    I am using the
    [ xml ][ / xml]

    Can I get help please on posting?

    Thank you for your patience!

    • Pascal Alma says:

      Hi Dea,

      I apologize for the xml syntax not working properly in the plugin I use. Can you send your pom.xml to ‘pascal.alma at redstream.nl’ so I can give it a quick look to see if I can find something.

  12. Dea says:
    <distributionManagement>
        <repository>
            <id>m2-internal-repo</id>
            <name>NameSomething</name>
            <url>file:///home/csstech/.m2/deploy/repository/</url>
        </repository>
    </distributionManagement>
    
  13. Dea says:

    Thanks Pascal,

    I sent you the snippets of my plugin and DM from the pom.xml

Comments are closed.