Kotlin-Kapt Annotation Processor not working with maven - maven

I want to generate jpa querydsl files from kotlin entity classes.
There is a very good examples online of how to generate the dsl files using gradle https://github.com/JetBrains/kotlin-examples/blob/master/gradle/kotlin-querydsl/build.gradle.
However I have tried to implement this in maven and have had no luck.
My current pom is below. Does anybody know what the issue might be?
Thanks in advance.
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test-jpa</artifactId>
<version>2.7.0-SNAPSHOT</version>
<properties>
<kotlin.version>1.1.50</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>3.6.4</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/java</sourceDir>
<sourceDir>${project.build.sourceDirectory}</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

The potential problem is that you missed the jpa classifier:
<annotationProcessorPath>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>3.6.3</version>
<classifier>jpa</classifier>
</annotationProcessorPath>
I added a Maven/Querydsl example to the kotlin-examples repository. Note that the example has a slightly more complicated pom as it also supports Java/Kotlin combined projects.

Related

Additional-spring-configuration-metadata.json only merges with the configurationProperties generated metadata on second build

I have a kotlin spring boot auto configuration project. It has a class Annotated with #ConfigurationProperties. This generates the property metadata file as expected. I want to add a property that does not come from the #ConfigurationProperties class. As documented, to do that I should create my own additional-spring-configuration-metadata.json file.
I found that the first time I run mvn clean package, the metadata is generated without including the info from additional-spring-configuration-metadata.json. The second time I run mvn package (assuming I didn't do a clean) the metadata includes the info from additional-spring-configuration-metadata.json
Why is this? How do I fix it to generate the data first time everytime?
My pom is below:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.redacted.entity.autoconfigure</groupId>
<artifactId>mongo-autoconfiguration</artifactId>
<version>1.0.0</version>
<name>mongo-autoconfiguration</name>
<description>AutoConfiguration for mongo</description>
<properties>
<java.version>1.8</java.version>
<kotlin.version>1.3.50</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<version>2.1.7.RELEASE</version>
</annotationProcessorPath>
<annotationProcessorPath>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.7.RELEASE</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
UPDATE: I added a solution below but I still have a bounty open. I will award it if someone finds a less cumbersome way of solving the problem
I fixed it by adding in the mvn copy resources plugin to copy the additional-spring-configuration-metadata.json file in the process-sources phase so it can be available when kapt runs.
additional-spring-configuration-metadata.json
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>process-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/META-INF</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/META-INF</directory>
<includes>
<include>
additional-spring-configuration-metadata.json
</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
You may be running into a bug that the kapt goal is not running - https://youtrack.jetbrains.com/issue/KT-18022. Even though that is marked as fixed there are recent comments of other reports with same issue.

QueryDSL generated classes keep getting deleted in maven project in Spring Tool Suite

I am using QueryDSL in a maven Spring Boot project in Eclipse, with Spring DevTools activated, and everything works fine, except for the fact that the QClass used in QueryDSL get deleted, and next time I try to relaunch the project (or it tries to auto-reload through Spring DevTools) I get an error, and I must go through "Run as"->"Maven generate-sources" again.
I am not sure what exactly triggers the deletion, but I noticed that for instance adding or removing a file will trigger the deletion. Removing DevTools didn't help.
Is there a way to prevent those constant deletion, or to make sure that the classes are automatically regenerated afterward?
Here are the relevant part of my pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>REDACTED</groupId>
<artifactId>REDACTED</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>REDACTED</name>
<description>REDACTED</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
[...]
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
[...]
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/customfolder</outputDirectory>
<clearOutputDir>false</clearOutputDir>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/customfolder/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Change the pom.xml as follows. It will work
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
and add the plugin
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>

Maven : get version from dependency of the present dependency

I have a pom.xml that contains some projects
<groupId>org.company</groupId>
<artifactId>aggregator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.company</groupId>
<artifactId>project-one</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.company</groupId>
<artifactId>project-two</artifactId>
<version>1.0.2-SNAPSHOT</version>
</dependency>
</dependencies>
Now, i initialized an other project and its pom.xml as below
<dependencies>
<dependency>
<groupId>org.company</groupId>
<artifactId>aggregator</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<version>${maven-iterator.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>iterator</goal>
</goals>
<configuration>
<items>
<item>project-one</item>
<item>project-two</item>
</items>
<pluginExecutors>
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<goal>copy</goal>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.company</groupId>
<artifactId>#item#</artifactId>
<version>???</version> <!-- I need to specify the version that is in the aggregator pom.xml -->
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.basedir}/Directory/#item#</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
I tried to remove the field and I thought maven would look for the version specified in the dependency for exemple :
project-one.1.0.0-SNAPSHOT
project-two.1.0.2-SNAPSHOT
but it didn't work.
Is there a way to handle versions through dependencies ?
Thanks

how to solve jmeter 3.1 compatibility issue

enter code hereI am using Jmeter 3.1 GUI version. when i run the maven project getting the error
jmeter.JMeter: Error in NonGUIDriver java.lang.IllegalArgumentException: Problem loading XML from.
I tried so many commination maven dependency it not help to solve the issue. Could you please provide pom.xml for jmeter3.1 jmeter-maven-plugin
My pom.xml
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<webapp.host></webapp.host>
</properties>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_jdbc</artifactId>
<version>2.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
<dependency>
<groupId>kg.apc</groupId>
<artifactId>jmeter-plugins-extras-libs</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>regression-l</id>
<properties>
<directory.Name>Regression</directory.Name>
<filePath>src/test/resources/data/</filePath>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>1.8.1</version>
<dependencies>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_jdbc</artifactId>
<version>2.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
<dependency>
<groupId>kg.apc</groupId>
<artifactId>jmeter-plugins-extras-libs</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>test</phase>
<goals>
<goal>jmeter</goal>
</goals>
<configuration>
<jmeterPlugins>
<plugin>
<groupId>kg.apc</groupId>
<artifactId>jmeter-plugins-extras-libs</artifactId>
</plugin>
</jmeterPlugins>
<propertiesUser>
<user.classpath>
${project.base.directory}/jmeter/lib/ext/
</user.classpath>
<filePath>${filePath}</filePath>
<host>${webapp.host}</host>
</propertiesUser>
<resultsFileFormat>xml</resultsFileFormat>
<ignoreResultFailures>true</ignoreResultFailures>
<ignoreResultErrors>true</ignoreResultErrors>
<testResultsTimestamp>true</testResultsTimestamp>
<appendResultsTimestamp>true</appendResultsTimestamp>
<resultsFileNameDateFormat>YYYY-MM-dd</resultsFileNameDateFormat>
<testFilesIncluded>
<jMeterTestFile>${directory.Name}/Service.jmx</jMeterTestFile>
</testFilesIncluded>
</configuration>
</execution>
</executions>
</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}/jmeter/results</dir>
<stylesheet>src/test/resources/jmeter.results.detailed.xsl</stylesheet>
<outputDir>${project.build.directory}/jmeter/detailedresults</outputDir>
<fileMappers>
<fileMapper
implementation="org.codehaus.plexus.components.io.filemappers.RegExpFileMapper">
<pattern>(.*?)\s(.*?)</pattern>
<replacement>$1$2</replacement>
<replaceAll>true</replaceAll>
</fileMapper>
<fileMapper
implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
<targetExtension>.html</targetExtension>
</fileMapper>
</fileMappers>
</transformationSet>
</transformationSets>
</configuration>
</plugin>
</plugins>
</build>
Here is the minimal working pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.blazemeter</groupId>
<artifactId>mvn-jmeter</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>maven-jmeter-demo</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.1.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
If you are using extra libraries or JMeter Plugins - you will need to modify it to add missing dependencies, for "vanilla" JMeter test it should work fine given you place it the following way (folder structure):
.
├── pom.xml
└── src
└── test
└── jmeter
└── Your_Test.jmx
In case if you still experience problems we'll need full stacktrace as the partial error you've posted doesn't tell the full story therefore it is impossible to indicate the root cause.
References:
Five Ways To Launch a JMeter Test without Using the JMeter GUI
JMeter Maven Plugin
Adding additional libraries to the classpath

Maven: Invalid content was found starting with element 'plugin'

I have updated my pom.xml with plagins and got error:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'plugin'. One of '{"http://maven.apache.org/POM/
4.0.0":parent, "http://maven.apache.org/POM/4.0.0":description, "http://maven.apache.org/POM/4.0.0":prerequisites,
"http://maven.apache.org/POM/4.0.0":issueManagement, "http://maven.apache.org/POM/4.0.0":ciManagement, "http://
maven.apache.org/POM/4.0.0":inceptionYear, "http://maven.apache.org/POM/4.0.0":mailingLists, "http://
maven.apache.org/POM/4.0.0":developers, "http://maven.apache.org/POM/4.0.0":contributors, "http://maven.apache.org/
POM/4.0.0":licenses, "http://maven.apache.org/POM/4.0.0":scm, "http://maven.apache.org/POM/4.0.0":organization,
"http://maven.apache.org/POM/4.0.0":build, "http://maven.apache.org/POM/4.0.0":profiles, "http://maven.apache.org/
POM/4.0.0":modules, "http://maven.apache.org/POM/4.0.0":repositories, "http://maven.apache.org/POM/
4.0.0":pluginRepositories, "http://maven.apache.org/POM/4.0.0":reports, "http://maven.apache.org/POM/4.0.0":reporting,
"http://maven.apache.org/POM/4.0.0":dependencyManagement, "http://maven.apache.org/POM/
4.0.0":distributionManagement}' is expected.
This is pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>oracle</groupId>
<artifactId>ojdbc16</artifactId>
<version>11.2.0.1.0</version>
<type>jar</type>
</dependency>
</dependencies>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-java2ws-plugin</artifactId>
<version>2.5.1</version>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.company.app</groupId>
<artifactId>services</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<className>com.company.app.services.WebServiceBean</className>
<genWsdl>true</genWsdl>
</configuration>
<goals>
<goal>java2ws</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>process-sources</id>
<phase>generate-sources</phase>
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>${project.build.directory}/generated/wsdl/WebServiceBean.wsdl</wsdl>
<extraargs>
<extraarg>-p</extraarg>
<extraarg>com.company.app.services.client.model</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</project>
How to solve this?
You have to add the following part in your pom file:
<build>
<plugins>
<plugin>
...
</plugin>
</plugins>
</build>
You need to add your code between <plugins> </plugins>
<build>
<plugins>
<plugin>
<!-- Add here -->
</plugin>
</plugins>
</build>
In case you have multiple plugins, you will add another <plugin> tag under <plugins>.

Resources