Iterate on your new app using Tanzu Developer Tools for IntelliJ

This topic guides you through starting to iterate on your first application on Tanzu Application Platform (commonly known as TAP). You deployed the app in the previous how-to Deploy your first application.

What you will do

  • Prepare to iterate on your application.
    • Prepare your project to support Live Update.
    • Prepare your IDE to iterate on your application.
  • Apply your application to the cluster.
  • Live update your application to view code changes updating live on the cluster.
  • Debug your application.
  • Delete your application from the cluster.

Prepare to iterate on your application

In the previous Getting started how-to topic, Deploy your first application, you deployed your first application on Tanzu Application Platform. Now that you have developed a skeleton workload, you are ready to begin to iterate on your new application and test code changes on the cluster.

Tanzu Developer Tools for IntelliJ is VMware Tanzu’s official IDE extension for IntelliJ. It helps you develop and receive fast feedback on your workloads running on the Tanzu Application Platform.

The IntelliJ extension enables live updates of your application while running on the cluster and allows you to debug your application directly on the cluster.

For information about installing the prerequisites and the Tanzu Developer Tools for IntelliJ extension, see Install Tanzu Developer Tools for IntelliJ.

Important

Use Tilt v0.30.12 or later for the sample application.

To prepare to iterate on your application, you must:

  1. Prepare your project to support Live Update
  2. Set up the IDE

Prepare your project to support Live Update

Tanzu Live Update uses Tilt. This requires a suitable Tiltfile to exist at the root of your project. Both Gradle and Maven projects are supported, but each requires a Tiltfile specific to that type of project.

The Tanzu Java Web App accelerator allows you to choose between Maven and Gradle and includes a Tiltfile. If you used the accelerator, your project is already set up correctly.

To verify your project is set up correctly, review the following requirements depending on your chosen build system.

Maven Spring Boot project requirements

If you are using Maven, your Tiltfile must be similar to the following:

SOURCE_IMAGE = os.getenv("SOURCE_IMAGE", default='your-registry.io/project/tanzu-java-web-app-source')
LOCAL_PATH = os.getenv("LOCAL_PATH", default='.')
NAMESPACE = os.getenv("NAMESPACE", default='default')

k8s_custom_deploy(
    'tanzu-java-web-app',
    apply_cmd="tanzu apps workload apply -f config/workload.yaml --update-strategy replace --debug --live-update" +
               " --local-path " + LOCAL_PATH +
               " --source-image " + SOURCE_IMAGE +
               " --namespace " + NAMESPACE +
               " --yes --output yaml",
    delete_cmd="tanzu apps workload delete -f config/workload.yaml --namespace " + NAMESPACE + " --yes",
    container_selector='workload',
    deps=['pom.xml', './target/classes'],
    live_update=[
      sync('./target/classes', '/workspace/BOOT-INF/classes')
    ]
)

k8s_resource('tanzu-java-web-app', port_forwards=["8080:8080"],
            extra_pod_selectors=[{'carto.run/workload-name': 'tanzu-java-web-app', 'app.kubernetes.io/component': 'run'}])

Gradle Spring Boot project requirements

If you are using Gradle, review the following requirements:

  • The Tiltfile looks like a Maven Tiltfile except for some key differences in the deps= and live-update= sections:

        ...
        deps=['build.gradle.kts', './build/classes/java/main', './build/resources/main'],
        live_update=[
           sync('./build/classes/java/main', '/workspace/BOOT-INF/classes'),
           sync('./build/resources/main', '/workspace/BOOT-INF/classes')
        ]
        ...
    
  • The project must be built as an exploded JAR. This is not the default behavior for a Gradle-based build. For a typical Spring Boot Gradle project, you must deactivate the jar task in the build.gradle.kts file as follows:

    ...
    tasks.named<Jar>("jar") {
        enabled = false
    }
    

Set up the IDE

After verifying your project has the required Tiltfile and Maven or Gradle build support, you are ready to set up your development environment.

  1. Open the Tanzu Java Web App as a project within your IntelliJ IDE by selecting File > Open, then selecting the Tanzu Java Web App folder and clicking Open. If you don’t have the Tanzu Java Web App you can obtain it by following the instructions in Generate a new project using an Application Accelerator, or from the Application Accelerator Samples GitHub page.

  2. Confirm that your current Kubernetes context contains a default namespace. The Tanzu Panel, found by clicking Tanzu Panel at the bottom-left of the IntelliJ window, uses the default namespace associated with your current Kubernetes context to populate the workloads from the cluster.

    1. Open the Terminal by clicking View > Terminal.

    2. Ensure that your current context has a default namespace by running:

      kubectl config get-contexts
      

      This command returns a list of all of your Kubernetes contexts with an asterisk (*) in front of your current context. Verify that your current context has a namespace in the namespace column.

    3. If your current context does not have a namespace in the namespace column, run:

      kubectl config set-context --current --namespace=YOUR-DEVELOPER-NAMESPACE
      

      Where YOUR-DEVELOPER-NAMESPACE is the namespace value you want to assign to your current Kubernetes context.

You are now ready to iterate on your application.

Apply your application to the cluster

Apply the workload to see your application running on the cluster:

  1. In the Project tab in IntelliJ, right-click any file under the application name tanzu-java-web-app and click Tanzu > Apply Workload.

  2. In the dialog box enter your Source Image, Local Path, and optionally a Namespace.

    1. Customize the installation of Local Source Proxy.

      If you don’t have Local Source Proxy configured, you can use the source image parameter instead. The source image value tells the Tanzu Developer Tools for IntelliJ extension where to publish the container image with your non-compiled source code, and what to name that image. The image must be published to a container image registry where you have write access. For example, gcr.io/myteam/tanzu-java-web-app-source.

      Note

      See the documentation for the registry you’re using to find out which steps are necessary to authenticate and gain push access.

      For example, if you use Docker, see the Docker documentation, or if you use Harbor, see the Harbor documentation.

    2. In the Local Path text box, provide the path to the directory containing the Tanzu Java Web App. The current directory is the default.

      The local path value tells the Tanzu Developer Tools for IntelliJ extension which directory on your local file system to bring into the source image. For example, dot (.) uses the working directory, or you can specify a full file path.

    3. (Optional) In the Namespace text box, provide the namespace to be associated with the workload on the cluster. If you followed the steps to Prepare your IDE to iterate on your application earlier, you do not need to enter a namespace because IntelliJ uses the namespace you associated with your context.

    4. Click the OK button.

The apply workload command runs, which opens a terminal and shows you the output of the command. The apply workload command can take a few minutes to deploy your application onto the cluster.

You can also use the Tanzu Panel to monitor your application as it’s being deployed to the cluster. The Tanzu Panel shows information about the workloads in the namespace associated with your current Kubernetes context. On the left side, it shows the workloads in the namespace. In the center, it shows the details of the Kubernetes resources for the running workloads.

Enable Live Update for your application

Live Update allows you to save changes to your code and see those changes reflected within seconds in the workload running on the cluster.

To enable Live Update for your application:

  1. Create a Run Configuration.

    1. In IntelliJ, select the Edit Run/Debug configurations drop-down menu at the top-right corner. Alternatively, navigate to Run > Edit Configurations.
    2. Select Tanzu Live Update.
    3. Select Add new run configuration, or click the plus icon at the top of the list.
    4. Give your new run configuration a name, for example, Tanzu Live Update - tanzu-java-web-app.
    5. In the Tiltfile Path text box, provide the path to the Tiltfile in the Tanzu Java Web App project directory.
    6. Select the folder icon on the right-side of the text box, go to the Tanzu Java Web App directory, select the Tiltfile, and click Open. The Tiltfile facilitates Live Update using Tilt.
    7. In the Local Path text box, provide the path to the directory containing the Tanzu Java Web App.

      The local path value tells the Tanzu Developer Tools for IntelliJ extension which directory on your local file system to bring into the source image.

      For example, /Users/developer/Documents/tanzu-java-web-app.

    8. Customize the installation of Local Source Proxy.

      If you don’t have Local Source Proxy configured, you can use the source image parameter instead. The source image value tells the Tanzu Developer Tools for IntelliJ extension where to publish the container image with your non-compiled source code, and what to name that image. The image must be published to a container image registry where you have write access. For example, gcr.io/myteam/tanzu-java-web-app-source.

      Note

      See the documentation for the registry you’re using to find out which steps are necessary to authenticate and gain push access.

      For example, if you use Docker, see the Docker documentation, or if you use Harbor, see the Harbor documentation.

    9. Click Apply, and then click the OK button.
  2. Begin Live Updating the application on the cluster by doing one of the following:

    • In the Project tab of IntelliJ, right-click the Tiltfile file under the application name tanzu-java-web-app and click Run 'Tanzu Live Update - tanzu-java-web-app'.

    • Alternatively, click the Edit Run/Debug configurations drop-down menu in the top-right corner, select Tanzu Live Update - tanzu-java-web-app, and then click the green play button to the right of the Edit Run/Debug configurations drop-down menu.

    The Run tab opens and displays the output from Tanzu Application Platform and from Tilt indicating that the container is being built and deployed.

    On the Tanzu Panel tab, the status of Live Update is reflected under the tanzu-java-web-app workload entry. Live update can take up to three minutes while the workload deploys and the Knative service becomes available.

    Note

    Depending on the type of cluster you use, you might see an error similar to the following:

    ERROR: Stop! cluster-name might be production. If you're sure you want to deploy there, add: allow_k8s_contexts('cluster-name') to your Tiltfile. Otherwise, switch k8scontexts and restart Tilt.

    Follow the instructions and add the line, allow_k8s_contexts('cluster-name') to your Tiltfile.

  3. When the Live Update task in the Run tab is successful, it resolves to Live Update Started. Use the hyperlink at the top of the Run output following the words Tilt started on to view your application in your browser.

  4. In the IDE, make a change to the source code. For example, in HelloController.java, edit the string returned to say Hello! and save.

  5. (Optional) Build your project by clicking Build > Build Project if you do not have Build project automatically activated under Preferences > Build, Execution, Deployment > Compiler.

  6. The container is updated when the logs stop streaming. Navigate to your browser and refresh the page.

  7. View the changes to your workload running on the cluster.

  8. Either continue making changes, or stop the Live Update process when finished. To stop Live Update navigate to the Run tab at the bottom left of the IntelliJ window and click the red stop icon on the left side of the screen.

Debug your application

Debug the cluster either on the application or in your local environment.

To debug the cluster:

  1. Set a breakpoint in your code. For example, in HelloController.java, set a breakpoint on the line returning text.

  2. Create a Run Configuration.

    1. In IntelliJ, select the Edit Run/Debug configurations drop-down menu at the top-right corner. Alternatively, navigate to Run > Edit Configurations.
    2. Select Tanzu Debug Workload.
    3. Select Add new run configuration, or click the plus icon at the top of the list.
    4. Give your new run configuration a name, for example, Tanzu Debug Workload - tanzu-java-web-app.
    5. In the Workload File Path text box, provide the path to the workload.yaml file in the Tanzu Java Web App project directory located at Config > workload.yaml.
    6. Select the folder icon on the right-side of the text box, navigate to the Tanzu Java Web App directory, select the workload.yaml file and click the Open button. The workload.yaml provides configuration instructions about your application to the Tanzu Application Platform.

    7. In the Local Path text box, provide the path to the directory containing the Tanzu Java Web App.

      The local path value tells the Tanzu Developer Tools for IntelliJ extension which directory on your local file system to bring into the source image. For example, /Users/developer/Documents/tanzu-java-web-app.

    8. Customize the installation of Local Source Proxy.

      If you don’t have Local Source Proxy configured, you can use the source image parameter instead. The source image value tells the Tanzu Developer Tools for IntelliJ extension where to publish the container image with your non-compiled source code, and what to name that image. The image must be published to a container image registry where you have write access. For example, gcr.io/myteam/tanzu-java-web-app-source.

      Note

      See the documentation for the registry you’re using to find out which steps are necessary to authenticate and gain push access.

      For example, if you use Docker, see the Docker documentation, or if you use Harbor, see the Harbor documentation.

    9. (Optional) In the Namespace text box, provide the namespace to be associated with the workload on the cluster. If you followed the steps to Prepare your IDE to iterate on your application, you do not need to enter a namespace because IntelliJ uses the namespace you associated with your context.

    10. Click Apply, and then click OK.

  3. Apply your application to the cluster.

  4. Obtain the URL for your workload:

    1. In the center panel of the Tanzu Panel go to Workload/tanzu-java-web-app > Running Application > Service/tanzu-java-web-app.

    2. Right-click the Service/tanzu-java-web-app entry and select Describe.

      IntelliJ Tanzu Panel showing the describe action on the tanzu-java-web-app service.

    3. In the resulting output, copy the value after Status > URL: that begins with https://tanzu-java-web-app.... Make sure you copy the value from Status > URL: and not the value under Status > Address > URL.

      IntelliJ terminal showing the pod URL.

    4. Open your web browser and paste the URL you copied to access your workload.

  5. In the Project tab of IntelliJ, right-click the workload.yaml file under the application name tanzu-java-web-app and select Run 'Tanzu Debug Workload - tanzu-java-web-app' to begin debugging the application on the cluster.

    1. Alternatively, select the Edit Run/Debug configurations drop-down menu in the top-right corner, select Tanzu Debug Workload - tanzu-java-web-app, and then click the green debug button to the right of the Edit Run/Debug configurations drop-down menu.
  6. The Debug tab opens and displays a message that it has connected.

  7. In your web browser, reload your workload. IntelliJ opens to show your breakpoint.

  8. You can now use the resume program action, or stop debugging, in the Debug tab.

Delete your application from the cluster

You can use the delete action to remove your application from the cluster as follows:

  1. In the Project tab, right-click any file under the application name tanzu-java-web-app and select Tanzu > Delete Workload.

  2. Alternatively, right-click tanzu-java-web-app in the TANZU WORKLOADS panel and select Delete Workload.

  3. In the confirmation dialog box that appears, click OK to delete the application from the cluster.

Next steps

check-circle-line exclamation-circle-line close-line
Scroll to top icon