title: In a Gradle Spring Boot project, how can I declare dependencies to only be used when running locally?
body: <p>I have a Spring Boot application built with Gradle that connects to a database.  When running locally it is oftentimes more convenient to run within an in-memory database (in this case, <a href="http://www.h2database.com/html/main.html" rel="nofollow noreferrer">h2</a>), rather than connecting to a real instance of the database.  To that end, the application declares a custom <code>BootRun</code> task for running the application locally.  How can I make sure the h2 dependency is available when running locally, but is not included in the generated Spring Boot jar?</p>
  
  <h1>Files:</h1>
  
  <h3>build.gradle</h3>
  
  <pre><code>import org.springframework.boot.gradle.tasks.run.BootRun
  
  buildscript {
      repositories {
          mavenCentral()
      }
      dependencies {
          classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
      }
  }
  
  plugins {
      id 'java'
      id 'org.springframework.boot' version '2.1.1.RELEASE'
      id "io.spring.dependency-management" version "1.0.6.RELEASE"
  }
  
  group 'test'
  version '1.0-SNAPSHOT'
  sourceCompatibility = 1.8
  
  repositories {
      mavenCentral()
  }
  
  dependencies {
      compile "org.springframework.boot:spring-boot-starter-web"
      compile "org.springframework.boot:spring-boot-starter-jdbc"
  
      // compile "..." // Dependency for the real, non-in memory, database goes here
  
      compile "com.h2database:h2" // How can I make sure this isn't included in the resulting jar?
  }
  
  task bootRunLocal(type: BootRun, group: ApplicationPlugin.APPLICATION_GROUP, dependsOn: classes) {
      main = 'test.BootApplication'
      classpath = sourceSets.main.runtimeClasspath
  
      systemProperty "spring.profiles.active", "local-db-h2"
  }
  </code></pre>
  
  <h3>application.yml</h3>
  
  <pre><code>spring:
    datasource:
      # Default and non-in-memory datasource configuration goes here
  ---
  spring:
    profiles: local-db-h2
    datasource.url: jdbc:h2:mem:testdb;MODE=Oracle
    datasource.platform: h2_local
  </code></pre>
  
  <h3>test/BootApplication.java</h3>
  
  <pre><code>package test;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.jdbc.core.JdbcTemplate;
  
  @SpringBootApplication
  public class BootApplication implements CommandLineRunner {
  
      @Autowired
      private JdbcTemplate jdbcTemplate;
  
      public static void main(String[] args) {
          SpringApplication.run(BootApplication.class, args);
      }
  
      @Override
      public void run(String... args) {
          jdbcTemplate.query("SELECT 1", row -&gt; {});
      }
  }
  </code></pre>