GlassFish 3 + Maven + remote deploy

Lurtz picture Lurtz · Sep 21, 2011 · Viewed 9.7k times · Source

I couldn't find any clear answer about how to deploy simple Maven based project to remote GlassFish server via maven like

mvn package xxx:deploy

I think only cargo plugin supports GlassFish 3. Right?

I've problems at configuration side.

Any sample remote GlassFish deployment will be great. Cargo is not a must, if others are support remote GlassFish then we can also use it too.

Answer

Developerx picture Developerx · Oct 5, 2012

In case you want to only use maven-glassfish-plugin (let say version 2.1), you can do a remote deploy by specifying the "host" parameter. Below is an example where configurations are setup in maven settings.xml and an plugin loads them using a profile:

In settings.xml define a profile:

<profile>
     <id>production-config</id>    
     <properties>
       <glassfish.glassfishDirectory>/var/local/glassfish/</glassfish.glassfishDirectory>
       <glassfish.user>admin</glassfish.user>
       <glassfish.adminPassword>adminadmin</glassfish.adminPassword>
       <glassfish.domain.name>prd-domain</glassfish.domain.name>
       <glassfish.domain.host>NAMEOFYOURREMOTEHOST</glassfish.domain.host>
       <glassfish.domain.adminPort>10161</glassfish.domain.adminPort>
       .
       .
     </properties> 
</profile>

Next put this profile in your active profiles:

<activeProfiles>
    <activeProfile>production-config</activeProfile>
</activeProfiles>

In your maven project pom.xml, create a profile and add the maven-glassfish-plugin in your list of profiles:

<profile>
    <id>production</id>
    <activation>
     <activeByDefault>false</activeByDefault>
     <os>
    <arch>x86</arch>
    <family>linux</family>
     </os>
     <property>
    <name>profile</name>
    <value>production</value>
     </property>
     <file>
      <exists>
       ${glassfish.glassfishDirectory}/domains/${glassfish.domain.name}/config/domain.passwords
      </exists>
     </file>
   </activation>
   <build>
      <plugins>
          <plugin>
               <groupId>org.glassfish.maven.plugin</groupId>
               <artifactId>maven-glassfish-plugin</artifactId>
               <configuration>
                  <terse>true</terse>
                  <echo>true</echo>
                  <debug>true</debug>
                  <glassfishDirectory>${glassfish.glassfishDirectory}</glassfishDirectory>
                  <user>${glassfish.user}</user>
                  <adminPassword>${glassfish.adminPassword}</adminPassword>
                  <domain>
                     <name>${glassfish.domain.name}</name>
                     <host>${glassfish.domain.host}</host>
                     <adminPort>${glassfish.domain.adminPort}</adminPort>
                  </domain>
                  <components>
                    <component>
                      <name>${project.artifactId}</name>  
                 <artifact>${project.build.directory}/${project.build.finalName}.war</artifact>
                    </component>
                  </components>
               </configuration>
               <executions>
                    <execution>
                <goals>
            <goal>deploy</goal>
            </goals>
            </execution>
        </executions>
         </plugin>
      </plugins>
    </build>
</profile>

This should do the trick. You can run this profile using maven : mvn glassfish:deploy -P production or just mvn deploy -P production (since we have added the goal deploy inside the executions part of plugin)

Using the model above you can create different profile per environment (dev, acc, tst, prd), and use different settings. For instance you can create a developer profile where a local glassfish is being used to deploy and run unit/integration tests on it.

Common mistake people make is to mix up the settings for the machine from where you are doing the remote deployment with the host where deployment is to be installed. glassfishDirectory is place from where you are running the deployment plugin from. As a result of mistake plugin just hangs, doing nothing and just waiting giving the impression that something is happening. Another mistake is to specify a password file instead of a password for a remote deploy which will also result in nothing.