Static files deployment in Spring boot using Gradle - spring

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?

Related

Generating war for spring boot application

I have been following the tutorial provided by spring and cannot seem to understand why my .war file is empty. Click here to see/download the code. The end goal is to download this file, build a .war file and then deploy it to my tomcat server. I've added the following to the build.gradle
apply plugin: "war"
but this only generates a war that contains zero class files.
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle
plugin:1.5.3.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
// end::actuator[]
// tag::tests[]
testCompile("org.springframework.boot:spring-boot-starter-test")
// end::tests[]
}
For the tutorial code, you need to do two things:
war plugin for packaging spring-boot ref:
apply plugin: 'war'
war {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
Create a deployable war file spring-boot ref:
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
And also specify the tomcat dependency as provided:
dependencies {
// …
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
// …
}
Here is my commit reference and result:

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

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

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

Spring Batch with Grails

im currently using grails 3.0.3 along with gradle 2.5 and hoping to generate a plugin that will use Spring Batch. I've just created a plugin via the command line, and update the build.gradle with just the
compile "org.springframework.boot:spring-boot-starter-batch"
However the batch folder does not seem to downloaded to the local .m2 cache and therefore the import org.springframework.batch libs not available.
The following is the build.gradle. Just wondering if anybody could suggest the reasons why?
Thanks
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
}
}
plugins {
id "io.spring.dependency-management" version "0.5.2.RELEASE"
id "com.jfrog.bintray" version "1.2"
}
version "0.1-SNAPSHOT"
group "org.grails.plugins"
apply plugin: 'maven-publish'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "spring-boot"
apply plugin: "org.grails.grails-plugin"
apply plugin: "org.grails.grails-gsp"
// Used for publishing to central repository, remove if not needed
apply from:'https://raw.githubusercontent.com/grails/grails-profile-repository/master/profiles/plugin/templates/grailsCentralPublishing.gradle'
apply from:'https://raw.githubusercontent.com/grails/grails-profile-repository/master/profiles/plugin/templates/bintrayPublishing.gradle'
ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.grails.org/grails/core" }
}
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}
dependencies {
provided 'org.springframework.boot:spring-boot-starter-logging'
provided "org.springframework.boot:spring-boot-starter-actuator"
provided "org.springframework.boot:spring-boot-autoconfigure"
provided "org.springframework.boot:spring-boot-starter-tomcat"
provided "org.grails:grails-web-boot"
provided "org.grails:grails-dependencies"
provided 'javax.servlet:javax.servlet-api:3.1.0'
testCompile "org.grails:grails-plugin-testing"
console "org.grails:grails-console"
//compile "org.springframework.boot:spring-boot-starter-batch:1.2.5.RELEASE"
compile "org.springframework.boot:spring-boot-starter-batch"
}
task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}

spring boot jsps not getting rendered

My build gradle looks like this
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-milestone" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC5")
}
}
configurations {
jasper
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
war {
baseName = 'foo-serving-web-content'
version = '0.1.1'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-milestone" }
}
dependencies {
compile("org.springframework:spring-webmvc:4.0.0.M2")
compile("org.springframework.boot:spring-boot-starter-web")
compile("javax.servlet:javax.servlet-api:3.1.0")
compile("javax.servlet:jstl:1.1.2")
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
Now the thing is my jsps are not getting rendered, just plain XMLs.
I found this post someone had issues but they are using maven.
JSP file not rendering in Spring Boot web application
is there something wrong with my build.gradle ?
A few things appear wrong:
you don't need "spring-webmvc" as an explicit dependency, and you are using a very old version. Just leave that out and you'll get 4.0.3.
you haven't included Jasper (despite the fact that it looks like you meant to with the "jasper" configuration) - example here (it's maven but you'll get the picture).
You haven't marked the tomcat dependencies as "providedRuntime" (see here for example, so I assume you don't need a deployable WAR.
You probably ought to address all of those points, but you can probably get something to work if you do a subset.

Resources