How to append timestamp to jar in spring boot gradle project? - spring-boot

How to append a timestamp to jar in the spring boot Gradle project?
./gradlew build
Creates jar app-0.0.1-SNAPSHOT.jar, how to create jar with timestamp
app-0.0.1-SNAPSHOT-2020-11-09.02-30.jar
-------- Gradle
plugins {
id 'org.springframework.boot' version '2.3.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}

How about
bootJar {
archiveFileName = "app-0.0.1-SNAPSHOT-"+new Date().format('yyyy-MM-dd.HH-mm')+ ".jar"
}
Tested with gradle 6.6.1 and spring boot plugin version 2.3.4

Related

Spring Boot Gradle application "Error: Could not find or load main class"

I have Spring boot Gradle project that runs perfectly local by Gradle, but not throw the jar file created.
The project has two targets: update data at Postgres DB from another api by scheduling, and let users get the data.
Application class:
I tried to deploy my project on Heroku, Google cloud and via java -jar command, I'm getting the same behavior.
I don't know if it's the problem but on the cloud the build runs successfuly, and after that I get the error: Error: Could not find or load main class com.Application . and ofcourse all the get requests gets denied and server is not accepts any requests.
my build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
}
}
plugins {
id 'java'
id 'idea'
id 'application'
id 'java-library'
id 'java-gradle-plugin'
id "com.heroku.sdk.heroku-gradle" version "2.0.0"
id 'org.springframework.boot' version '2.7.2'
id "io.spring.dependency-management" version "1.0.4.RELEASE"
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'application'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
mainClassName = 'com.Application'
sourceSets.main.java.srcDirs = ['src/main/java']
springBoot{
mainClass = "com.Application"
}
repositories {
mavenCentral()
}
configurations {
all*.exclude module: "spring-boot-starter-logging"
all*.exclude group: "ch.qos.logback"
all*.exclude module: "log4j-over-slf4j" // allow using log4j 2.x
all*.exclude module: "slf4j-simple" // log4j is the configured backend
}
dependencies {
implementation group: 'org.jsoup', name: 'jsoup', version: '1.14.3'
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.apache.maven.plugins:maven-shade-plugin:3.0.0')
implementation('com.google.code.geocoder-java:geocoder-java:0.16')
implementation('org.springframework.boot:spring-boot-starter-web')
implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.0'
runtimeOnly('org.postgresql:postgresql')
implementation ( 'org.projectlombok:lombok:1.18.20')
annotationProcessor ( 'org.projectlombok:lombok:1.18.20')
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
task cleanBuildPublish{
build.mustRunAfter clean
}
group 'com'
version '1.0-SNAPSHOT'
jar {
enabled =false
manifest {
attributes(
'Main-Class': 'com.Application',
)
}
}
bootJar {
manifest {
attributes(
'Main-Class': 'com.Application',
)
}
}
test {
useJUnitPlatform()
}
and my project structure :
the error I get on google cloud:

getting springboot to support groovy 4.0 (apache packaging) | needed 3.0.0-M2 and more

spring-boot > 2.3.1 will grab groovy-bom from codehaus instead of org.apache.groovy packaging, even if you declare org.apache.groovy dependendices
I found this means spring-boot > 2.3.1 will not build groovy 4.0
even spring initializr bakes this in... because when you go springboot 2.6.7, initializr is using groovy packaging from org.codehaus. so that limits 2.6.7 to use groovy 3.0.10, as that's the cutoff for groovy to show up in org.apache packaging. and groovy 4.x uses apache packages.
here's the gradle initializr created from this URL
plugins {
id 'org.springframework.boot' version '2.6.7'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'groovy'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.codehaus.groovy:groovy'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
Support for Groovy 4 is coming in Spring Framework 6 and Spring Boot 3. It’s currently available in Spring Boot 3.0.0-M2 which is published to https://repo.spring.io/milestone.
you 1st have to change settings.gradle to add the following:
pluginManagement {
repositories {
maven { url 'https://repo.spring.io/milestone' }
gradlePluginPortal()
}
}
Then I had to modify my build.gradle as follows:
plugins {
// id 'org.springframework.boot' version '2.6.7'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'groovy'
}
plugins {
id 'org.springframework.boot' version '3.0.0-M2'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = javaSrcVersion
targetCompatibility = javaClassVersion
repositories {
mavenCentral()
maven { url("https://repo.spring.io/milestone/")}
}
dependencies {
runtimeOnly('com.h2database:h2')
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
// implementation 'org.codehaus.groovy:groovy'
implementation("org.apache.groovy:groovy:${groovyVersion}")
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation('com.google.code.findbugs:jsr305:latest.integration')
implementation group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
implementation group: 'jakarta.persistence', name: 'jakarta.persistence-api', version: '3.1.0'
implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
testImplementation("org.testng:testng:${testNgVersion}")
}
tasks.named('test') {
useJUnitPlatform()
}

Build failed Spring Boot Plugin was not found

I download a repo 'gs-handling-form-submission' from spring.io and now I'm trying to build: gradle build
Following Error has being triggered
* Where:
Build file 'C:\Users\Username\Desktop\gs-handling-form-submission\complete\build.gradle' line: 11
* What went wrong:
Plugin [id: 'org.springframework.boot', version: '2.4.2'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.springframework.boot:org.springframework.boot.gradle.plugin:2.4.2')
Searched in the following repositories:
Gradle Central Plugin Repository
I already added a repo url, changed spring.boot version, added buildscript.repositories() at the first line, even doing that, I can't get to compile.
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
jcenter()
mavenCentral()
maven {
url: "https://plugins.gradle.org/m2/"
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
Fixing the build.gradle, removing : url, was possible to build
plugins {
id 'org.springframework.boot' version '2.6.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
jcenter()
mavenCentral()
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}

Cannot change dependencies of configuration ':implementation' after it has been included in dependency resolution

I have got a very basic build.gradle file.
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
bootJar{
mainClassName = "$mainClassName";
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
I get the following error when bootJar task is placed before dependencies block. When placed after the dependencies block i don't get an error.
What went wrong:
A problem occurred evaluating root project 'sample-project-spring-boot'.
Cannot change dependencies of configuration ':implementation' after it has been included in dependency resolution.

Gradle spring boot 2 spring-boot-dependencies cannot download dependencies

I am using eclipse 2018, gradle 5.2.1, buildship 3.0.1.
My config looks like:
I try to create spring boot 2 according to building-spring-boot-2-projects-with-gradle
The build.gradle is:
plugins {
id 'java'
id 'com.gradle.build-scan' version '2.1'
id 'org.springframework.boot' version '2.1.3.RELEASE'
}
repositories {
jCenter()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-dependencies:2.1.3.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
components {
withModule('org.springframework:spring-beans') {
allVariants {
withDependencyConstraints {
// Need to patch constraints because snakeyaml is an optional dependency
it.findAll { it.name == 'snakeyaml' }.each { it.version { strictly '1.23' } }
}
}
}
}
}
buildScan {
// always accept the terms of service
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
// always publish a build scan
publishAlways()
}
bootJar {
mainClassName = 'gt4.App'
}
However, after I save build.gradle, the Project and External Dependencies disappear, and the spring boot jars are not downloaded too.
What I was wrong?
If I create spring boot project with gradle by Spring Tool Suite 4, the generated build.gradle is:
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
then it works.
Do I use spring-boot-dependencies wrong?
try to deleting the local Repository and build project again to download dependancy.
Delete .m2 or .gradle folder and then Rebuild your project.
a)On a Windows machine, the .gradle or .m2 path will be:
c:\Users\username\.m2 or c:\Users\username\.m2
b)On linux machine, the .gradle or .m2 path will be:
USER_HOME/.m2/

Resources