Problem importing Java classes in groovy files Java 11 - java

I've been trying to move an application to Java 11. This applications has multiple modules that use a common pom. After refactoring the main pom.xml to Java 11 I haven't been able to import Java classes correctly from Groovy classes and I keep getting this error from mvn clean compile "Error occurred while calling a method on a Groovy class from classpath.: InvocationTargetException: startup failed:". Here's my pom file configuration.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.7.0</version>
<executions>
<execution>
<goals>
<goal>execute</goal>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>compileTests</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<formats>
<format>xml</format>
</formats>
</configuration>
<executions>
<execution>
<id>clean</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<id>instrument</id>
<phase>site</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<configuration>
<destFile>${project.basedir}/target/jacoco.exec</destFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/groovy</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/groovy</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
What am I doing wrong?

Related

How do I define schema directory in JAXB2 maven plugin version 2.4?

I am trying to define the directory for my schema in my pom.xml. I keep getting an error saying "Element schemaDirectory is not allowed here". I am guessing this element is not supported in this version. So how do define my schema directory in this version?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.basedir}src/main/resources</schemaDirectory>
<outputDirectory>${project.basedir}src/main/java</outputDirectory>
</configuration>
</plugin>
Thanks in advance!
This is valid for older version eg. 1.5:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaDirectory>${project.basedir}src/main/resources</schemaDirectory>
<outputDirectory>${project.basedir}src/main/java</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
For version 2.4, according to documentation there is no longer schemaDirectory tag https://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.4/xjc-mojo.html
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.basedir}src/main/java</outputDirectory>
<sources>
<source>${project.basedir}src/main/resources</source>
</sources>
</configuration>
</plugin>
For version 2.5.0
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<source>${project.basedir}/src/main/resources/${specify.xsd}</source>
</sources>
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
Then run mvn compile

Is it possible to only run the JaCoCo maven plugin during the site lifecycle?

Consider the follow build configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${surefireArgLine}</argLine>
<parallel>all</parallel>
<threadCount>2</threadCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>${failsafeArgLine}</argLine>
<parallel>classes</parallel>
<threadCount>2</threadCount>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</execution>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco-it.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
And reporting configuration:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<id>JaCoCo-UnitTest</id>
<reports>
<report>report</report>
</reports>
<configuration>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</reportSet>
<reportSet>
<id>JaCoCo-IntegrationTest</id>
<reports>
<report>report</report>
</reports>
<configuration>
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
</configuration>
</reportSet>
</reportSets>
</plugin>
The awesome part is mvn clean site works awesome, I get my reports. However, this now means that the JaCoCo agent is starting during the normal lifecycle, ie: mvn clean install verify.
Is there a way to disable the JaCoCo agent during the normal lifecycle but not the site cycle?
Simply skip the JaCoCo execution using a property:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Skip this phase if jacoco.skip property is set to true -->
<skip>${jacoco.skip}</skip>
<destFile>${project.build.directory}/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Skip this phase if jacoco.skip property is set to true -->
<skip>${jacoco.skip}</skip>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
You can also disable JaCoCo execution when tests are skipped:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
<!-- Skip tests if skip.unit.tests property is set to true -->
<skipTests>${skip.unit.tests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Skip this phase if unit tests are skipped -->
<skip>${skip.unit.tests}</skip>
<destFile>${project.build.directory}/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Skip this phase if unit tests are skipped -->
<skip>${skip.unit.tests}</skip>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>

Kotlin + Maven can not find main class when running packaged jar

I am trying to package(maven package) and run(java -jar file.jar) a project that contains both java and kotlin code. I have followed the tutorial at: https://michaelrice.com/2016/08/hello-world-with-kotlin-and-maven/
kotlin source src/main/kotlin
java source src/main/java
the file I am trying to run is located in src/main/kotlin/THEFILE.kt
After succesfull packaging trying to run the jar I get an error
Error: Could not find or load main class THEFILEKt
What can be the reason for this and how to fix it?
Thanks in advance!!!
Pom.xml includes a necessary kotlin plugins and dependency:
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>src/main/kotlin</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>
THEFILEKt
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
THEFILEKt
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
There are some issues when you have both java and kotlin code. I noticed the following -
If I had a folder called src/main/java, then the main class needed to be written in Java.
If I had a folder called src/main/kotlin, then the main class needed to be written in kotlin.
I did not try with both java and kotlin source folders.
I would suggest that you move all your java code to the kotlin folder. It will still work. Perhaps when there is a java folder, it expects the main class to be in java? Not sure.
I had the same problem until I deleted maven-compiler-plugin. pom.xml build block:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<!-- lombok -->
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.16.8.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- instead of default compiler -->
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>src/main/kotlin</source>
<source>target/generated-sources/annotations</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
<source>src/test/kotlin</source>
</sourceDirs>
</configuration>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
</plugins>
</build>

Ignore Classes and Methods form Code Coverage (jacoco)

In my application I want to ignore to classes (packageName1/Starter/packageName1/IO/CmdException.class) and for every class equals and hashCode method.
My pom.xml looks like:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.0.201403182114</version>
<configuration>
<instrumentation>
<ignores>
<ignore>*hashCode</ignore>
<ignore>*equals</ignore>
</ignores>
<excludes>
<exclude>packageName1/Starter.class</exclude>
<exclude>packageName1/IO/CmdException.class</exclude>
</excludes>
</instrumentation>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The problem is that coverage still threats these classes and methods.
This Works for me to exclude classes.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<configuration>
<ignores>
<ignore>com.sample.package.bean.*</ignore>
</ignores>
<excludes>
<!-- all class which we want to exclude from the coverage report -->
<exclude>**/*class1.class</exclude>
<exclude>**/*class2.class</exclude>
<exclude>**/*class3.class</exclude>
<exclude>**/*class4.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

Is there any maven sleep functionality?

I have maven profile set for testing where in pre-integration-tests maven starts two jetty servers and afterwards tests are started. The problem I've stumbled into is in the servers, they aren't fully loaded when tests start. It seems like the problem is fixed by adding 5 second sleep time into the tests, but I wish to add it in maven and remove from tests. Is there anyway I could do that? Please look into code sample below for additional information
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.work.projectname</groupId>
<artifactId>projectname-web</artifactId>
<version>${project.version}</version>
<type>war</type>
<destFileName>projectname-${project.version}.war</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.work.projectname</groupId>
<artifactId>projectname-stubs-web</artifactId>
<version>${project.version}</version>
<type>war</type>
<destFileName>projectname-stubs-${project.version}.war</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-server.version}</version>
<executions>
<execution>
<id>start-projectname</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy-war</goal>
</goals>
<configuration>
<daemon>true</daemon>
<reload>manual</reload>
<war>${project.build.directory}/projectname-${project.version}.war</war>
<webAppXml>${basedir}/src/jetty/jetty-loginService.xml</webAppXml>
<webAppConfig>
<contextPath>/projectname</contextPath>
<parentLoaderPriority>true</parentLoaderPriority>
<tempDirectory>${project.build.directory}/tmp-projectname</tempDirectory>
</webAppConfig>
<stopPort>8006</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
<execution>
<id>start-projectname-stubs</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy-war</goal>
</goals>
<configuration>
<daemon>true</daemon>
<reload>manual</reload>
<war>${project.build.directory}/projectname-stubs-${project.version}.war</war>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>${stub-jetty-server.port}</port>
<maxIdleTime>300000</maxIdleTime>
</connector>
</connectors>
<stopPort>8008</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
<execution>
<id>stop-projectname</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<stopPort>8006</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
<execution>
<id>stop-projectname-stubs</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<stopPort>8008</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<configuration>
<parallel>classes</parallel>
<threadCountClasses>33</threadCountClasses>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You can do it with the help of the maven antrun plugin. Something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<configuration>
<tasks>
<sleep seconds="5" />
</tasks>
</configuration>
<executions>
<execution>
<id>sleep-for-a-while</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
EDIT: In case someone will catch this. Based on the comment from jl, tasks have indeed been deprecated and here is the same thing based on using targets instead. Sligthly reorganized but has the same function.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>sleep-for-a-while</id>
<phase>pre-integration-test</phase>
<configuration>
<target>
<sleep seconds="5" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I have created a maven-sleep-plugin once. Not sure how much it works now, you can try and let us know.
Usage (after checking out the source and building):
<plugin>
<groupId>org.jboss.maven.plugins</groupId>
<artifactId>maven-sleep-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>sleep-5-seconds</id>
<phase>pre-integration-test</phase>
<goals>
<goal>sleep</goal>
</goals>
<configuration>
<delay>5</delay>
</configuration>
</execution>
</executions>
</plugin>

Categories

Resources