Build Tool Integration

Page last updated: May 13, 2015

This page assumes you are using cf CLI v6 and version 1.1.2 of either the Cloud Foundry Maven plugin or the Cloud Foundry Gradle plugin.

Maven Plugin

The Cloud Foundry Maven plugin allows you to deploy and manage applications with Maven goals. This plugin provides Maven users with access to the core functionality of the Cloud Foundry cf command-line tool.

Basic Configuration

To install the Cloud Foundry Maven plugin, add the cf-maven-plugin to the <plugins> section of the pom.xml file:

  <plugins>
      <plugin>
          <groupId>org.cloudfoundry</groupId>
          <artifactId>cf-maven-plugin</artifactId>
          <version>1.1.2</version>
      </plugin>
  </plugins>

This minimal configuration is sufficient to execute many of the Maven goals provided by the plugin, as long as you provide all other necessary configuration information through command-line parameters.

Additional Configuration

Instead of relying on command-line parameters, you can include additional configuration information in the pom.xml by nesting a <configuration> section within the cf-maven-plugin section.

Example:

  <plugins>
      <plugin>
          <groupId>org.cloudfoundry</groupId>
          <artifactId>cf-maven-plugin</artifactId>
          <version>1.1.2</version>
          <configuration>
              <target>http://api.run.pivotal.io</target>
              <org>mycloudfoundry-org</org>
              <space>development</space>
              <appname>my-app</appname>
              <url>my-app.shared-domain.com</url>
              <memory>512</memory>
              <instances>2</instances>
              <env>
                  <ENV-VAR-NAME>env-var-value</ENV-VAR-NAME>
              </env>
              <services>
                  <service>
                      <name>my-rabbitmq</name>
                      <label>rabbitmq</label>
                      <provider>rabbitmq</provider>
                      <version>n/a</version>
                      <plan>small_plan</plan>
                  </service>
              </services>
          </configuration>
      </plugin>
  </plugins>

After adding and configuring the plugin you can build and push the application to Cloud Foundry with the following command:

$ mvn clean package cf:push

Security Credentials

While you can include Cloud Foundry security credentials in the pom.xml file, a more secure method is to store the credentials in the Maven settings.xml file, using the server XML configuration element (http://maven.apache.org/settings.html#Servers). The default location for this configuration file is ~/.m2/settings.xml.

To implement this:

  1. Add a server to the servers section of the settings.xml file. Include the Cloud Foundry security credentials (username and password) and an ID tag. The pom.xml references this ID to access the security credentials.

    <settings>
        . . .
        <servers>
            . . .
            <server>
                <id>cloud-foundry-credentials</id>
                <username>[email protected]</username>
                <password>s3cr3t</password>
            </server>
            . . .
        </servers>
        . . .
    </settings>
    
  2. Add a server configuration element referencing the ID to the pom.xml file:

    <plugins>
        <plugin>
            <groupId>org.cloudfoundry</groupId>
            <artifactId>cf-maven-plugin</artifactId>
            <version>1.1.2</version>
            <configuration>
                <server>cloud-foundry-credentials</server>
                . . .
            </configuration>
        </plugin>
    </plugins>
    

Command-Line Usage

Key functionality available with the Cloud Foundry Maven plugin:

Maven GoalCloud Foundry CommandSyntax
cf:loginlogin -u USERNAME$ mvn cf:login
cf:logoutlogout$ mvn cf:logout
cf:appapp APPNAME$ mvn cf:app [-Dcf.appname=APPNAME]
cf:appsapps$ mvn cf:apps
cf:targetapi$ mvn cf:target
cf:pushpush$ mvn cf:push [-Dcf.appname=APPNAME] [-Dcf.path=PATH] [-Dcf.url=URL] [-Dcf.no-start=BOOLEAN]
cf:startstart APPNAME$ mvn cf:start [-Dcf.appname=APPNAME]
cf:stopstop APPNAME$ mvn cf:stop [-Dcf.appname=APPNAME]
cf:restartrestart APPNAME$ mvn cf:stop [-Dcf.appname=APPNAME]
cf:deletedelete APPNAME$ mvn cf:delete [-Dcf.appname=APPNAME]
cf:scalescale APPNAME -i INSTANCES$ mvn cf:scale [-Dcf.appname=APPNAME] [-Dcf.instances=INTEGER]
cf:envenv APPNAME$ mvn cf:env [-Dcf.appname=APPNAME]
cf:servicesservices$ mvn cf:services
cf:create-servicescreate-service SERVICE PLAN SERVICE_INSTANCE$ mvn cf:create-services
cf:delete-servicesdelete-service SERVICE_INSTANCE$ mvn cf:delete-service
cf:bind-servicesbind-service APPNAME SERVICE_INSTANCE$ mvn cf:bind-services
cf:unbind-servicesunbind-service APPNAME SERVICE_INSTANCE$ mvn cf:unbind-services

Gradle Plugin

The Cloud Foundry Gradle plugin allows you to deploy and manage applications with Gradle tasks. This plugin provides Gradle users with access to the core functionality of the Cloud Foundry cf command-line tool.

Basic Configuration

To install the Cloud Foundry Gradle plugin, add the cf-gradle-plugin as a dependency in the buildscript section of the build.gradle file:

  buildscript {
      repositories {
          mavenCentral()
      }
      dependencies {
          classpath 'org.cloudfoundry:cf-gradle-plugin:1.1.2'
          . . .
      }
  }

  apply plugin: 'cloudfoundry'

This minimal configuration is sufficient to execute many of the Gradle tasks provided by the plugin, as long as you provide all other necessary configuration information through command-line parameters

Additional Configuration

Instead of relying on command-line parameters, you can add additional configuration information to build.gradle in a cloudfoundry configuration section:

  cloudfoundry {
      target = "https://api.run.pivotal.io"
      space = "deployment"
      file = file("path/to/my/file.war")
      uri = "my-app.shared-domain.com"
      memory = 512
      instances = 1
      env = [ "key": "value" ]
      serviceInfos {
          "my_rabbitmq" {
              label = "rabbitmq"
              plan = "small_plan"
              bind = true
          }
      }
  }

After adding and configuring the plugin you can build and push the application to Cloud Foundry with the following command:

$ gradle clean assemble cfPush

Security Credentials

While you can include Cloud Foundry security credentials in the build.gradle file, a more secure method is to store the credentials in a gradle.properties file. This file can be placed in either the project directory or in the ~/.gradle directory.

To implement this, add cfUsername and cfPassword with the Cloud Foundry security credentials parameters to the gradle.properties file as follows:

  [email protected]
  cfPassword=examplePassword

(Note there are no quotes around either the username or password.)

Command-Line Usage

Key functionality available with the Cloud Foundry Gradle plugin:

Gradle TaskCloud Foundry CommandSyntax
cfLoginlogin -u USERNAME$ gradle cfLogin
cfLogoutlogout$ gradle cfLogout
cfAppapp APPNAME$ gradle cfApp [-PcfApplication=APPNAME]
cfAppsapps$ gradle cfApps
cfTargetapi$ gradle cfTarget
cfPushpush$ gradle cfPush [-PcfApplication=APPNAME] [-PcfUri=URL] [-PcfStartApp=BOOLEAN]
cfStartstart APPNAME$ gradle cfStart [-PcfApplication=APPNAME]
cfStopstop APPNAME$ gradle cfStop [-PcfApplication=APPNAME]
cfRestartrestart APPNAME$ gradle cfStop [-PcfApplication=APPNAME]
cfDeletedelete APPNAME$ gradle cfDelete [-PcfApplication=APPNAME]
cfScalescale APPNAME -i INSTANCES$ gradle cfScale [-PcfApplication=APPNAME] [-PcfInstances=INTEGER]
cfEnvenv APPNAME$ gradle cfEnv [-PcfApplication=APPNAME]
cfServicesservices$ gradle cfServices
cfCreateServicecreate-service SERVICE PLAN SERVICE_INSTANCE$ gradle cfCreateServices
cfDeleteServicesdelete-service SERVICE_INSTANCE$ gradle cfDeleteServices
cfBindbind-service APPNAME SERVICE_INSTANCE$ gradle cfBind
cfUnbindunbind-service APPNAME SERVICE_INSTANCE$ gradle cfUnbind