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.
Related
I use jOOQ and MySQL DB in my application. For integration tests I use H2 database and there is a problem. Is there some way to run jooq-codegen-maven plugin twice? I found some maven example for this case. However, in two different cases, I must use two different dependencies. Can I somehow to include dependency in execution?
You can have multiple <execution> elements in any Maven plugin configuration, e.g.
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.9.1</version>
<executions>
<execution>
<id>first-generation</id>
<phase>generate-sources</phase>
<goals><goal>generate</goal></goals>
<configuration>
<!-- jOOQ configuration here -->
</configuration>
</execution>
<execution>
<id>second-generation</id>
<phase>generate-sources</phase>
<goals><goal>generate</goal></goals>
<configuration>
<!-- jOOQ configuration here -->
</configuration>
</execution>
</executions>
</plugin>
Im trying to get the properties-maven-plugin to read from my .properties file. Flyway (which im trying to use the properties for) just keeps throwing errors about the db url being malformed, but works if i set the values within the pom.xml itself, rather than using the properties read from file.
Im using eclipse with the m2e plugin.
plugin config to read from .properties
<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>src/main/resources/config.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
Flyway config where the properties are being used
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>flyway:migrate</goal>
</goals>
</execution>
</executions>
<configuration>
<driver>${db.driver}</driver>
<url>${db.url}</url>
<user>${db.user}</user>
<password>${db.password}</password>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
</plugin>
config.properties located in /src/main/resources/
# Database details
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/dbname
db.user=username
db.pass=password
Ive tried looking through several other stackoverflow threads but none of the solutions seem to work. I'm new to maven and the whole thing seems to be throwing me, anyone got a light to shed?
There are basically two ways you can execute the Flyway migrate goal:
As part of a lifecycle phase: You have configured the Flyway plugin to be executed during the compile phase. That means you can just enter mvn compile and Flyway will be executed along with all other goals that are part of that lifecycle phase and all previous phases. Everything works fine, except you have a slight misconfiguration: The goal must not be prefixed. In order to fix this you have to provide the goal without prefix:
<goals>
<goal>migrate</goal>
</goals>
Now the execution works. Flyway gets all parameters from the properties-maven-plugin because it is executed in a previous phase.
Direct invocation: If you execute Flyway with mvn flyway:migrate, the plugin is invoked independent of any lifecycle phases. Since no phase is executed, the properties-maven-plugin is also not executed because it relies on the initialize phase - which effectively doesn't set any parameters. That's why Flyway complains about missing parameters.
Solution:
If you want the properties-maven-plugin to work together with Flyway you have to execute Flyway as part of a lifecycle. If you don't want to invoke it with every compile phase, you could create a separate profile and only run this profile whenever you need a Flyway migration with mvn compile -PflywayMigration:
<profiles>
<profile>
<id>flywayMigration</id>
<build>
<plugins>
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>migrate</goal>
</goals>
</execution>
</executions>
<configuration>
<driver>${db.driver}</driver>
<url>${db.url}</url>
<user>${db.user}</user>
<password>${db.password}</password>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
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>
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.
I have a property defined like this:
<properties>
<main.basedir>${project.parent.basedir}</main.basedir>
</properties>
Since I use Windows as OS, it contains backslashes. I want to add this path to a glassfish domain as JVM option (using glassfish maven plugin). The problem is, that asadmin can consume only slash as separator, and all my backslashes keep on disappearing. How can I define a property with exactly the same content with slashes?
I don't think there is a non-programmatical way to do that. So I suggest a groovy one-liner with the Maven GMaven plugin (GMaven is usually the simplest way to embed programmatic code into a pom):
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<id>setproperty</id>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
pom.properties['main.basedir']=project.parent.basedir.absolutePath.replace('\\','/');
</source>
</configuration>
</execution>
</executions>
</plugin>
Just an update to Sean's answer, I have had to make some minor adjustments in order to adapt it to the latest groovy maven plugin version:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>setproperty</id>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
project.properties['basedir']=project.parent.basedir.absolutePath.replace('\\','/');
</source>
</configuration>
</execution>
</executions>
</plugin>