sumpacle / vscode-gradle

A VS Code extension to list & run Gradle tasks

Home Page:https://marketplace.visualstudio.com/items?itemName=richardwillis.vscode-gradle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gradle Tasks

Visual Studio Marketplace Installs Build & Publish CodeQL GitHub bug issues

This VS Code extension provides a visual interface for your Gradle build. It supports whatever Gradle supports and is language agnostic, but can work nicely alongside other extensions like the Java language support extension.

Gradle Tasks Screencast

Requirements

Project Discovery

This extension supports both root and nested Gradle projects. A Gradle project is identified by having Gradle Wrapper scripts (gradlew or gradlew.bat) at the root of a directory. (Nested Gradle project discovery is not enabled by default, set "gradle.nestedProjects": true to enable it.)

Feature Overview

Once the extension is activated you will see a new Gradle icon in the activity bar. Clicking on this icon will reveal some tree views in the sidebar.

Protip: you can move drag & drop any of these treeviews into the main explorer view for easier access. This is especially useful for pinned tasks.

You can also run any Gradle task as a VS Code task. Access the Gradle VS Code tasks by running Run Task from the command palette.

List projects and tasks

A Gradle build can have one or more projects. Projects are listed in a flat list with the root project listed first, and sub-projects listed alphabetically thereafter.

When you expand a project, tasks are listed in a tree, grouped by the task group. You can toggle the display of the tasks by clicking on the Show Flat List/Show Tree button in the treeview header.

Gradle Tasks View
Run tasks

Tasks can be run via:

  • Gradle Tasks, Pinned Tasks or Recent Tasks treeviews
  • Run Task command
  • Run a Gradle Build command

A running task will be shown with an animated "spinner" icon in the treeviews, along with Cancel Task & Restart Task buttons. The Cancel Task button will gracefully cancel the task. The Restart Task button will first cancel the task, then restart it.

Gradle Tasks Running

A task will be run a vscode terminal where you can view the task output.

Send a SIGINT signal (ctrl/cmd + c) in the terminal to gracefully cancel it.

Gradle Tasks Output

Tasks run via the Run a Gradle Build command are not reflected in any of the treeviews. Use this command to specify your own Gradle build arguments, for example to run multiple tasks or to exclude tasks.

Run Gradle Build
Control task terminal behaviour

Use one terminal for each task type (default):

"gradle.reuseTerminals": "task"

Use one terminal for all tasks:

"gradle.reuseTerminals": "all"

Don't re-use terminals for any tasks. A new terminal will be created for each task run:

"gradle.reuseTerminals": "off"
Debug JavaExec tasks

This extension provides an experimental feature to debug JavaExec tasks. Before using this feature you need to install the Debugger for Java and Language Support for Java extensions.

To enable this feature you need to specify which tasks can be debugged within your project .vscode/settings.json.

You also need to specify whether you want to clean output cache before debugging, to ensure Gradle doesn't skip any tasks due to output caching (this is most useful when debugging tests).

Output cache is cleaned by adding a cleanTaskName task (eg cleanTest) to the build.

Example config:

"gradle.javaDebug": {
    "tasks": [
        "run",
        "test",
        "subproject:customJavaExecTask"
    ],
    "clean": true
}

You should now see a debug command next to the run command in the Gradle Tasks view. The debug command will start the Gradle task with jdwp jvmArgs and start the vscode Java debugger.

Debug Screencast

Pin tasks

As there could be many tasks in a Gradle project, it can be useful to pin commonly used tasks. Pinned tasks will be shown in a seperate view. Pin a task by accessing the task context menu (by right-clicking a task). You can also pin a task with specific arguments.

Pin a Gradle Task

To remove a pinned a task, access the task context menu and select Remove Pinned Task, or clear all pinned tasks by clicking on the Clear Pinned Tasks button in the treeview header.

Remove a pinned Gradle Task
List recent tasks

Recently run Gradle tasks are listed in a seperate treeview. This can be useful to see a history of tasks and to easily access the associated task terminals.

The number shown next to the task is the amount of times the task has been run. Click on the Show Terminal button next to a task to view the most recent terminal for that task. Click on the Close Terminal/s button to close the terminal/s for that task.

Click on the Clear Recent Tasks button in the treeview header to remove all recent tasks from the list, or click on the Close All Terminals button to close all task terminals.

Recent Tasks
List & kill Gradle daemons

Gradle daemon processes are listed by their process ID in a seperate treeview and can have the following states: IDLE, BUSY, STOPPED, STOPPING, CANCELED.

Stop individual daemons by clicking on the Stop Daemon button next to the listed daemon.

Stop all daemons by clicking on the Stop Daemons button in the treeview header.

Recent Tasks

After stopping a daemon, it will remain in the STOPPED state for a while, but the underlying process won't exist. This is the default Gradle behaviour.

Most of the time there should be no reason to stop a daemon. View more info on the Gradle Daemon from the Gradle website.

The extension uses the Gradle wrapper to list daemons, and is quite a slow process. If the daemon view is not useful for you, you can simply collapse the view, or disable it completely.

Full features list
  • List Gradle Tasks & Projects
  • Run Gradle tasks as VS Code tasks
  • Supports massive Gradle projects (eg with 10000+ tasks)
  • Uses the Gradle Tooling API to discover and run Gradle tasks
  • Uses a long running gRPC server which provides good performance
  • Supports Kotlin & Groovy build files
  • Supports multi-project builds
  • Supports multi-root workspaces
  • Supports nested projects (enabled via setting)
  • Show flat or nested tasks in the explorer
  • Gracefully cancel a running task
  • Debug JavaExec tasks
  • Run/debug a task with arguments (supports both build & task args, eg gradle tasks --all --info)
  • Pin tasks
  • List recent tasks
  • List & stop daemons
  • Reports Gradle dist download progress

VS Code Settings

This extension contributes the following settings:

  • gradle.autoDetect: Automatically detect Gradle tasks ("on" or "off")
  • gradle.focusTaskInExplorer: Focus the task in the explorer when running a task (boolean)
  • gradle.nestedProjects: Process nested projects (boolean or an array of directories)
  • gradle.reuseTerminals: Reuse task terminals ("task" [default], "all", or "off")
  • gradle.javaDebug: Debug JavaExec tasks (see below for usage)
  • gradle.debug: Show extra debug info in the output panel (boolean)
  • gradle.disableConfirmations: Disable the warning confirm messages when performing batch actions (eg clear tasks, stop daemons etc) (boolean)

Gradle & Java Settings

Set Gradle & Java options with standard environment variables or standard Gradle settings (eg via gradle.properties or task configuration).

Example Environment Variables

  • JAVA_HOME
  • GRADLE_USER_HOME

Note, the VS Code settings take precedence over the environment variables.

Use an environment manager like direnv to set project specific environment variables

Compatibility with the Java language support extension

VS Code Settings

This extension supports the following settings which are contributed by the Java language support extension:

  • java.home: Absolute path to JDK home folder used to launch the Gradle daemons
  • java.import.gradle.java.home: Absolute path to JDK home folder used to launch the Gradle daemons (if set, this value takes precedence over java.home)
  • java.import.gradle.user.home: Setting for GRADLE_USER_HOME
  • java.import.gradle.jvmArguments: JVM arguments to pass to Gradle
  • java.import.gradle.wrapper.enabled: Enable/disable the Gradle wrapper
  • java.import.gradle.version: Gradle version, used if the Gradle wrapper is missing or disabled

Class References

There are cases where Gradle tasks will generate Java classes. To ensure these Java classes are indexed correctly by the Java language server, you need to ensure the paths are added to the .classpath, and this is typically achieved using Gradle sourceSets.

Once you've configured your sourceSets correctly, follow these steps:

  1. Generate your classes by running the relevant Gradle Task
  2. Force the Language Server to index the generated classes by right-clicking on build.gradle and selecting Update project configuration.

At this point the Gradle sourceSet paths will be added to the .classpath and the Language Server will automatically update references when those classes change.

Extension API

This extension provides an API which can be used by 3rd-party vscode extensions.

πŸ‘‰ Extension API

Troubleshooting

View logs by selecting "Gradle Tasks" in the output panel Gradle extension output
Set the "gradle.debug" setting to "true" to view debug logs in the output panel Gradle extension debug output
Task output will be shown in the Terminal panel Gradle task output
Task output is truncated

The integrated terminal has a limited buffer size and will not show the full output for tasks that generate a large output. Increase the terminal buffer size in your settings, for example:

{
  "terminal.integrated.scrollback": 5000
}
"No connection to the gradle server. Try restarting the server"

This error means the Gradle Task server has stopped, or there was an error starting it. Click on "Restart Server" to restart it.

If you continue to get this error, view the task error messages by selecting "Gradle Tasks" in the output panel.

The task server is started using a shell script generated by CreateStartScripts. The script uses #!/usr/bin/env sh and is as portable as the Gradle wrapper script. If there are any problems executing the start script then it's likely an issue either with your PATH, or Java is not installed.

PATH problems

The following error demonstrates a typical issue with your PATH:

env: sh: No such file or directory
The terminal process terminated with exit code: 127

Use the following task to debug your shell environment within vscode:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Print task shell info",
      "type": "shell",
      "command": "echo \"Path: $PATH \nShell: $SHELL\"",
      "problemMatcher": []
    }
  ]
}

Fixing your $PATH

Check your dotfiles (eg ~/.bash_profile, ~/.bashrc, ~/.zshrc) and fix any broken PATH exports. See Issues with environment vars below for more information.

Java path problems

You might see an error like:

ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

The start script should find the path to Java in the usual locations. If you get this error it suggests an issues with your $PATH or you simply haven't installed Java. Run the Gradle wrapper script (eg ./gradlew tasks) to debug further, or see Issues with environment vars below for more information.

Shell environment

Another potential problem is that the PATH or JAVA_HOME environment vars have been defined within .bashrc. See Issues with environment vars below for more information.

Issues with environment vars

(The following is only relevant for MacOS & Linux.)

The Gradle Server is launched inside a non-interactive non-login shell, which loads the profile script (not the rc script). This means the shell will not load, for example, ~/.bashrc. If you've defined PATH or other environment variables within ~/.bashrc, they will not be available for the server startup script and not be available to Gradle.

Generally, environment settings should be defined in in ~/.bash_profile, but as a workaround, you can load ~/.bashrc from within ~/.bash_profilefor example:

if [ -r ~/.bashrc ]; then
  source ~/.bashrc
fi
"Orphaned" Java processes after quitting VS Code

You might notice some Java processes are not closed after existing VS Code. These processes are the Gradle Daemons that Gradle spawns. This is the default behaviour of Gradle.

You'll have N processes per Gradle version. Eventually Gradle will shut them down. Read more about the Gradle Daemon.

Incompatibility with other extensions

This extension is incompatible with the following extensions:

The reason for the incompatibility is due to the extensions providing the same tasks types (gradle) with different task definitions.

Support

Contributing

Refer to CONTRIBUTING.md for instructions on how to run the project.

πŸ‘‰ Architecture Overview

Credits

  • Originally forked from Cazzar/vscode-gradle
  • Inspired by the built-in npm extension
  • Thanks to @hanct for providing feature suggestions and BETA testing
  • Thanks to @dcermak for providing inspiration to write some unit tests
  • Thanks to all who have submitted bug reports and feedback

Related Extensions

Check out vscode-spotless-gradle which formats your source files using Spotless & Gradle.

Release Notes

See CHANGELOG.md.

License

See LICENSE.md.

About

A VS Code extension to list & run Gradle tasks

https://marketplace.visualstudio.com/items?itemName=richardwillis.vscode-gradle

License:MIT License


Languages

Language:TypeScript 77.1%Language:Java 20.1%Language:Shell 1.9%Language:Kotlin 0.4%Language:JavaScript 0.4%