Gradle Java Spring boot project with Jooq JPADatabase does not detect entities - java

I am trying to generate jOOQ (v.3.14.8) classes from JPA-annotated entities (#Entity) in a Gradle Spring Boot (v.2.4.3) project with the nu.studer.jooq plugin (v.5.2.1) and the JPADatabase as database.
Whenever I run ./gradlew generateJooq no files are generated in the target folder with the following INFO:
INFO: No modified files: This code generation run has not produced any file modifications.
This means, the schema has not changed, and no other parameters (jOOQ version, driver version, database version,
and any configuration elements) have not changed either.
In automated builds, it is recommended to prevent unnecessary code generation runs. This run took: 283.126ms
Possible means to prevent this:
- Use manual code generation and check in generated sources: https://www.jooq.org/doc/latest/manual/code-generation/codegen-version-control/
- Use schema version providers: https://www.jooq.org/doc/latest/manual/code-generation/codegen-advanced/codegen-config-database/codegen-database-version-providers/
- Use gradle tasks and inputs: https://github.com/etiennestuder/gradle-jooq-plugin/blob/master/README.md
Mar 12, 2021 3:35:02 PM org.jooq.tools.JooqLogger info
INFO: Removing excess files
BUILD SUCCESSFUL in 2s
It looks to me like the entities are not being picked up.
Here is my build.gradle file:
plugins {
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'nu.studer.jooq' version '5.2.1'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-jooq')
runtimeOnly 'com.h2database:h2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
jooqGenerator 'org.jooq:jooq-meta-extensions-hibernate'
}
test {
useJUnitPlatform()
}
jooq {
// version = dependencyManagement.importedProperties['jooq.version'] // use jOOQ version defined in Spring Boot
version = '3.14.8' // specify jOOQ version
configurations {
main {
generationTool {
// logging = org.jooq.meta.jaxb.Logging.WARN
generator {
name = 'org.jooq.codegen.DefaultGenerator'
strategy.name = 'org.jooq.codegen.DefaultGeneratorStrategy'
database {
name = 'org.jooq.meta.extensions.jpa.JPADatabase'
properties {
property {
key = 'packages'
value = 'com.example.jooq.entities'
}
property {
key = 'useAttributeConverters'
value = true
}
}
}
target {
packageName = 'com.example.jooq.generated.jooq.entities'
directory = 'target/generated-sources/jooq'
}
}
}
}
}
}
The sample code for this implementation can be found here.
Thanks for your help already, really appreciate.

Related

Gradle Not Finding Method Implementation for Arguments

I am trying to build a simple maven library and I can't get it to build. I am using Gradle Groovy and Kotlin Multiplatform Library in IntelliJ IDEA.
I haven't even been able to begin coding since my dependencies wont load. I am very new to all this so bare with me.
This is my build.gradle
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.7.10'
}
group = 'me.me'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies{
implementation platform("io.cucumber:cucumber-bom:7.1.0")
implementation 'io.cucumber:cucumber-java'
implementation 'io.cucumber:cucumber-junit-platform-engine'
implementation 'io.cucumber:cucumber-junit'
implementation 'io.cucumber:cucumber-spring'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = '1.8'
}
withJava()
testRuns["test"].executionTask.configure {
useTestNG()
}
}
sourceSets {
commonMain {
}
commonTest {
dependencies {
implementation kotlin('test')
}
}
}
}
and this is the error I am getting
Build file '/Users/Me/Documents/GitHub/Project-Tools/build.gradle' line: 13
A problem occurred evaluating root project 'Project-Tools'.
> Could not find method implementation() for arguments
[DefaultExternalModuleDependency{group='io.cucumber', name='cucumber-bom', version='7.1.0', configuration='default'}]
on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I even tried commenting out each dependency one at a time so see if any work and none did
EDIT: Also I am on a Mac

java.lang.ClassNotFoundException: org.postgresql.Driver from jooq code generating task

I am implementing java-spring-boot project ( jdk11, spring boot 2.3.0.RELEASE ) using jooq-hikari-hibernate-postgres for back end.
I have already created the tables using flyway.
I have created a task name "generateJooqRepo" in build.gradle to generate the java code for the repository. The task is running however failing with error message:
Execution failed for task ':generateJooqRepo'.
> java.lang.ClassNotFoundException: org.postgresql.Driver
Any insight on what I am doing wrong?
build.gradle:
import org.jooq.codegen.*
plugins {
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'org.flywaydb.flyway' version '6.4.4'
id "nu.studer.jooq" version "4.2"
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'application'
apply plugin: 'maven'
apply plugin: 'jacoco'
apply plugin: 'idea'
apply plugin: 'nu.studer.jooq'
configurations {
deployerJars
}
sourceCompatibility = '11.0.1'
targetCompatibility = '11.0.1'
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
dependencies {
//Spring Dependencies
implementation 'org.springframework.boot:spring-boot-starter-actuator:2.3.0.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web:2.3.0.RELEASE'
//postges
jooqRuntime 'org.postgresql:postgresql:42.2.14'
implementation 'org.postgresql:postgresql:42.2.14'
//The gradle-jooq-plugin needs dependencies in a separate configuration.
// It uses the jooqRuntime configuration to detect the needed dependencies, it doesn't use the compile or implementation configuration.
jooqRuntime group: 'org.jooq', name: 'jooq-meta-extensions', version: '3.13.2'
//flyway
compile 'org.flywaydb:flyway-core:6.4.4'
//jooq dependency
implementation 'org.springframework.boot:spring-boot-starter-jooq:2.3.0.RELEASE'
compile 'org.jooq:jooq:3.13.2'
compile 'org.jooq:jooq-meta:3.13.2'
compile 'org.jooq:jooq-codegen:3.13.2'
// This dependency is found on compile classpath of this component.
implementation 'org.apache.commons:commons-lang3:3.9'
}
group = 'com.ringo.tapos.service'
version = '1.0.0-SNAPSHOT'
mainClassName = "com.ringo.tapos.service.Application"
// Use your favourite XML builder to construct the code generation configuration file
// ----------------------------------------------------------------------------------
task generateJooqRepo {
doLast {
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
.configuration('xmlns': 'http://www.jooq.org/xsd/jooq-codegen-3.13.0.xsd') {
jdbc() {
driver('org.postgresql.Driver')
url('jdbc:postgresql://localhost:5432/postgres')
user('postgres')
password('postgres')
schema('tapos')
}
generator() {
database() {
}
generate([:]) {
pojos true
daos true
relations true
deprecated false
records true
//immutablePojos false
}
target() {
packageName('com.ringo.tapos.service.repositories')
directory('src/main/java')
}
}
}
GenerationTool.generate(writer.toString())
}
}
You should add postgres driver dependency to jooqRuntime configuration as well
jooqRuntime 'org.postgresql:postgresql:42.2.14'
UPDATE:
You should add postgres driver to your buildscript classpath if you want to use it in gradle script outside of the tasks provided by nu.studer.jooq plugin
buildscript {
dependencies {
classpath 'org.postgresql:postgresql:42.2.14'
}
}

Querydsl Annotation Processor issue after upgrade to Gradle 5

I have a gradle script which generates querydsl classes from Mongo annotated entities. It was working so far, but after upgrade to Gradle 5 I have a problem with:
* What went wrong:
Execution failed for task ':myproject-common:compileQuerydsl'.
Annotation processor 'org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor' not found
Please find my gradle.build script below. Any ideas what could be wrong? I read that there was change in Gradle 5 that annotation processors are not used by default during compilation and annotationProcessor declaration should be added but when I add it to dependencies the same error occurs.
plugins {
id 'org.springframework.boot' version '2.0.4.RELEASE'
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
}
repositories {
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
jar {
enabled = true
baseName = 'myproject-common'
version = '0.0.1-SNAPSHOT'
}
// do no package commons into fat jar
bootJar {
enabled = false
}
querydsl {
library = 'com.querydsl:querydsl-apt:4.1.4'
querydslSourcesDir = 'src/main/querydsl'
springDataMongo = true
}
sourceCompatibility = 11.0
targetCompatibility = 11.0
sourceSets {
main {
java {
srcDirs = ['src/main/java', 'src/main/querydsl']
}
}
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.data:spring-data-mongodb")
compile("org.springframework.boot:spring-boot-starter-data-rest")
compile("org.springframework.boot:spring-boot-starter-security")
compile("com.fasterxml.jackson.datatype:jackson-datatype- jsr310:2.8.6")
compile("com.google.guava:guava:23.0")
compile("commons-io:commons-io:2.5")
compile("org.aspectj:aspectjweaver:1.8.9")
compile("org.apache.commons:commons-lang3:3.5")
compile("commons-collections:commons-collections:3.2.2")
compile("org.javamoney:moneta:1.1")
compile("com.fizzed:rocker-runtime:1.2.0")
compile("com.querydsl:querydsl-core:4.1.4")
compile("com.querydsl:querydsl-mongodb:4.1.4")
compile("com.querydsl:querydsl-apt:4.1.4")
compile("com.codepoetics:protonpack:1.15")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.assertj:assertj-core:3.7.0")
}
This is my working configuration for JPA without using additional plugins. Gradle 5.3, openjdk 11.0.2.
plugins {
id 'java-library'
}
ext {
springBootVersion = '2.2.0.M1'
queryDslVersion = '4.2.1'
}
dependencies {
api(
"com.querydsl:querydsl-jpa:$queryDslVersion"
)
implementation(
platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"),
'org.springframework.boot:spring-boot-starter-validation',
'org.springframework.boot:spring-boot-starter-data-jpa',
'org.liquibase:liquibase-core',
'org.postgresql:postgresql'
)
annotationProcessor(
platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"),
'jakarta.persistence:jakarta.persistence-api',
'jakarta.annotation:jakarta.annotation-api',
"com.querydsl:querydsl-apt:$queryDslVersion:jpa"
)
}
Please pay attention to the annotation processor. It has suffix ":jpa". Probably this is what you missed. To activate the same one for mongodb you should add ":morphia" suffix.
Please also look at these 2 dependencies:
'jakarta.persistence:jakarta.persistence-api'
'jakarta.annotation:jakarta.annotation-api'
This is a workaround for the issue described here:
https://discuss.gradle.org/t/annotationprocessor-querydsl-java-lang-noclassdeffounderror/27107
They should be transitive dependencies of the annotation processor, but they aren't yet. Probably you will have to include some mongo dependencies to annotationProcessor too.
Generated sources are located in \build\generated\sources\annotationProcessor\java\main
I have finally found a workaround. Querydsl's lack of compatibility with Gradle 5 is reported here as a bug: https://github.com/ewerk/gradle-plugins/issues/108
Workaround is to add to gradle script:
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
I could solve the problem by adding the following two dependencies.
annotationProcessor "com.querydsl:querydsl-apt:4.2.1:jpa"
annotationProcessor 'javax.annotation:javax.annotation-api:1.3.1'
The second dependency was a hidden reason why it not worked for me.
I faced the same issue for Spring Boot Data JPA and Query DSL with Gradle 6.6.1. I tried so many things in my build.gradle file, including some of the suggestions in other responses to this question. I was able to come up with a build.gradle file with a minimal set of additions to the standard build file (standard in the sense of the one generated by https://start.spring.io). Here it is:
plugins {
id 'org.springframework.boot' version '2.4.0'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
id 'com.ewerk.gradle.plugins.querydsl' version "1.0.10"
}
group = 'org.code'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
querydsl {
querydslDefault = true
jpa = true
}
configurations {
querydsl.extendsFrom implementation
}
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
ext {
queryDslVersion = '4.4.0'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
The key addition is the Query DSL plugin:
id 'com.ewerk.gradle.plugins.querydsl' version "1.0.10"
However, it is not enough on its own. Other important additions are:
querydsl {
querydslDefault = true
jpa = true
}
configurations {
querydsl.extendsFrom implementation
}
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
The configuration i am using,it works:
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
}
group = 'io.loremipsum'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
querydsl {
library = 'com.querydsl:querydsl-apt:4.1.4'
querydslSourcesDir = 'src/main/querydsl'
springDataMongo = true
}
sourceSets {
main {
java {
srcDirs = ['src/main/java', 'src/main/querydsl']
}
}
}
// is required when gradle > 5.0
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
compile 'org.springframework.boot:spring-boot-starter-web'
compile("com.querydsl:querydsl-core:4.1.4")
compile("com.querydsl:querydsl-mongodb:4.1.4")
compile("com.querydsl:querydsl-apt:4.1.4")
compile 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'
}
pay attention to the dependency declaration:
compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation dependency should not be used or will result in an exception:
Annotation processor 'org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor' not found
I see you have compile("com.querydsl:querydsl-apt:4.1.4") in your dependencies. According to the docs
Since implementation details matter for annotation processors, they must be declared separately on the annotation processor path. Gradle ignores annotation processors on the compile classpath.
So, put com.querydsl:querydsl-apt:4.1.4 in the annotationProcessor scope.
BTW, consider switching to api / implementation scopes over compile.

using springboot with local elastic-search instance not working

I want to use elasticSearch repository inside a spring boot application .
I have a local running instance of elastic search 5.5.2
I've search for a while, tried so many combinations of the several sources but nothing yield a working solution , and right now I'm stuck with error :
Error creating bean with name 'elasticsearchTemplate'
what am i doing wrong ?
This is my main class
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld1");
try
{
SpringApplication.run(Application.class, args);
}
catch(Exception e)
{
logger.info("caught an exception",e);
}
}
}
config class
#Configuration
#EnableElasticsearchRepositories(basePackages = "hello")
#ComponentScan(basePackages = {"hello"})
public class config {
#Bean
public NodeBuilder nodeBuilder() {
return new NodeBuilder();
}
#Bean
public ElasticsearchOperations elasticsearchTemplate() {
Settings.Builder elasticsearchSettings =
Settings.settingsBuilder()
.put("http.enabled", "false") // 1
.put("path.data", "/home/nad/elasticsearch_data") // 2
.put("path.home", "/home/nad/Downloads/elasticsearch-5.5.2"); // 3
return new ElasticsearchTemplate(nodeBuilder()
.local(true)
.settings(elasticsearchSettings.build())
.node()
.client());
}
}
my build.gradle file
group 'FirstWebServer'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.8.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
repositories {
maven { url "http://repo.spring.io/milestone" }
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
// tag::jetty[]
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
// end::jetty[]
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
// end::actuator[]
//compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-elasticsearch', version: '1.2.5.RELEASE'
compile group: 'org.springframework.data', name: 'spring-data-elasticsearch', version: '3.0.1.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '2.0.0.M6', ext: 'pom'
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.18'
testCompile("junit:junit")
}
what is the proper way of configuration ? some examples shows application.properties configuration , some dont .
What about compatability , some shows that only elasticsearch 2. * or lower, some said that 5
Its really confusing :(
Thank you very much for your help
To use Elasticsearch 5.x you need Spring Data release Kay (already released), which is only part of Spring Boot 2 (to be released soon). Right now your only option really is to use the latest Spring Boot 2 build and wait for a stable release.
Some people tried using Spring Data Kay with Spring boot 1.5, but ended up in dependency hell. Probably not worth the pain with Spring Boot 2 just around the corner.

spring web developement - disable caching for static content

I'm developping a angularjs application with Spring.
I often have to change my html/javascript file and I noticed that spring is caching static contents. How can I disable that?
I already tried this ...
#Configuration
#AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
class WebMvcConfig extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
#Autowired
private Environment env;
#Bean
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
boolean devMode = this.env.acceptsProfiles("dev");
//boolean useResourceCache = !devMode;
boolean useResourceCache = false;
Integer cachePeriod = devMode ? 0 : null;
registry.addResourceHandler("/public/**")
.addResourceLocations("/public/", "classpath:/public/")
.setCachePeriod(cachePeriod)
.resourceChain(useResourceCache)
.addResolver(new GzipResourceResolver())
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new AppCacheManifestTransformer());
}
}
and that ...
WebContentInterceptor webContentInterceptor;
public #Bean WebContentInterceptor webContentInterceptor () {
if (this.webContentInterceptor == null) {
this.webContentInterceptor = new WebContentInterceptor();
this.webContentInterceptor.setAlwaysUseFullPath (true);
this.webContentInterceptor.setCacheSeconds (0);
this.webContentInterceptor.setCacheMappings (new Properties() {
private static final long serialVersionUID = 1L;
{
put ("/styles/**", "0");
put ("/scripts/**", "0");
put ("/images/**", "0");
put ("/js/**", "0");
}
});
}
return this.webContentInterceptor;
}
this is my build.gradle file
group 'xyz'
version '1.0-SNAPSHOT'
buildscript{
repositories{
mavenCentral()
}
dependencies{
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.security:spring-security-web:4.0.3.RELEASE'
compile 'net.sf.dozer:dozer:5.4.0'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'com.h2database:h2'// For Testing purpose
compile 'com.google.guava:guava:19.0' // google library for data collections
testCompile("junit:junit")
//testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper){
gradleVersion = '2.3'
}
configurations.all {
// https://stackoverflow.com/questions/14024756/slf4j-class-path-contains-multiple-slf4j-bindings/25694764#25694764
exclude module: 'slf4j-log4j12'
}
Just put this configuration option into your application.properties:
spring.resources.chain.cache=false # Disable caching in the Resource chain.
You may want to also take a look at more fine grained config options related to Static content served by Spring Boot (scroll down to section # SPRING RESOURCES HANDLING).
Additionally, there may be static resources cached by infrastructure that is not handled by Spring Boot and it's container (e.g Web Browser). If you want to overcome this type of caching, there is option to use technique called cache busting. Read this section of Spring Boot docs to get more info about it.
It was a silly mistake. I was editing the HTML/CSS/JS source files but the compiled and deployed version was not affected from this modification.
To see not compiled but the very version of static content you're editing set in application.yml something like:
spring:
profiles: dev
resources:
static-locations: file:src/main/resources/static
But do so only in dev profile to avoid problem after deployment.
Add this is in application.properties
Use this if you want to cache for a particular amount of time
spring.resources.cache.cachecontrol.cache-public=true spring.resources.cache.cachecontrol.max-age=2000 #(2 seconds)
Use this if you don't want any cache
spring.resources.cache.cachecontrol.no-cache=true
But try to adopt first way, as it is not a good practice to load static content every time from server

Categories