Build executable .jar from spring boot 1.5.3 with gradle - java

I have a spring boot project (1.5.3 release) and using gradle 4.4.
I'll build a .jar executable for install a service on linux server.
but I'm having problems generating the executable .jar file.
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
//springBootVersion = '2.0.3.RELEASE'
}
repositories {
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: 'org.springframework.boot'
apply plugin: 'application'
springBoot {
executable = true
}
group = 'com.mygroup'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile("org.springframework.boot:spring-boot-starter-web")
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4')
compile group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.11.371'
compile group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.11.370'
runtime('org.springframework.boot:spring-boot-devtools')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
testCompile('com.jayway.jsonpath:json-path')
}
How i can generate the .jar?

There missing dependency for boot jar.
add this in build.gradle:
bootJar {
baseName = 'application-name'
version = '1.0.0'
}
command in terminal:
gradle bootjar

you can try this:
plugins {
id 'java'
id "maven-publish"
}
publishing {
publications {
maven(MavenPublication) {
groupId = 'com.your.project'
version = '0.0.1-SNAPSHOT'
from components.java
}
}
}
repositories {
mavenCentral()
}
and run command
gradle publishToMavenLocal
jar file will be generated in local maven repository.

Related

Spring Boot (Gradle) app deployed to Google App Engine returns 404 Not Found

I have a Spring Boot (2.1.7) (Gradle project) that I want to deploy to Google App Engine.
I am able to successfully deploy the app (using the documentation found here) but when visiting the app url it returns a 404 Not Found screen:
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>404 Not Found</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Not Found</h1>
</body></html>
Here's what my build.gradle file looks like:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.1.0'
classpath 'org.akhikhl.gretty:gretty:+'
}
}
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'org.akhikhl.gretty'
apply plugin: 'com.google.cloud.tools.appengine'
group = 'com.company'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
configurations.all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
exclude group: 'org.slf4j', module: 'jul-to-slf4j'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jetty'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
providedCompile 'com.google.appengine:appengine:+'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
gretty {
httpPort = 8080
contextPath = '/'
servletContainer = 'jetty9' // What App Engine Flexible uses
}
appengine {
deploy {
version = "3"
projectId = "apprealtest"
stopPreviousVersion = true // default - stop the current version
promote = true // default - & make this the current version
}
}
Here is what my {projectRoot}/src/main/appengine/app.yaml file looks like:
runtime: java
env: flex
runtime_config: # Optional
jdk: openjdk8
handlers:
- url: /.*
script: this field is required, but ignored
manual_scaling:
instances: 1
This is what my {projectRoot}/src/main/webapp/WEB-INF/appengine-web.xml file looks like:
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<runtime>java8</runtime>
<threadsafe>true</threadsafe>
</appengine-web-app>
This is what the {projectRoot}/src/main/java/com/company/hello/HelloApplication.java looks like:
package com.company.hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
This is what the {projectRoot}/src/main/java/com/company/hello/HelloController.java looks like:
package com.company.hello;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloController {
#GetMapping("/")
public String get() {
return System.currentTimeMillis() + "";
}
#GetMapping("/greeting")
public String getGreeting() {
return "Greetings!";
}
}
I'm making the call to https://apprealtest.appspot.com and https://apprealtest.appspot.com/greeting.
What am I doing wrong?
To deploy it in GAE Standard you can download this official SpringBoot for App Engine Standard sample.
The instructions are for maven but to do it with Gradle you can simply add this build.gradle file:
buildscript { // Configuration for building
repositories {
jcenter() // Bintray's repository - a fast Maven Central mirror & more
mavenCentral()
}
dependencies {
classpath 'com.google.cloud.tools:appengine-gradle-plugin:1.+' // Latest 1.x.x release
}
}
repositories { // repositories for Jar's you access in your code
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots' // SNAPSHOT repository (if needed)
}
mavenCentral()
jcenter()
}
apply plugin: 'java' // standard Java tasks
apply plugin: 'war' // standard Web Archive plugin
apply plugin: 'com.google.cloud.tools.appengine' // App Engine tasks
apply plugin: 'maven-publish'
dependencies {
compile 'com.google.appengine:appengine-api-1.0-sdk:+' // Latest App Engine Api's
compile 'org.springframework.boot:spring-boot-starter-web:2.1.1.RELEASE'
testCompile 'org.springframework.boot:spring-boot-starter-test:2.1.1.RELEASE'
providedCompile 'org.slf4j:jul-to-slf4j:1.7.25'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
}
configurations.all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
exclude group: 'org.slf4j', module: 'jul-to-slf4j'
}
group = 'com.google.appengine.demos'
version = '0.0.1-SNAPSHOT'
description = 'springboot-appengine-standard'
sourceCompatibility = '1.8'
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
And you can deploy it with gradle appengineDeploy.
I tested it myself and it worked. If you want to do it in GAE Flexible the corresponding sample would be this one which also has instructions for Maven so you would have to do the necessary migration to Gradle.
As I have not found a complete example of spring boot with the Gradle build tool.
Added a complete example with updated spring boot version 2.2.3 and AppEngine Gradle plugin 2.2.0.
buildscript {
repositories {
mavenCentral()
maven{
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.2.0'
}
}
plugins {
id 'java'
id 'war'
id 'org.springframework.boot' version '2.2.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
configurations.all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
exclude group: 'org.slf4j', module: 'jul-to-slf4j'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compile "com.google.appengine:appengine-api-1.0-sdk:+"
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
providedCompile "com.google.appengine:appengine-api-stubs:+"
providedCompile "com.google.appengine:appengine-testing:+"
providedCompile 'org.slf4j:jul-to-slf4j:1.7.25'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile group: 'net.bytebuddy', name: 'byte-buddy', version: '1.10.3'
}
apply plugin: "com.google.cloud.tools.appengine"
appengine { // App Engine tasks configuration
run { // local (dev_appserver) configuration (standard environments only)
jvmFlags = ['-Ddatastore.backing_store=../../src/main/webapp/WEB-
INF/appengine-generated/local_db.bin', '-Dappengine.fullscan.seconds=5']
port = 8812 // default
}
deploy { // deploy configuration\
stopPreviousVersion = false // default - stop the current version
promote = false // default - & make this the current version
version = 'pr'
projectId = ''
}
}
This repo has a complete example and the test case for its controller. hope it would help.

Gradle couldn't locate the Spring core jar

i am spring boot using gradle build. till yesterday gradle build was run smoothly. But today i got this following error ;
Could not resolve all artifacts for configuration ':classpath'.
Could not find spring-core.jar (org.springframework:spring-core:5.2.0.BUILD-SNAPSHOT:20190328.215418-203).
Searched in the following locations:
https://repo.spring.io/snapshot/org/springframework/spring-core/5.2.0.BUILD-SNAPSHOT/spring-core-5.2.0.BUILD-20190327.205120-195.jar
Could not find spring-jcl.jar (org.springframework:spring-jcl:5.2.0.BUILD-SNAPSHOT:20190328.215418-203).
Searched in the following locations:
https://repo.spring.io/snapshot/org/springframework/spring-jcl/5.2.0.BUILD-SNAPSHOT/spring-jcl-5.2.0.BUILD-20190327.205120-195.jar
this is my build.gradle file
buildscript {
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/snapshot' }
maven { url 'https://repo.spring.io/milestone' }
maven {url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.3.RELEASE")
}
}
plugins {
id 'org.springframework.boot' version '2.2.0.BUILD-SNAPSHOT'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'idea'
group = 'me.namila'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/snapshot' }
maven { url 'https://repo.spring.io/milestone' }
jcenter()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
When visiting the relevant url,what i can see is gradle is searching for 27th march build (spring-jcl-5.2.0.BUILD-20190327.205120-195.jar) while the server has the 28th build spring-jcl-5.2.0.BUILD-20190328.164750-201.jar. how to fix this error? i have added buildscript repositories too. any suggesions?
As Antoniossss said in the comments, this happened because snapshot build failure. To fix it i moved to previous build version of springboot. i did the following changes to the build.gradle file;
id 'org.springframework.boot' version '2.1.3.RELEASE'
changed the '2.2.0.BUILD-SNAPSHOT' to 2.1.3 release. this fixed the error. :)
the buildscript repositories & dependencies are a mess, there's a mismatch:
buildscript {
repositories {
maven { url 'https://repo.spring.io/libs-milestone' }
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.2.0.BUILD-SNAPSHOT'
}
}
apply plugin: 'org.springframework.boot'
see the documentation.

Gradle cannot resolve Spring Boot HATEOAS dependency

I created my skeleton project from http://start.spring.io . But when I build the application, Gradle cannot resolve the HATEOAS dependency. Here is the error I get:
Error:java: Illegal char <:> at index 78:
C:\Users\TempUser\Downloads\hateoas\Could not resolve
org.springframework.boot:spring-boot-starter-hateoas:2.0.4.RELEASE.
This is my build.gradle file:
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-hateoas:2.0.4.RELEASE')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
This declaration
compile('org.springframework.boot:spring-boot-starter-hateoas:2.0.4.RELEASE')
cause error. Change to
compile('org.springframework.boot:spring-boot-starter-hateoas')
This is what under the hood
You've already specified the spring boot components' version right here:
ext {
springBootVersion = '2.0.4.RELEASE'
}
Hence, all starter dependencies must be specified without the version value. Use:
compile('org.springframework.boot:spring-boot-starter-hateoas')
instead of
compile('org.springframework.boot:spring-boot-starter-hateoas:2.0.4.RELEASE')
Hope this helps

java.sql.SQLException: No suitable driver found for jdbc:mariadb in a Gradle project (Intellij)

I have a Gradle project in IntelliJ which I am using to control my dependencies but I am new to Gradle so I am probably doing something wrong.
I am getting this error when running my code:
java.sql.SQLException: No suitable driver found for jdbc:mariadb://<db address>
the build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.10.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')
}
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
dependencies {
// https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client
compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '1.1.7'
}
The jdbc.properties file that I am also using:
jdbc.drivers=com.mariadb.jdbc.Driver
jdbc.url=jdbc:mariadb://<db address>
jdbc.user=root
jdbc.password=password
What am I doing wrong?
You are missing a dependency on the MariaDB Java driver hence this message:
java.sql.SQLException: No suitable driver found for jdbc:mariadb://<db address>
You can add the MariaDB Java driver to your classpath by updating the dependencies block of build.gradle ...
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.mariadb.jdbc:mariadb-java-client")
testCompile('org.springframework.boot:spring-boot-starter-test')
}

Spring configuration: Unable to locate Spring NamespaceHandler

Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx] Offending resource: ServletContext resource [/WEB-INF/spring-servlet.xml]
In gradle you can use shadowJar plugin for fix that. My build.gradle file for compile fat jar:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'application'
mainClassName = 'ru.antowka.Initializer'
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
}
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
shadowJar {
mergeServiceFiles('META-INF/spring.*')
}
// JDK 8
sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding = 'UTF-8'
repositories {
maven {
url 'http://repo.spring.io/snapshot'
}
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
compile 'org.jsoup:jsoup:1.8.3'
compile 'org.springframework:spring-context:4.2.2.BUILD-SNAPSHOT'
compile 'log4j:log4j:1.2.17'
compile 'org.quartz-scheduler:quartz:1.8.6'
compile 'org.springframework:spring-support:2.0.8'
compile 'org.springframework:spring-tx:2.5.4'
compile 'org.springframework:spring-orm:4.1.7.RELEASE'
compile 'org.hibernate:hibernate-core:4.3.10.Final'
compile 'org.postgresql:postgresql:9.4-1201-jdbc41'
compile 'org.apache.commons:commons-dbcp2:2.1'
testCompile 'org.springframework:spring-test:4.2.0.RELEASE'
testCompile 'org.mockito:mockito-core:1.+'
testCompile 'junit:junit:4.12'
}

Categories

Resources