GORM not bootstrapping from JAR - spring

Replacing a persistence layer in legacy app with a JAR file using Spring, Hibernate and GORM. Methods like person.save() work fine when running agains project with Gradle etc. in project. However, after I build the fat jar and reference it with -cp my-big-fat-gorm.jar I get:
java.lang.IllegalStateException: Method on class [blah.Person] was
used outside of a Grails application. If running in the context of a
test using the mocking API or bootstrap Grails correctly.
Using Spring boot for Spring, Hibernate4 and GORM and build.gradle file show below...
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'application'
mainClassName = "blah.App"
jar {
baseName = 'blah-gorm'
version = '1.0-SNAPSHOT'
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.8.2'
compile 'org.grails:gorm-hibernate4-spring-boot:1.1.0.RELEASE'
compile 'org.slf4j:slf4j-simple:1.7.7'
runtime 'com.h2database:h2:1.4.181'
}
Am I missing something in the JAR file creation that causes Spring boot to honor #Entity etc.?
Here is a GitHub project that illustrates this and should allow you to execute and see the same stuff I'm seeing.
https://github.com/twcrone/spring-gorm-jar

You don't have the Spring Boot Gradle plugin installed so you're not actually creating a fat JAR you need to add the following to your build.gradle file:
apply plugin: 'spring-boot'
buildscript {
ext {
springBootVersion = '1.1.0.M2'
groovyVersion = '2.3.2'
}
repositories {
mavenCentral()
maven {
url 'http://repo.spring.io/milestone'
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
With this in place doing gradle assemble and then java -jar ... results in bootstrapping GORM correctly

Related

Issue in upgrading Spring Boot App with Apache Camel Dependencies

Our application is currently running on spring boot 1.5.10.RELEASE version, and I'm trying to upgrade to spring boot 2.0.0.RELEASE But after upgrading to 2.0.0.RELEASE I get the following error
Error: Could not find or load main class com.app.MyApplication
Given Below is the build.gradle file
buildscript {
ext {
springBootVersion = '2.0.0.RELEASE'
camelVersion = '2.24.0'
}
repositories {
mavenCentral()
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
apply from: '../common.gradle'
apply from: '../test.gradle'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'io.spring.dependency-management'
dependencies {
providedRuntime "org.springframework.boot:spring-boot-starter-tomcat"
compile ("org.flywaydb:flyway-core:5.0.7")
compile ("org.springframework.retry:spring-retry:1.2.2.RELEASE")
compile("org.springframework:spring-jms:5.1.1.RELEASE")
compile("org.springframework.boot:spring-boot-starter-activemq:2.0.6.RELEASE")
compile("org.springframework:spring-jdbc:5.0.4.RELEASE")
compile("org.springframework.retry:spring-retry:1.2.2.RELEASE")
compile("com.amazonaws:aws-java-sdk-sqs:1.11.428")
compile("com.amazonaws:amazon-sqs-java-messaging-lib:1.0.4")
compile ("org.flywaydb:flyway-core:5.0.7")
runtime("org.apache.activemq:activemq-kahadb-store:5.15.6")
compile("org.apache.camel:camel-spring-boot-starter:${camelVersion}")
compile("org.apache.camel:camel-core:${camelVersion}")
compile("org.apache.camel:camel-http-starter:${camelVersion}")
compile("org.apache.camel:camel-http4:${camelVersion}")
compile("org.apache.camel:camel-jsonpath:${camelVersion}")
compile("org.apache.camel:camel-jackson:${camelVersion}")
compile("org.apache.camel:camel-base64:${camelVersion}")
compile("org.apache.camel:camel-groovy:${camelVersion}")
compile("org.apache.camel:camel-jolt:${camelVersion}")
compile("org.apache.camel:camel-jaxb:${camelVersion}")
compile("org.apache.camel:camel-ahc:${camelVersion}")
compile("org.codehaus.groovy:groovy-json:3.0.3")
}
configurations {
all*.exclude group: '', module: 'servlet-api'
}
springBoot {
mainClassName = 'com.app.MyApplication'
}
war {
archiveName = "application.war"
}
I'm wondering if this is due to the camel dependencies as when I tried to use Spring Boot version 1.5.22. It is working fine but as soon as I change this to 2.x.x it stop working.
This issue got resolved when I ran this application in Intellij instead of the eclipse.

How can I generate JPA metamodel in spring-boot with gradle?(+ lombok)

I want add JPA metamodel to my project - Spring boot + gradle
I find a lot of examles how can i do it but all with Maven. Also I find this site: https://plugins.gradle.org/search?term=metamodel
and try first three plugins. With each plugins I get errors: error: cannot find symbol in classes marked lombok #Builder annotation and some classes is not entity. It is example some plugin:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.iboyko.gradle.plugins:jpamodelgen-plugin:1.0.1"
}
}
dependencies {
compile ('org.hibernate:hibernate-jpamodelgen')
}
1) Which plugin or method is the most official (correct) to create JPA metamodel in Spring boot + spring-data-jpa + gradle ?
2) How can I specify only the package with entities and not scan another classes?
3) how to make friends with it with lombok?
EDIT
I add this code to gradle file:
sourceSets.configureEach { sourceSet ->
tasks.named(sourceSet.compileJavaTaskName).configure {
options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/java")
}
}
and it generate classes_ fine. After that I mark
generated/sources/java
folder ass root of generated classes(rigth clik to this folder and mark as)
After that I try import generated classes in my repository and IDE show this:
For each module I have 2 module - my_module and my_module_main(I don't understand why) and all classes generate in my_module but all code in my_module_main. If I add this dependency - I have this:
and in generated class I have this:
I was struggling through the same issue. Finally I got it working by adding both dependencies in both compileOnly and annotationProcessor forms. Don't ask me why, but it somehow works.
compileOnly 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'
compileOnly 'org.hibernate:hibernate-jpamodelgen'
annotationProcessor('org.hibernate:hibernate-jpamodelgen')
Did you use Spring Initalizr to generate the project?
This is what you get from Initializr (expect the jpamodelgen that I added by myself):
buildscript {
ext {
springBootVersion = '2.1.1.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 {
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('com.h2database:h2')
compileOnly('org.projectlombok:lombok')
compileOnly ('org.hibernate:hibernate-jpamodelgen')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
// Add source set to define where the generated source code should go to
sourceSets.configureEach { sourceSet ->
tasks.named(sourceSet.compileJavaTaskName).configure {
options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/java")
}
}
You can find a demo project on GitHub:
https://github.com/simasch/demo-gradle-jpa
In IntelliJ you can right click on generated/sources/java and choose

Could not find method bootJar() for arguments

While building the gradle project I am getting below error-
FAILURE: Build failed with an exception.
Where:
Build file '/Users/vdubey/Documents/microservices/workspace/Promo-Service/build.gradle' line: 30
What went wrong:
A problem occurred evaluating root project 'Promo-Service'.
Could not find method bootJar() for arguments [build_3jq74tz48uic808y18txabjvx$_run_closure1#5c4aa147] on root project 'Promo-Service' of type org.gradle.api.Project.
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Get more help at https://help.gradle.org
Any clue why it is failing?
I had this error while following the Spring Boot with Docker guide because my application is using Spring Boot 1.5.10.RELEASE and bootRun was only introduced in 2.0.0.
Luckily, the Spring Boot Docker guide code is in a Github repository, so I was able to navigate back to a pre 2.0.0 version: https://github.com/spring-guides/gs-spring-boot-docker/tree/8933f6efa9a94cf596095658dc0b81986d11a3ec
See the completed build.gradle file for 1.5.10-RELEASE:
// This is used as the docker image prefix (org)
group = 'springio'
jar {
baseName = 'gs-spring-boot-docker'
version = '0.1.0'
}
// tag::task[]
docker {
name "${project.group}/${jar.baseName}"
files jar.archivePath
buildArgs(['JAR_FILE': "${jar.archiveName}"])
}
// end::task[]
Consider checking the presence of gradle plugin for Spring Boot:
https://plugins.gradle.org/plugin/org.springframework.boot
For Gradle 2.1 and later:
plugins {
id "org.springframework.boot" version "2.1.0.RELEASE"
}
For older Gradle versions:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.0.RELEASE"
}
}
apply plugin: "org.springframework.boot"
I had this problem when I am following official Spring testing-web guide. They offer initial gradle file as below.
buildscript {
repositories { mavenCentral() }
}
plugins { id "io.spring.dependency-management" version "1.0.4.RELEASE" }
ext { springBootVersion = '2.0.5.RELEASE' }
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
bootJar {
baseName = 'gs-testing-web'
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")
}
task wrapper(type: Wrapper) {
gradleVersion = '4.6'
}
But when I run the ./gradlew clean command I get the below exception
Could not find method bootJar() for arguments [] on root project '' of type org.gradle.api.Project.
The problem is instead of using plugins { id "io.spring.dependency-management" version "1.0.4.RELEASE" } use id "org.springframework.boot" version "2.1.3.RELEASE". Also don't forget the java and io.spring.dependency-management plugins.
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
8.1. Reacting to the Java plugin
When Gradle’s java plugin is applied to a project, the Spring Boot plugin:
Creates a BootJar task named bootJar that will create an executable,
fat jar for the project. The jar will contain everything on the
runtime classpath of the main source set; classes are packaged in
BOOT-INF/classes and jars are packaged in BOOT-INF/lib
8.4. Reacting to the dependency management plugin
When the io.spring.dependency-management plugin is applied to a
project, the Spring Boot plugin will automatically import the
spring-boot-dependencies bom.
I shared valid build.gradle file below which can be a starting point for anyone who have this problem.
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/4.6/userguide/tutorial_java_projects.html
*/
plugins {
id "org.springframework.boot" version "2.1.3.RELEASE"
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
ext {
springBootVersion = '2.1.3.RELEASE'
}
// In this section you declare where to find the dependencies of your project
repositories {
// You can declare any Maven/Ivy/file repository here.
mavenCentral()
}
bootJar {
mainClassName = 'com.softwarelabs.App'
baseName = 'spring-boot-integration-test'
version = '0.1.0'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
// This dependency is found on compile classpath of this component and consumers.
compile 'com.google.guava:guava:23.0'
testCompile("org.springframework.boot:spring-boot-starter-test")
// Use JUnit test framework
testCompile 'junit:junit:4.12'
}
task wrapper(type: Wrapper) {
gradleVersion = '4.6'
}
For more details please check the spring boot gradle plugin documentation getting started part.
Check how the Spring Boot plugin reacts by other plugins.
I had this error while building a repository I had downloaded.
I was able to resolve the same by modifying my build.gradle to include a buildscript dependency for spring-boot-gradle-plugin and apply org.springframework.boot as a plugin
buildscript {
ext {
springBootVersion = '<Spring boot version>'
}
repositories {
...<my repository config>...
}
// These are gradle build dependencies and not application requirements
dependencies {
...<other dependencies>...
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
apply plugin: 'org.springframework.boot'

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

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?

Resources