Search code examples
javagradlegoogle-sheetsgoogle-apipackaging

How to make Gradle + Google Sheets API + Java into a single downloadable program?


As of now I have made a chatbot application by combining Java and Google Sheets using the Google API which requires me to use Gradle. So far I have only been able to run the program through Gradle using the terminal.

In my end result I want to be able to send this program to someone and they should be able to install it or run it as easy as possible without having to run it through the terminal.

I suspect that the person might need to download Gradle to run the file since it is needed for the Google API. So I think the best way to do this is to send a zip file (or something else) with the Gradle Installer and the java file and somehow make it install everything automatically. Is this possible?

This is how my build.gradle file looks like

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'SheetsQuickstart'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'

buildscript {
      repositories {
          jcenter()
      }
      dependencies {
          classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
      }
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.api-client:google-api-client:1.23.0'
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
    compile 'com.google.apis:google-api-services-sheets:v4-rev516-1.23.0'
    compile(group: 'org.springframework', name: 'spring-core', version:'4.3.11.RELEASE') 
}

jar {
    doFirst {
        manifest {
            if (!configurations.compile.isEmpty()) {
                attributes(
                        'Class-Path':configurations.compile.collect{it.toURI().toString()}.join(' '),
                        'Main-Class': 'SheetsQuickstart')
            }
        }
    }
}

Solution

  • Assuming google sheets api is a dependency within gradle, cant you just build a jar file through gradle build then run the jar as an executable file?

    build.gradle file example:

      version '1.0-SNAPSHOT'
      apply plugin: 'java'
      apply plugin: 'application'
      apply plugin: 'com.github.johnrengelman.shadow'
    
      group = 'your.package.path.here'
      version = '0.0.1-SNAPSHOT'
    
      mainClassName = 'your.package.path.here.MainClassName'
    
      buildscript {
          repositories {
              jcenter()
          }
          dependencies {
              classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
          }
      }
    
    
      dependencies {
          compile(group: 'org.springframework', name: 'spring-core', version:'4.3.11.RELEASE') 
      }
    
      repositories {
          mavenCentral()
     }
    
     jar {
     doFirst {
        manifest {
            if (!configurations.compile.isEmpty()) {
                attributes(
                        'Class-Path':configurations.compile.collect{it.toURI().toString()}.join(' '),
                        'Main-Class': 'your.package.path.here.MainClassName')
    
            }
        }
    }
    

    Should be enough for a reg min build file, then add your dependencies.