How do I create an an executable jar in Spring Boot? - spring

I have a Spring MVC project using Spring Boot 1.5.2 with Gradle Buildship in Spring Tool Suite.
How do I create a JAR file from my source only which will run in another server and download the required dependencies there?
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')
}

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

Related

unable to run the springboot jar from command line

My requirement is to build a spring boot jar in standalone tomcat executable.I am not able to run the jar from command line.
I am using gradle
buildscript {
ext {
springBootVersion = '1.4.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile project(':testProject')
implementation('org.springframework.boot:spring-boot-starter-web')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
configurations {
all*.exclude module: 'spring-boot-starter-logging'
}
jar {
manifest {
attributes 'Implementation-Title': 'SpringAppApplication',
'Implementation-Version': version
}
}
running java -jar target/SpringAppApplication-0.0.1-SNAPSHOT.jar
I get error Invalid or corrupt jarfile SpringApp-0.0.1-SNAPSHOT.jar
.Tried giving the complete path also.

Using spring-boot Gradle plugin but getting spring boot version from dependency management

I have a Gradle setup where I currently need to hard code the spring-boot version. Is it possible to do this with dependency management instead? The issue occurs in my project module that contains the actual code. There I need a call to :
springBoot {
requiresUnpack = ["com.example:util"]
}
In order to do that I need to apply the spring-boot plugin. For that plugin to be available I need to have a dependency to "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}". And for that I need to specify the springBootVersion hard coded...
This is my parent gradle file:
buildscript {
ext {
springBootVersion = '1.4.0.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath 'io.spring.gradle:dependency-management-plugin:0.6.0.RELEASE'
}
}
plugins {
id "io.spring.dependency-management" version "0.6.0.RELEASE"
}
group 'test'
repositories {
mavenCentral()
}
allprojects {
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
// Note that these two BOM's are not always easy to combine, always define cloud first and try to find versions with same version of spring-boot when upgrading.
mavenBom 'org.springframework.cloud:spring-cloud-starter-parent:Brixton.SR5'
mavenBom 'io.spring.platform:platform-bom:2.0.7.RELEASE'
}
}
}
and this is my module gradle file:
group = "test"
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
targetCompatibility = 1.8
// Temporary fix for jersey
springBoot {
requiresUnpack = ["com.example:util"]
}
If I remove the dependency to spring-boot-gradle-plugin in parent gradle file, then I cannot use the apply plugin: 'spring-boot' in the module gradle file and without that the gradle DLS method springBoot { is not defined...
Can I achieve this without having to hard code springBootVersion = '1.4.0.RELEASE'in my parent gradle file?

Static files deployment in Spring boot using Gradle

I have a spring boot application using Gradle build system. My build.gradle is given below:
buildscript {
ext {
springBootVersion = '1.2.7.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath('io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE')
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.security:spring-security-web")
compile("org.springframework.security:spring-security-config")
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
//testCompile('org.springframework.boot:spring-boot-starter-test')
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.7'
}
I then run from cmd, gradle war and I get the war file which I can successfully deploy to a tomcat instance.
I have a folder /static to serve the html as well as js files that will use the spring rest service.
The war package does not seem to contain these static files. So when I request the home page (index.html) spring returns 404 error.
I tried manually copying the static folder to various locations in webapps folder of tomcat, which did not help.
I can still access the REST end points.
Could some one please help on this?

How to use Gradle with Spring Boot to get Gosling Release Train of Spring data?

How can I get the latest Gosling release train into my Gradle build file?
I used to be using the 1.1.9.RELEASE group for most of my dependencies. Now I need to fix the problem with RepositoryRestMvcConfiguration mentioned here and to do so I am trying to upgrade to the latest release of spring Data, which has fixed the bug according to the github issue I linked to.
When I added the Gosling release train dependencies I also removed the spring boot starters for spring-data-jpa and spring-data-rest thinking I might have dependency conflicts. Doing this pulled in the new jar files but now I am getting cannot find symbol errors on all my javax.persistence annotations.
Can I use the Gosling release train with the spring boot starters or do I have to figure out how to pull in all spring boot dependencies manually in order to use Gosling?
I am using Gradle 2.3.10 on Mac OS X Yosemite.
New Code
buildscript {
ext {
springBootVersion = '1.3.0.M3'
}
repositories {
jcenter()
mavenCentral()
//maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:0.5.0.RELEASE"
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: "io.spring.dependency-management"
ext {
springVersion = '4.1.6.RELEASE'
springDataVersion = 'Gosling-RELEASE'
}
dependencyManagement {
imports {
mavenBom "org.springframework:spring-framework-bom:${springVersion}"
mavenBom "org.springframework.data:spring-data-releasetrain:${springDataVersion}"
}
}
jar {
baseName = 'my-data-api'
version = '0.0.1'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
jcenter()
mavenCentral()
//maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator:1.3.0.M3")
compile("org.springframework.boot:spring-boot-starter-aop:1.3.0.M3")
compile 'org.springframework.data:spring-data-jpa'
compile 'org.springframework.data:spring-data-rest-webmvc'
compile("org.springframework.boot:spring-boot-starter-web:1.3.0.M3")
compile("org.springframework.boot:spring-boot-starter-jdbc:1.3.0.M3")
compile('org.antlr:stringtemplate:4.0.2')
compile('org.apache.commons:commons-lang3:3.0')
compile('commons-io:commons-io:2.4')
compile('com.ingres.jdbc:iijdbc:10.0-4.0.5')
testCompile("org.springframework.boot:spring-boot-starter-test:1.3.0.M3")
}
Old code
buildscript {
ext {
springBootVersion = '1.3.0.M2'
}
repositories {
jcenter()
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'my-data-api'
version = '0.0.1'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
jcenter()
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator:1.2.0.RC2")
compile("org.springframework.boot:spring-boot-starter-aop:1.1.9.RELEASE")
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.1.9.RELEASE")
compile("org.springframework.boot:spring-boot-starter-web:1.1.9.RELEASE")
compile("org.springframework.boot:spring-boot-starter-data-rest:1.1.9.RELEASE")
compile("org.springframework.boot:spring-boot-starter-jdbc:1.1.9.RELEASE")
compile('org.antlr:stringtemplate:4.0.2')
compile('org.apache.commons:commons-lang3:3.0')
compile('commons-io:commons-io:2.4')
compile('com.ingres.jdbc:iijdbc:10.0-4.0.5')
testCompile("org.springframework.boot:spring-boot-starter-test:1.1.9.RELEASE")
}
Edit:
If I put a javax persistence dependency in my build.gradle then I can successfully build and use RepositoryRestConfigurerAdapter, but I get runtime problems with dependencies missing for my entityManagerFactory
If you are already using milestone versions of Spring Boot, I suggest you switch to M5. It includes Gosling-RELEASE Spring Data.

Spring Boot Gradle add Native library failed (java.lang.UnsatisfiedLinkError)

I am trying to add a native library( .dll file) to a basic spring gradle project.
I have tried many different settings and all of them didn't work in a basic java project , I have successfully ran that dll file by adding VM argument: java.library.path gradle
here's the script:
buildscript {
ext {
springBootVersion = '1.2.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'messaging'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-aop")
compile("org.springframework.boot:spring-boot-starter-amqp")
compile("org.springframework:spring-messaging")
compile('javax.inject:javax.inject:1')
compile('com.google.guava:guava:11.0.2')
compile('org.codehaus.jackson:jackson-core-asl:1.1.0')
compile('org.codehaus.jackson:jackson-mapper-asl:1.9.13')
testCompile("org.springframework.boot:spring-boot-starter-test")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
I have tried:
add VM arguments on eclipse
add jvm properties
add gradle system property ( can not succeeded to add that parameter)
I couldn't understand why but when I follow the steps below, it worked:
1. I changed the java package name
2. rebuild the project with gradle
3. run the project

Resources