Gradle not creating a Tomcat deployable war - spring

I'm running into an issue with building a war with Gradle 4.8.1. Here is the build.gradle:
buildscript {
ext {
springBootVersion = '2.0.3.RELEASE'
}
repositories {
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
apply plugin: 'idea'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
archivesBaseName = 'sample'
//war {
// archiveName = 'sample.war'
// group = 'my.group'
// version = '2.0.0-SNAPSHOT'
//}
sourceCompatibility = 1.8
repositories {
jcenter()
maven { url "${nexusUrl}/content/groups/public" }
}
dependencies {
// Spring
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.springframework.boot:spring-boot-starter-log4j2'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
compile 'org.springframework.boot:spring-boot-starter-web'
testCompile 'org.springframework.boot:spring-boot-starter-test'
// Lombok
compile "org.projectlombok:lombok:1.18.0"
// Data
compile ('org.postgresql:postgresql') {
exclude module: 'slf4j-simple'
}
// Http
compile "org.apache.httpcomponents:httpclient:4.5.5"
}
configurations {
all*.exclude module: 'spring-boot-starter-logging'
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "${nexusUrl}/content/groups/public/") {
authentication(userName: nexusUsername, password: nexusPassword)
}
snapshotRepository(url: "${nexusUrl}/content/repositories/snapshots") {
authentication(userName: nexusUsername, password: nexusPassword)
}
pom.version = '2.0.0-SNAPSHOT'
pom.artifactId = 'sample'
pom.groupId = 'my.group'
}
}
}
I also tried removing the 'maven' plugin, archivesBaseName, and the uploadArchive task while uncommenting the war task and I get the same result. When using uploadArchive the war deploys to the nexus server fine and I get no errors. When deploying the war to tomcat (in both instances) tomcat 7 and 8 throw no errors and I receive no logs from either catalina or the project, although archiveName inside the war task does not properly rename the war. I have tried this on two other machines/tomcat instances with the same result. When building this as a fat jar, or running this in IntelliJ, everything works as expected (including the logging).
Any help or direction would be greatly appreciated.

It sounds like it is not initializing the servlet at all. Make sure to extend the SpringBootServletInitializer for WAR deployment.
Spring Boot Docs - Traditional Deployment

I was also facing the same issue. The issue is Spring application doesnt get initialized when the war is deployed on tomcat.
After a lot of struggle, I figured out that I needed to extend SpringBootServletInitializer in my application. So my effective code looks like
public class SyncApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SyncApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SyncApplication.class, args);
}
}
Looks like this SpringBootServletInitializer directs war plugins generate bootstrapping code while building the war, and thus spring context is initialized while deploying the app.

Related

How to configure 'mainClassName' for Spring Boot Application in a Kotlin Gradle multi-module project?

I have a Gradle multi-module project in Kotlin using Spring Boot in some of the modules.
I use the 'org.springframework.boot' and 'io.spring.dependency-management' plugin in the project's root build.gradle which besides other things provide the bootRun task.
AFAIK I need to configure the mainClassName (or entry point) like this:
springBoot {
mainClassName = 'de.shinythings.hexagon.HexagonApplicationKt'
}
While this works in the subproject that contains the actual Spring Boot application it does not work in the root project.
I want to be able to configure it in the root project because I want to be able to run my project with ./gradlew bootRun instead of ./gradlew configuration:bootRun.
Other tasks like ./gradlew build or ./gradlew test work fine.
Here is the root project's build.gradle (link):
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.10'
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
implementation project(':configuration')
}
springBoot {
mainClassName = 'de.shinythings.hexagon.HexagonApplicationKt'
}
subprojects {
group = 'de.shinythings.hexagon'
version = '0.0.1-SNAPSHOT'
repositories {
mavenCentral()
}
ext {
mockKVersion = '1.10.2'
}
apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}
dependencyManagement {
imports {
mavenBom('org.springframework.boot:spring-boot-dependencies:2.3.4.RELEASE')
}
}
compileKotlin {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
freeCompilerArgs = ["-Xjsr305=strict"]
allWarningsAsErrors = true
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
freeCompilerArgs = ["-Xjsr305=strict"]
allWarningsAsErrors = true
}
}
}
In the subproject/module this setting works (link) :
plugins {
id 'org.springframework.boot'
id 'org.jetbrains.kotlin.plugin.spring' version '1.4.10'
}
dependencies {
implementation project(':common')
implementation project(':application')
implementation project(':adapters:persistence')
implementation project(':adapters:web')
implementation 'org.springframework.boot:spring-boot-starter'
implementation('com.h2database:h2')
}
springBoot {
mainClassName = 'de.shinythings.hexagon.HexagonApplicationKt'
}
What do I have to do to configure the mainClassName is the root's project build.gradle?

Compilation fails using Gradle and Kotlin

I am going through the examples in Kotlin in Action book. The gradle buid script is as follows:
group 'kotlin-in-action'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.2-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
apply plugin: 'java'
dependencies {
testCompile 'junit:junit:4.12'
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile "junit:junit:4.12"
compile 'junit:junit:4.12'
compile 'junit:junit:4.12'
}
sourceSets {
main.kotlin.srcDirs += 'src'
}
All the scripts compile except for two classes that uses junit.
package ch06.ex1_8_1_LateinitializedProperties
import org.junit.Before
import org.junit.Test
import org.junit.Assert
class MyService {
fun performAction(): String = "foo"
}
class MyTest {
private var myService: MyService? = null
#Before fun setUp() {
myService = MyService()
}
#Test fun testAction() {
Assert.assertEquals("foo",
myService!!.performAction())
}
}
The compiler says it can't find junit. I have tried adding the jar files in IntelliJ, but this has not resolved the problem. The jar files I have added are junit and hamscrest-core. This is all version 4.12.
I have resolved this. I had to add the Junit and hamcrest-core jar files from the mavan depository.

Spring bootRun not available from within Eclipse gradle

I am just starting out with Spring and have successfully setup and am running the RESTful tutorial using gradle buildship within Eclipse.
https://spring.io/guides/gs/rest-service/#initial
However, one thing I cannot get working within Eclipse is it says you can run the application from gradle rather than the jar directly by the command:
gradle bootRun
but bootRun is not a task created by the gradle script, only 'run' (which does not work). All the other entries, like 'build', etc. are there.
It's not a big deal as I can run the jar from the command line but I'd like to run it from within Eclipse (I do not have gradle installed either, just buildship).
thanks.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
You don't need 'gradle bootRun'
Just run that as a main method:
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

Missing schema module for spring-cloud-stream

Trying to use the following example from Spring Docs
#Bean
public MessageConverter userMessageConverter() throws IOException {
AvroSchemaMessageConverter avroSchemaMessageConverter {
return new AvroSchemaMessageConverter(MimeType.valueOf("avro/bytes");
}
Using Gradle as follows
buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'org.springframework.boot'
dependencies {
compile('org.springframework.cloud:spring-cloud-stream')
compile('org.springframework.cloud:spring-cloud-starter-stream-kafka')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR2"
}
}
Gradle is loading the correct version of spring-cloud-streams 1.1.0.RELEASE but it does not match with the Github Repo. The artifact is missing the org.springframework.cloud.stream.schema package/source.
Am I missing something here?
The artifact org.springframework.cloud:spring-cloud-starter-stream-kafka brings in spring-cloud-stream, spring-cloud-stream-codec and related dependencies like spring-integration. You would have to explicitly define org.springframework.cloud:spring-cloud-stream-schema.
Also, you don't need to specify 'org.springframework.cloud:spring-cloud-stream' as it will be part of org.springframework.cloud:spring-cloud-starter-stream-kafka via org.springframework.cloud:spring-cloud-stream-binder-kafka.

spring boot war deployment to tomcat with status:OK But gives 404 on access

My configration class looks as follow
#Configuration
#EnableAutoConfiguration
#ComponentScan(basePackages = "com.projectx")
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
Also build.gradle file looks like this
apply plugin: 'java'
sourceCompatibility=1.8
targetCompatibility=1.8
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
mainClassName = 'com.projectx.data.config.Application'
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-release" }
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.1.RELEASE")
}
}
war {
baseName = 'gs-accessing-data-jpa'
version = '0.1.0'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/libs-release" }
maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}
So wherever i am deploying this project to tomcat it is showing following error. As I have mate all requirement to convert jar to war in spring not sure what is wrong this my project.
It gives 404 NOT FOUND.(I am using STS 3.6.0 with Gradle plugin)

Resources