Maven read .properties to use in pom.xml - java

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>

Related

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.

Maven call automatically the release prepare when activated a profile

I do not have much experience with Maven profiles ....
I do not know if Maven can do this, but it definitely could be useful to me ...
Is possible to define a profile, which when called, automatically run the maven release plugin prepare goal?
I explained better ....
instead of:
mvn release: prepare
I would like to call
mvn install-pProfileThatPerformThePrepare
that automatically perform the prepare ...
Thank you....
You just have to bind an execution of the plugin to a phase (e.g. "install" in your example):
<profile>
<id>my-profile</id>
<build>
<plugins>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>prepare</goal>
</goals>
<configuration>
<updateWorkingCopyVersions>false</updateWorkingCopyVersions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

Maven 2.2.1 attached tests. Issue -Dmaven.test.skip=true

I am using the plugin to attached tests in the test of another module.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And in the module where the jar is required:
<dependency>
<groupId>com.myco.app</groupId>
<artifactId>foo</artifactId>
<version>1.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
It has been very useful to me, but I have found a problem: When I execute "clean install -Dmaven.test.skip=true", also the dependency test-jar is required and the proccess fails
yes, because -Dmaven.test.skip=true just makes the maven junit plugins (surefire and failsafe) not execute - it prevents them from running any tests.
it does NOT prevent maven from trying to "collect" all of your test-scoped dependenies. maven still collects all of them.
if you want optional dependencies (regardless of what scope) you should read about maven profiles - you could define a profile in which this dependency will be defined and then maven will try and get it only if you activate the profile (from the command line, for example)
-Dmaven.skip.test or -DskipTests just skips the test execution, it still compiles test classes so it needs test dependencies
If you want to skip the compilation of test classes, you can configure maven compiler plugin to do so, more helpful would be to create separate build profile and skip compilation on demand by specifying special build profile
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>

Generate QueryDsl Q Classes From Package

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>

Any easy way to generate a Findbug HTML report from Maven without site:site?

I am trying to integrate FindBugs in a maven project. Does anyone have a sample pom.xml generating a simple findbug HTML report in target? Is it possible to generate this report without having to run site:site?
Findbugs jar contains 5 XSLT transformations that can be used to convert hard to read XML to easy to read HTML so we can use xml-maven-plugin plugin to execute transformation and here is the configuration:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.4.0</version>
<executions>
<execution>
<id>findbug</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<findbugsXmlOutputDirectory>
${project.build.directory}/findbugs
</findbugsXmlOutputDirectory>
<failOnError>false</failOnError>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>${project.build.directory}/findbugs</dir>
<outputDir>${project.build.directory}/findbugs</outputDir>
<stylesheet>fancy-hist.xsl</stylesheet>
<!--<stylesheet>default.xsl</stylesheet>-->
<!--<stylesheet>plain.xsl</stylesheet>-->
<!--<stylesheet>fancy.xsl</stylesheet>-->
<!--<stylesheet>summary.xsl</stylesheet>-->
<fileMappers>
<fileMapper
implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
<targetExtension>.html</targetExtension>
</fileMapper>
</fileMappers>
</transformationSet>
</transformationSets>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>findbugs</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
To get the report just execute mvn clean install.
The above code snippet contains all 5 possible transformations so try them all and hopefully you will find one you like.
I tried it with maven 3 and Finbugs 2.0
Check out Sonar. It's an open-source, stand-alone, web service that you "submit" your code to and it produces beautiful HTML reports on all kinds of code metrics. It also keeps a history of builds. And best of all, you don't have to modify your builds or poms!
There is a maven goal for it too: sonar:sonar. Jenkins (previously Hudson) has a plugin for it, so it's totally painless if you use that for your CI.
Check it out - you won't be sorry!

Categories