2

I have a Spring MVC project using Spring Boot 1.5.2 with Gradle Buildship in Spring Tool Suite.

  1. How do I create a JAR file from my source only which will run in another server and download the required dependencies there?
  2. How do I create a fat JAR with all of the source files and the dependencies?

My gradle.build file is:

buildscript {
    ext {
        springBootVersion = '1.5.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
mainClassName = "com.jtv.elastic.mvc.ElasticSpringApplication"    
sourceCompatibility = 1.8
repositories {
    mavenCentral()
}    
dependencies {
    compile('org.springframework.boot:spring-boot-starter-web') 
    testCompile('org.springframework.boot:spring-boot-starter-test')
}
khateeb
  • 5,265
  • 15
  • 58
  • 114
  • Spring Boot already creates a fat JAR with all necessary dependencies, which is executable. So what problem exactly do you have? – dunni Mar 27 '17 at 12:33
  • @dunni In which folder will find the fat JAR? Also please answer the first part of the question. – khateeb Mar 27 '17 at 12:49
  • 1
    You find the JAR in the output folder of Gradle (i'm not sure if it's called build or target, but if you look in your project folder you should find it). Regarding the first question: I don't exactly understand what you want to achieve. Gradle already downloads all of your dependencies. If you want to do that within your application, you have to build it your own (and mess around with classloaders etc., so i don't know any reasons why someone would want that). – dunni Mar 27 '17 at 12:52
  • @dunni I want to create a JAR containing just my source files. When I run in a server, Gradle will download the dependencies if they are not present in the server and then run the app. This will reduce the size of the JAR which does not need to contain all of the dependencies. – khateeb Mar 27 '17 at 13:05
  • 1
    Then just remove the spring-boot-gradle-plugin from your build.gradle and Gradle will only build a JAR with your classes. – dunni Mar 27 '17 at 13:06
  • @dunni If I run it using `java -jar`, will Gradle automatically download the dependencies? – khateeb Mar 27 '17 at 13:08
  • 1
    No, of course not. Gradle is a build system and not intended to be used in a runtime environment. That's why Spring Boot packages all necessary dependencies in the JAR by default. If you want to use Gradle on your server, you have to write the scripts yourself. – dunni Mar 27 '17 at 13:23

1 Answers1

0

You should use bootRepackage task for gradle. More information here and here

kchrusciel
  • 143
  • 7