Spring Data Mongodb with QueryDsl - mongodb

plugins {
id 'org.springframework.boot' version '2.6.6'
id "com.ewerk.gradle.plugins.querydsl" version '1.0.10' }
configurations {
compileOnly {
extendsFrom annotationProcessor
} }
repositories {
mavenCentral() }
ext {
queryDslVersion = "5.0.0" }
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
//for querydsl
implementation "com.querydsl:querydsl-mongodb:${queryDslVersion}"
annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}" }
tasks.named('test') {
useJUnitPlatform() }
def querydslDir = "$buildDir/generated/querydsl"
querydsl {
springDataMongo = true
querydslSourcesDir = querydslDir }
sourceSets {
main.java.srcDir querydslDir }
configurations {
querydsl.extendsFrom compileClasspath }
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl }
error:
APPLICATION FAILED TO START
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.mongodb.client.internal.MongoClientImpl.createCluster(MongoClientImpl.java:219)
The following method did not exist:
'com.mongodb.ServerApi com.mongodb.MongoClientSettings.getServerApi()'
The calling method's class, com.mongodb.client.internal.MongoClientImpl, is available from the following locations:
jar:file:~/.gradle/caches/modules-2/files-2.1/org.mongodb/mongodb-driver-sync/4.4.2/~/mongodb-driver-sync-4.4.2.jar!/com/mongodb/client/internal/MongoClientImpl.class
jar:file:~/.gradle/caches/modules-2/files-2.1/org.mongodb/mongo-java-driver/3.12.8/~/mongo-java-driver-3.12.8.jar!/com/mongodb/client/internal/MongoClientImpl.class
The calling method's class was loaded from the following location:
file:~/.gradle/caches/modules-2/files-2.1/org.mongodb/mongodb-driver-sync/4.4.2/~/mongodb-driver-sync-4.4.2.jar
The called method's class, com.mongodb.MongoClientSettings, is available from the following locations:
jar:file:~/.gradle/caches/modules-2/files-2.1/org.mongodb/mongo-java-driver/3.12.8/~/mongo-java-driver-3.12.8.jar!/com/mongodb/MongoClientSettings.class
jar:file:~/.gradle/caches/modules-2/files-2.1/org.mongodb/mongodb-driver-core/4.4.2/~/mongodb-driver-core-4.4.2.jar!/com/mongodb/MongoClientSettings.class
The called method's class hierarchy was loaded from the following locations:
com.mongodb.MongoClientSettings: file:~/.gradle/caches/modules-2/files-2.1/org.mongodb/mongo-java-driver/3.12.8/~/mongo-java-driver-3.12.8.jar
Action:
Correct the classpath of your application so that it contains compatible versions of the classes com.mongodb.client.internal.MongoClientImpl and com.mongodb.MongoClientSettings
Help me Plese Can't I use spring-data-starter-mongodb with
QueryDsl?

Related

Replacement for ScalaCompile in gradle when switching to Kotlin

I convert a project that was originally written in Scala into kotlin. It is a typical backend - frontend-design with the backend written in Scala and the Frontend in Angular (the code was written by a former co-worker who is no longer working in this project)
During the gradle-build, the e2e-tests are evaluated. For this purpose the backend is compiled in the following way:
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.scala.ScalaCompile
class ScalaCompilerPlugin implements Plugin<Project> {
void apply(Project project) {
project.configurations {
scalaCompilerPlugin
}
project.afterEvaluate {
project.tasks.withType(ScalaCompile) {
options.encoding = 'UTF-8'
if (scalaCompileOptions.additionalParameters == null) {
scalaCompileOptions.additionalParameters = []
}
scalaCompileOptions.additionalParameters <<
"-Xplugin:" + project.configurations.scalaCompilerPlugin.asPath
}
}
}
}
The 'scalaCompilerPlugin'-plugin is then added to the build.gradle-file in the backend.
plugins {
id 'scala'
id 'com.github.psxpaul.execfork' version '0.1.12'
}
apply plugin: ScalaCompilerPlugin
// ...
Since now the project is converted to Kotlin/Ktor I plan to use something like a KotlinCompiler-plugin for the same purpose, but in gradle 6.0.1, there seems to be no such plugin, and not being that firm with gradle I don't know how to add org.jetbrains.kotlin.gradle.tasks.KotlinCompile.
How can I achieve the same and start the backend during the gradle-build to run the e2e-tests during the test-runs? I am also open to suggestions to change this design.

Spring Boot MongoDb QueryDSL Gradle Intellij Integration for generating the QueryDSL Q classes

I am trying to generate the QueryDSL Q classes for my Mongo entities using gradle in a Spring Boot project. The IDE I'm using is Intellij.
The code I'm using is adapted from this topic Generating JPA2 Metamodel from a Gradle build script:
sourceSets {
generated {
java {
srcDirs = ['src/generated/java']
}
}
}
configurations {
querydslapt
}
task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
source = sourceSets.main.java
classpath = configurations.compile + configurations.querydslapt
options.compilerArgs = [
"-proc:only",
"-processor", "org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor"
]
destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}
compileJava {
dependsOn generateQueryDSL
source generateQueryDSL.destinationDir
}
compileGeneratedJava {
dependsOn generateQueryDSL
options.warnings = false
classpath += sourceSets.main.runtimeClasspath
}
clean {
delete sourceSets.generated.java.srcDirs
}
idea {
module {
downloadJavadoc = true
downloadSources = true
generatedSourceDirs += file('src/generated/java')
}
}
The problem is that in the end in Intellij I have 3 modules. Main, test and generated. The test and generated modules are dependent on the main module. I would like also for the main module to be dependent on the generated module since I'm using the generated Q classes in my code.
All my attempts to solve this end up in a cyclic dependency error from Gradle.
Can someone give me some tips what I could try to solve this problem.
Thanks!

Gradle plugin development error passing arguments

I am trying to create a custom gradle plugin following the example here: http://www.javacodegeeks.com/2012/08/gradle-custom-plugin.html. Everything works fine as long as I dont try to provide arguments to the task, however when I attempt to add arguments, I receive the following error:
Error:(26, 0) Task of type 'com.jwoolston.finalizer.gradle.FinalizerTask_Decorated' has been instantiated directly which is not supported. Tasks can only be created using the DSL.
I've googled it but I don't seem to be getting any results related to my situation (at least not that I understand.
The plugin short id is declared in the generated jar's manifest as: finalizer-plugin.
I have the following files:
FinalizerPlugin.groovy
class FinalizerArgumentExtension {
String path = ''
}
class FinalizerPlugin implements Plugin<Project> {
#Override
void apply(Project project) {
project.extensions.create('finalizeArgs', FinalizerArgumentExtension)
project.task('finalizeTask', type: FinalizerTask)
}
}
FinalizerTask.groovy
class FinalizerTask extends DefaultTask {
#TaskAction
def executeTask() {
println "------------executeTask-------------------"
println "Source Directory : ${project.finalizeArgs.path}"
}
}
Plugin related exerpt of build.gradle of the utilizing project:
apply plugin: 'java'
apply plugin: 'maven'
version = '1.0.0'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
buildscript {
repositories {
maven {
url uri("file://C:\\Users\\ideal\\.m2\\repository")
}
}
dependencies {
classpath group: 'com.jwoolston.finalizer',
name: 'gradle',
version: '1.0.1-SNAPSHOT'
}
}
apply plugin: 'finalizer-plugin'
finalizeArgs {
path = "src/main/java"
}
In short, I don't understand the error. Near as I can tell, everything matches the tutorial. I am trying to simply pass in a string argument to the plugin's task.
Plugin class needs to be fixed as follows:
class FinalizerPlugin implements Plugin<Project> {
#Override
void apply(Project project) { ... }
}
Groovy only supports String interpolation for double-quoted Strings:
println "Source Directory : ${project.finalizeArgs.path}"

How to define a random Id to database table id in mongoDb database using grails 2.2.2?

I am using MongoDb database and grails 2.2.2
in this i want to assign a random Id rather than by default incremental Id generating by Db.
for e.g : abcd.com/demo/view/14524 rather than
abcd.com/demo/view/5
my domain code:
student.groovy
class student{
static mapping = {
id generator:'assigned'
}
static constraints = {
id generator:'assigned'
}
def beforeInsert() {
Random randomNumber = new Random();
id=randomNumber.nextInt(100**4);
println "id "+ id;
}
}
this is dependencies list :-
grails.project.dependency.resolution = {
// inherit Grails' default dependencies
inherits("global") {
// specify dependency exclusions here; for example, uncomment this to disable ehcache:
// excludes 'ehcache'
}
log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
checksums true // Whether to verify checksums on resolve
legacyResolve false // whether to do a secondary resolve on plugin installation, not advised and here for backwards compatibility
repositories {
inherits true // Whether to inherit repository definitions from plugins
grailsPlugins()
grailsHome()
grailsCentral()
mavenLocal()
mavenCentral()
// uncomment these (or add new ones) to enable remote dependency resolution from public Maven repositories
//mavenRepo "http://snapshots.repository.codehaus.org"
//mavenRepo "http://repository.codehaus.org"
//mavenRepo "http://download.java.net/maven/2/"
//mavenRepo "http://repository.jboss.com/maven2/"
}
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
// runtime 'mysql:mysql-connector-java:5.1.20'
compile 'org.imgscalr:imgscalr-lib:4.1'
}
plugins {
runtime ":hibernate:$grailsVersion"
runtime ":jquery:1.8.3"
runtime ":resources:1.1.6"
// Uncomment these (or add new ones) to enable additional resources capabilities
runtime ":zipped-resources:1.0"
runtime ":cached-resources:1.0"
//runtime ":yui-minify-resources:0.1.4"
compile ":cache-headers:1.1.5"
build ":tomcat:$grailsVersion"
runtime ":database-migration:1.2.1"
compile ':cache:1.0.1'
compile ":rest:0.7"
compile ":rendering:0.4.4"
compile ":mail:1.0.1"
compile ":oauth:2.1.0"
compile ":spring-security-core:1.2.7.3"
compile ":spring-security-ui:0.2"
compile ":jquery-ui:1.8.24"
compile ":famfamfam:1.0.1"
compile ":calendar:1.2.1"
}
}
but its giving error.
Is any other option to assign a random Id?
Thank You. :)
You can use the Mongo's ObjectId as a random identifier:
import org.bson.types.ObjectId
class Student {
ObjectId id
String name
}

Use Gradle Project Extension Properties In Configuration Phase

When writing a custom Gradle plugin, how is it possible to access the extension properties defined in the consuming build.gradle in the custom plugin’s configuration phase?
Please see the following MWE.
build.gradle
apply plugin: 'codechecks'
codechecks {
checkstyleConfig = '/home/user/checkstyle.xml'
}
CodechecksPlugin.groovy
class CodechecksPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create('codechecks', CodechecksPluginExtension)
project.apply( [ plugin: 'checkstyle' ] )
project.checkstyle {
configFile = project.codechecks.checkstyleConfig
}
}
}
CodechecksPluginExtension.groovy
class CodechecksPluginExtension {
def checkstyleConfig = 'config/checkstyle/checkstyle.xml'
}
Wanted behavior: The checkstyle plugin uses the configuration file defined in the build.gradle codechecks extension.
Actual behavior: The default value in the CodechecksPluginExtension is being used because the build.gradle codechecks extension is not yet evaluated.
I already tried putting all uses of the codechecks extension in the plugins into closures but they won’t expand correctly due to class casting issues at execution phase.
Thanks for your help!
project.afterEvaluate works for me.
Try:
project.afterEvaluate {
project.checkstyle {
configFile = project.codechecks.checkstyleConfig
}
}