using springboot with local elastic-search instance not working - java

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.

Related

Use Spring Boot as a transitive dependency from common JAR with Gradle

I have a common library that is published as a jar to maven local, which is then used by a set of very thin client applications. These applications basically feed their own configuration to the common library and initializes themselves as Spring applications.
This common library uses spring boot and a bunch of other dependencies. My question is, is it possible for me to not define spring boot as a dependency in the applications, and simply use the common library's spring version as a transitive dependency in all the applications?
I feel like this should be possible, but when I tried doing it, the springframework.org.boot classes are not being resolved (eg: #SpringBootApplication annotation).
These are my build.gradle files:
build.gradle from the common library
plugins {
id 'java-library'
id 'maven-publish'
}
group 'com.my.group'
version '1.0-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenLocal()
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId = 'com.my.group'
artifactId = 'common-lib'
version = '0-alpha.1'
from components.java
}
}
}
configurations {
springBom
compileOnly.extendsFrom(springBom)
annotationProcessor.extendsFrom(springBom)
implementation.extendsFrom(springBom)
}
dependencies {
springBom enforcedPlatform('org.springframework.boot:spring-boot-dependencies:2.3.5.RELEASE')
api 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.cloud:spring-cloud-aws-messaging'
annotationProcessor 'org.springframework.boot:spring-boot-autoconfigure-processor'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
// a few other non-spring dependencies
}
build.gradle from a sample application
plugins {
id 'java'
}
group = 'com.my.group'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenLocal()
}
dependencies {
implementation group: 'com.my.group', name: 'common-lib', version: '0-alpha.1'
}
Example application initialization
package com.my.group.Core;
// none of the below are resolved
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
#SpringBootApplication(scanBasePackages = {"com.my.group"})
public class ConsumerApplication implements ApplicationRunner {
#Autowired
Core core;
public ConsumerApplication() {
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
#Override
public void run(ApplicationArguments args) {
try {
core.processEvents();
} catch (ConfigurationException | InitializationException e) {
e.printStackTrace();
}
}
}
If I add in the following dependencies to my applications, everything works as expected.
springBom enforcedPlatform('org.springframework.boot:spring-boot-dependencies:2.3.5.RELEASE')
implementation 'org.springframework.boot:spring-boot-starter'
I found the issue. I have not included api in the configurations and as a result, the application could not resolve the version. Fixed by updating the build.gradle in my common library like so:
configurations {
springBom
compileOnly.extendsFrom(springBom)
annotationProcessor.extendsFrom(springBom)
implementation.extendsFrom(springBom)
api.extendsFrom(springBom)
}

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.

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

Is there a way to specify dependencies for a newly created sourceset in gradle?

In gradle I have created a new sourceSet for service testing like this:
sourceSets{
servicetest{
java.srcDirs = [//path to servicetests]
}
}
This source set depends on TestNG, so I was hoping to pull the dependency down by doing something like:
dependencies{
servicetest(group: 'org.testng', name: 'testng', version: '5.8', classifier: 'jdk15')
}
Unfortunately this returns an error. Is there any way to declare a dependency for a specific sourceSet or am I out of luck?
Recent Gradle versions automatically create and wire configurations 'fooCompile' and 'fooRuntime' for each source set 'foo'.
If you are still using an older version, you can declare your own configuration and add it to the source set's compileClasspath or runtimeClasspath. Something like:
configurations {
serviceTestCompile
}
sourceSets {
serviceTest {
compileClasspath = configurations.serviceTestCompile
}
}
dependencies {
serviceTestCompile "org.testng:testng:5.8"
}
The following works for Gradle 1.4.
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_6
sourceSets {
serviceTest {
java {
srcDir 'src/servicetest/java'
}
resources {
srcDir 'src/servicetest/resources'
}
compileClasspath += sourceSets.main.runtimeClasspath
}
}
dependencies {
compile(group: 'org.springframework', name: 'spring', version: '3.0.7')
serviceTestCompile(group: 'org.springframework', name: 'spring-test', version: '3.0.7.RELEASE')
serviceTestCompile(group: 'org.testng', name:'testng', version:'6.8.5')
}
task serviceTest(type: Test) {
description = "Runs TestNG Service Tests"
group = "Verification"
useTestNG()
testClassesDir = sourceSets.serviceTest.output.classesDir
classpath += sourceSets.serviceTest.runtimeClasspath
}

Categories