Spring Data JPA - Specifications and Querydsl - spring

I'm trying to integrate QueryDSL to my existing project with Spring Data, I've tried different samples and now I've decided to stick to this one Advanced Spring Data JPA - Specifications and Querydsl.
Problem: when I run the project as Maven generate-sources I get this error
error: Annotation processor 'com.mysema.query.apt.jpa.JPAAnnotationProcessor' not found
I'm adding this plugin to my pom.xml as the blog post indicates:
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>maven-apt-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
and the dependency:
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-sql</artifactId>
<version>3.6.9</version>
</dependency>
Can anyone point me in the right direction on how to solve this or how to properly integrate QueryDSL to my project ? Thanks in advance!

The way I could make this work was using the com.querydsl.apt.jpa.JPAAnnotationProcessor instead of the com.mysema.query.apt.jpa.JPAAnnotationProcessor and by changing the dependencies as follow:
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>4.0.6</version>
</dependency>
The plugin end up like this:
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
I also executed in the command line at the projects root mvn eclipse:eclipse to update Eclipse to include the generated sources.
Update:
Replaced the plugin maven-apt-plugin for apt-maven-plugin and changed version to 1.1.3

Instead of explicitly configuring the apt-maven-plugin just add the following dependency to the project (note the jpa classifier):
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<classifier>jpa</classifier>
</dependency>
In case of Jakarta Persistence:
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<classifier>jakarta</classifier>
</dependency>
Also, if you want more Querydsl features than what QuerydslPredicateExecutor has to offer check out https://github.com/infobip/infobip-spring-data-querydsl#JPA.

I had to add the following to fix it.
<querydsl.version>5.0.0</querydsl.version>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>${querydsl.version}</version>
</dependency>

Related

IntelliJ not running jOOQ codegen plugin on build of maven imported project

I have a maven multiple module project and I use IntelliJ for development (importing the POM). The project structure is:
project
entities-module (JPA entities)
query-module (jOOP codegen maven plugin using JPADatabase)
app-module(app logic module)
The problem I'm having is that IntelliJ is not running jOOQ codegen plugin (I'm using Intellij build logic, not delegating to Maven goals).
What I'm doing right now is to do a build from Maven, to force jOOQ code gen and then work from IntelliJ. But this is error prone and I would like to avoid it.
Following the corresponding snippets of my POM:
pom.xml/project/build/pluginManagement/plugins:
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>${jooq.version}</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta-extensions</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta-extensions-hibernate</artifactId>
<version>${jooq.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>jooq-codegen</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generator>
<database>
<name>org.jooq.meta.extensions.jpa.JPADatabase</name>
<properties>
</properties>
</database>
</generator>
</configuration>
</plugin>
pom.xml/project/build/plugins:
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<dependencies>
<dependency>
<groupId>org.foo</groupId>
<artifactId>foo-lib</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<configuration>
<generator>
<database>
<properties>
<property>
<key>packages</key>
<value>
org.foo.data
</value>
</property>
</properties>
</database>
<target>
<packageName>org.foo.data</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
</plugin>

Using Provided Artifact As Maven Plugin Dependency

This seems like it should be a simple question, but I can't seem to find any information about it. When a maven plugin has a required dependency, is it possible to tell it to use an artifact defined elsewhere in the section of the pom?
As an example, I'm trying to add the 'maven-processor-plugin' to my build. That plugin has a dependency on 'hibernate-jpamodelgen'. I'm working with wildfly, so I already have that jar as a dependency of the project. I want to ensure I am using the same version for both. Is what I'm trying to do even possible?
Some code snippets:
<dependencies>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb3</artifactId>
<version>${version.server.bom}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<processor>-proc:none</processor>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>4.5</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/java/jpametamodel</outputDirectory>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<!-- How do I handle this without hard coding the version? -->
<!-- <version>???</version> -->
</dependency>
</dependencies>
</plugin>
</build>
Define a property like <hibernate-jpamodelgen.version> in the <properties> section of the POM.
Then use that property for the version like ${hibernate-jpamodelgen.version}.

Maven can't find any test

I copy pom.xml with a similar project were all ok. Add need dependencies. Also, I add Jupiter with scope "compile" otherwise project doesn't compile. I try to find a solution in similar problems but nothing found.
Env: ubuntu 18, idea 2018.3, openjdk8, maven last version.
I try to change more new or old version junit, surefire plugin and junit-platform-surefire-provider but don't find need combination.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.api.version>5.4.0</junit.api.version>
<junit.jupiter.version>5.4.0</junit.jupiter.version>
<junit.platform.version>1.3.1</junit.platform.version>
<aspectj.version>1.9.4</aspectj.version>
<allure.version>2.12.1</allure.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-junit5</artifactId>
<version>${allure.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
<systemProperties>
<property>
<name>junit.jupiter.extensions.autodetection.enabled</name>
<value>true</value>
</property>
</systemProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-maven</artifactId>
<version>LATEST_VERSION</version>
<configuration>
<reportVersion>2.4.1</reportVersion>
</configuration>
</plugin>
</plugins>
</build>
By default the maven Surefire Plugin expects your tests filenames to match the following conditions:
**/Test*.java - includes all of its subdirectories and all Java
filenames that start with "Test".
**/*Test.java - includes all of
its subdirectories and all Java filenames that end with "Test".
**/*Tests.java - includes all of its subdirectories and all Java
filenames that end with "Tests".
**/*TestCase.java - includes all
of its subdirectories and all Java filenames that end with
"TestCase".
If you test file names don't match the above criteria you have two options. First of all modify your test filenames so that they do match the above criteria. Secondly you can modify the inclusion criteria by configuring the maven-surefire-plugin
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<includes>
<include>%regex[.*(Cat|Dog).*Test.*]</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
For more information have a look at the maven-surefire-plugin documentation.

Should I put openjpa dependency in openjpa maven plugin if it is already in dependencies?

I use OpenJPA as a JPA provider.
I put this dependency in pom.xml
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa-all</artifactId>
<version>2.3.0</version>
</dependency>
and the openjpa-maven-plugin
<plugin>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa-maven-plugin</artifactId>
<version>2.3.0</version>
<configuration>
<includes>**/entities/*.class</includes>
<addDefaultConstructor>true</addDefaultConstructor>
<enforcePropertyRestrictions>true</enforcePropertyRestrictions>
</configuration>
<executions>
<execution>
<id>enhancer</id>
<phase>process-classes</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
</plugin>
Should I put openJPA dependency again in the plugin if it's already in pom.xml? What purpose it has?
Thank you.
Put dependencies in plugin configuration is necessary if you need a specific dependency not in your project dependency.
I don't known openjpa usage but in your example the dependency in your pom is not exactly the same of dependency in your plugin configuration. So if plugin class need is not same class in openjpa-all artifact you need to kept plugin dependency.

Maven - differences between dependencies inside plugin and outside?

I've come across some sample code which specifies dependencies inside the plugin tag like this :
<build>
<plugins>
<plugin>
<dependencies>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.8</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
It look strange to me because mostly I see people put the dependencies tag outside the build tag.
When you add <dependency> to project it is available to that project depending on its scope (compile, test, runtime, etc)
But when you add <dependency> inside plugin's execution you are making that artifact available to that plugin in classpath at runtime
For example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>check my sources</id>
<goals>
<goal>check</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>checkstyle</groupId>
<artifactId>checkstyle</artifactId>
<version>4.4</version>
</dependency>
</dependencies>
</plugin>
in this snippet checkstyle:checkstyle:4.4 is being available to maven-checkstyle-plugin at runtime
Read More
How to override a plugin's dependency in Maven

Resources