Generate QueryDsl Q Classes From Package - java

How do I generate QueryDsl Q-Classes by only specifying a package name?
Given the source classes reside in my target/generated-sources folder since they are the product of other build plugins (WSDLs, XSDs, etc.)
I have tried using the following plugins, but can't find the right configuration:
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-maven-plugin</artifactId>
<version>2.9.0</version>
<executions>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources</outputDirectory>
<processor>${com.mysema.query.apt.ProcessorClass}</processor>
</configuration>
</executions>
and:
<groupId>com.mysema.maven</groupId>
<artifactId>maven-apt-plugin</artifactId>
<version>1.0.4</version>
What I'd like to do is something like this:
<configuration>
<packageName>com.my.package</packageName>
<sourceFolder>target/generated-sources</sourceFolder>
<targetFolder>target/generated-sources/querydsl</targetFolder>
</configuration>
...which would generate the classes:
com.my.package.QFoo.java
com.my.package.QBar.java
Since there's no common JPA or JDO annotation, and I don't have have access to the source files, I haven't been able to use any of the com.mysema.query.apt.*Processors for the maven-apt-plugin's <processor>.
EDIT 1: added full maven-apt-plugin configuration.
EDIT 2:
- I was able to get the maven-apt-plugin to work sporadically via the maven command line, but not Eclipse/STS by extending AbstractQuerydslProcessor to look for #XmlType-annotated classes. Double code-generation is admittedly not an ideal solution.

The answer is to generate the Q-classes using the strategy Timo outlined here: https://github.com/mysema/querydsl/issues/196
In my module's package-info.java:
#QueryEntities({ com.remote.module.Foo.class,
com.remote.module.Bar.class })
package com.my.local.module.querydsl;
import com.mysema.query.annotations.QueryEntities;
The plugin execution in the Maven POM:
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<executions>
<execution>
<id>apt-maven-plugin-remote-module-QuerydslAnnotationProcessor</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources</outputDirectory>
<showWarnings>true</showWarnings>
<!-- genereate Q-classes specified in package-info.java -->
<processor>com.mysema.query.apt.QuerydslAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
</dependency>
</dependencies>
</plugin>

Related

How to package spring boot project with multiple JAR dependencies?

I have a spring boot project that has a few local jar files dependencies like
mssql-jdbc-6.2.1.jre8.jar
internal-commons.jar
internal-db.jar
etc.
How do I package the project into a WAR file? Thanks in advance!
You can use existing libraries to achieve this.
If you use maven, you can use the maven-dependency-plugin
For example, in your pom.xml file, add the following to your dependencies:
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
</dependency>
And then, use it as a plugin in your pom.xml:
<plugin>
<!-- http://jonathangraham.github.io/2016/01/05/Local_Jar_Dependency_With_Maven -->
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
The entire process is explained here in more detail.

Combining Querydsl-jpa and querydsl-sql and code generation

Here is the thing:
I have been using querydsl-jpa in my projects and code generation has never been a problem. I use this plugin in maven:
<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>${project.build.directory}/generated-sources</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
Now, I need to also use querydsl-sql and apparently, I can't use the Q-generated classes created by com.querydsl.apt.jpa.JPAAnnotationProcessor. Here is the plugin in maven:
<plugin>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-maven-plugin</artifactId>
<version>4.2.1</version>
<executions>
<execution>
<goals>
<goal>export</goal>
</goals>
</execution>
</executions>
<configuration>
<jdbcDriver>com.mysql.cj.jdbc.Driver</jdbcDriver>
<jdbcUrl>jdbc:mysql://localhost:3306/mydatabase</jdbcUrl>
<jdbcUser>root</jdbcUser>
<jdbcPassword></jdbcPassword>
<packageName>com.myproject.domain</packageName>
<targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>
</plugin>
THE CHALLENGE
The second plugin above generates Q-classes for all schemas in my DBMS (MySql) whereas I have specified the schema to generate Q-classes from.
How do I specify the username, password and jdbcUrl from a file since I don't want to store sensitive information in the git repository.
Here are my solutions:
For challenge one, I haven't found a solution per se but some sort of workaround. I created a user in my DBMS (MySql) that has privileges on the single schema that I am interested in. That way, the user won't be able to generate Q-classes for other schemas. So problem one "solved".
Though I still believe that in the plugin one should be able to specify the schema to be generated. Interestingly enough <schemaPattern></schemaPattern> as suggested by #Rober Bain which is also in the querydsl-sql documentation does not work.
For challenge two, first you need to create a properties files say dev.properties with the needed content
jdbc-url=jdbc:mysql://localhost:3306/myschema?nullNamePatternMatchesAll=true
jdbc-user=my_user
jdbc-password=my_password
Then, include the following properties-maven-plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>dev.properties</file> // Reference to properties file
</files>
</configuration>
</execution>
</executions>
</plugin>
... and in your query-dsl plugin ...
<plugin>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-maven-plugin</artifactId>
<version>4.2.1</version>
<executions>
<execution>
<goals>
<goal>export</goal>
</goals>
</execution>
</executions>
<configuration>
<jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
<jdbcUrl>${jdbc-url}</jdbcUrl>
<jdbcUser>${jdbc-user}</jdbcUser>
<jdbcPassword>${jdbc-password}</jdbcPassword>
<packageName>com.myproject.domain</packageName>
<targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
</plugin>
Check out this link for more info Read pom.xml configurations from properties file
Since the above link is down, use Wayback Online to see the original web page.
or
Here is a snapshot of the content
and a continuation
Use schemaPattern within the configuration element: "a schema name pattern in LIKE pattern form; must match the schema name as it is stored in the database, multiple can be separated by comma (default: null)" from the querydsl docs.
While the doesn't do exactly what you're asking for, I believe it's the standard way of solving this problem. Use encrypted data in a Maven pom.

com.mysema.querydsl querydsl-apt plugin does not generate QueryDSL classes

We had configured the generation of QueryDSL in our project using the maven plugin:
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
<dependencies>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
At some point, we made changes in our project, and the generation process begin to fail with this error:
cannot find symbol import xxx.xxxx.xxxx.domain.QContact;
After some investigation, this problem is caused by a class that has been entirely commented.
For example:
//package xxx.xxxx.xxxxx;
////
//public class Test {
////
//}
If you put a class like this in your domain package, the generation process fails.
Well, on the other hand, this kind of classes wouldn't have to exist in your project.

Customize package for QueryDSL output

I am creating QueryDSL objects for MongoDB using Maven, here's the build xml,
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<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>
</execution>
</executions>
<configuration>
<outputDirectory>src/main/java</outputDirectory>
<!-- This processor uses the Spring MongoDB annotations for processing -->
<processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
</configuration>
</plugin>
</plugins>
</build>
But its generating the query class QDomain for Domain in the same package as Domain.
Can the plugin be customized to put Query classes in a separate package?
You can use the querydsl.packageSuffix APT option to add a suffix to your generated package. Just add the following block inside configuration
<options>
<querydsl.packageSuffix>.query</querydsl.packageSuffix>
</options>

GWT, Maven and AspectJ: RequestFactory validation for AOPed code?

To use GWT 2.4.0 RequestFactory, you have to run request factory validation tool. Otherwise, it just won't work. [Google says][1], that it's enough just to add 2 plugins to pom.xml:
<!-- requestfactory-apt runs an annotation processor (APT) to
instrument its service interfaces so that
RequestFactoryServer can decode client requests. Normally
you would just have a dependency on requestfactory-apt
with <scope>provided</scope>, but that won't work in
eclipse due to m2e bug
https://bugs.eclipse.org/bugs/show_bug.cgi?id=335036 -->
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.0.5</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.google.web.bindery</groupId>
<artifactId>requestfactory-apt</artifactId>
<version>${gwtVersion}</version>
</dependency>
</dependencies>
</plugin>
<!-- Google Plugin for Eclipse (GPE) won't see the source
generated above by requestfactory-apt unless it is exposed
as an additional source dir-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/apt</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
The problem is, I have quite a complicated server-side code that uses AOP, so when validation tool is ran against that code, it fails because "there's no method xxx()", "class xxx doesn't implement interface yyy", etc.
So, my question is, is it possible to fix this issue on pom.xml level, rather then moving all AOP code into separate project that will be compiled separately?
Solved by moving all AOPed code to another project.

Categories