Jaxb2 maven plugin getting error when generating xsd from complex classes - maven

I have a case that i have 35 classes that some of them related with each other inside of them. Such as;
Addendum.java
#XmlType(name="addendum",namespace= GenericNameSpaceConstants.POLICY_NAMESPACE_URI)
#XmlAccessorType(XmlAccessType.FIELD)
public class Addendum implements Serializable {
#XmlElement(name="changeNumber",nillable=false,required=true)
private Long changeNumber;
#XmlElement(name="changeTypeDesc",nillable=false,required=true)
private String changeTypeDesc;
#XmlElement(name="changeTypeId",nillable=false,required=true)
private Integer changeTypeId;
}
Policy.java
#XmlRootElement(name="policy",namespace=GenericNameSpaceConstants.POLICY_NAMESPACE_URI)
#XmlType(name="policy",namespace= GenericNameSpaceConstants.POLICY_NAMESPACE_URI)
#XmlAccessorType(XmlAccessType.FIELD)
public class Policy {
#XmlElement(name="addendum",required=true,nillable=false)
private Addendum addendum;
}
My jaxb schemage config in pom file like that
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<createJavaDocAnnotations>false</createJavaDocAnnotations>
<sources>
<source>
${project.basedir}\src\main\java\com\aegon\common\service\bean\
</source>
</sources>
<verbose>true</verbose>
<outputDirectory>${basedir}/src/main/resources/schemas</outputDirectory>
<transformSchemas>
<transformSchema>
<toPrefix>pol</toPrefix>
<toFile>policy_model_v2.xsd</toFile>
</transformSchema>
</transformSchemas>
<generateEpisode>true</generateEpisode>
</configuration>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>schemagen</goal>
</goals>
</execution>
</executions>
</plugin>
When i run the project for phase generate-resources or generate-sources. I am getting this error Addendum is a non-static inner class, and JAXB can't handle those.
How can i resolve this problem?? How can i generate all classes xsd in a simple xsd Or how can i create xsds' one by one and import to complex one

I have found the problem. every class need a default constructor

Related

How to create executable jar in Kotlin using maven-assembly-plugin without a main class?

I want to create an executable jar of my Kotlin codebase using the maven-assembly-plugin. In Kotlin, the main class does not have to be part of a class necessarily but the plugin wants to hear a class.
If I do create a main class, then there is no problem. Let's say I have a main class:
MyApplication.kt
package com.my.application
class MyApplication {
companion object {
#JvmStatic
fun main(args: Array<String>) {
.. do stuff here ..
}
}
}
I also configured the plugin:
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
com.my.application.MyApplication
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
The above works. I can execute the jar successfully. However, there is no need in Kotlin to necessarily have the class with the companion object and then the annotation.
I could just type:
MyApplication.kt
import com.my.application
fun main(args: Array<String>) {
.. do stuff here ..
}
But if I then execute the jar I get an exception:
Error: Could not find or load main class com.my.application.MyApplication
Caused by: java.lang.ClassNotFoundException: com.my.application.MyApplication
How can I make this work?
Kotlin will implicitly compile to a class which is derived from the filename suffixed with Kt. In your case it results to com.my.application.MyApplicationKt
https://kotlinlang.org/docs/java-to-kotlin-interop.html#package-level-functions

AspectJ binary weaving with Jcabi Maven plugin not working for Kotlin code

I'm trying to run a little annotation over function that will log before and after the method execution.
What I've done: (all classes are under src/main/kotlin)
Annotation class
#Target(AnnotationTarget.FUNCTION)
#Retention(AnnotationRetention.RUNTIME)
annotation class LogMe
Aspect class
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
#Aspect
abstract class Aspect {
#Around("#annotation(LogMe) && execution(* *(..))")
fun logMe(joinPoint: ProceedingJoinPoint): Any {
beforeExecution(joinPoint)
afterExecution(joinPoint)
return joinPoint.proceed()
}
private fun beforeExecution(joinPoint: JoinPoint) {
println("[${joinPoint.signature.name} has started its execution]")
}
private fun afterExecution(joinPoint: JoinPoint) {
println("[${joinPoint.signature.name} has ended its execution]")
}
}
Foo class with annotated method
class Foo {
#LogMe
fun yourMethodAround() {
println("Executing foo.yourMethodAround()")
}
}
main file
fun main(args: Array<String>) {
val foo = Foo()
foo.yourMethodAround()
}
my POM.xml (cut version)
...
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.3.40</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>1.3.40</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.4</version>
</dependency>
<!-- TEST -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>1.3.40</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit</artifactId>
<version>1.3.40</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
<groupId>org.jetbrains.kotlin</groupId>
<version>1.3.40</version>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals> <goal>test-compile</goal> </goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<version>0.14.1</version>
<executions>
<execution>
<goals>
<goal>ajc</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>MainKt</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
When I basically run this main, what I'm obtaining is the println that it's into my Foo class method:
Executing foo.yourMethodAround()
But I'm not getting the before and after execution prinln that I was expecting from the Aspect class.
Does any of you ever faced this issue before? This is struggling me, because I can't understand what's going on here.
Disclaimer:
I have never used the Jcabi plugin before, normally I always use AspectJ Maven plugin, also for binary weaving.
I have never used the Kotlin language before, normally I use Java or Groovy.
Now some things are not okay in your aspect:
It must not be abstract, otherwise no instance can be created.
For void methods it must be able to return null, so the Kotlin return type should be Any?
You should proceed() in between the before and after log messages, otherwise the log output will be wrong.
Assuming that your classes, especially the annotation class, do not reside in the default package but have an actual package name, you need to use the fully qualified class name in your pointcut, e.g. #annotation(de.scrum_master.app.LogMe)
Using an aspect class name Aspect, i.e. the same name as the #Aspect annotation, just in another package, is kind of ugly. You should rename it.
For me this works nicely:
package de.scrum_master.aspect
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
#Aspect
class LogAspect {
#Around("#annotation(de.scrum_master.app.LogMe) && execution(* *(..))")
fun logMe(joinPoint: ProceedingJoinPoint): Any? {
beforeExecution(joinPoint)
val result = joinPoint.proceed()
afterExecution(joinPoint)
return result
}
private fun beforeExecution(joinPoint: JoinPoint) {
println("[${joinPoint.signature.name} has started its execution]")
}
private fun afterExecution(joinPoint: JoinPoint) {
println("[${joinPoint.signature.name} has ended its execution]")
}
}
Besides, maybe you also should configure the Jcabi plugin to language level Java 8. It works without it here, but maybe it is better depending on which language features you use:
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
My console after mvn clean verify looks like this:
$ java -jar target/so-aj-kotlin-56890630-1.0-SNAPSHOT.jar
[yourMethodAround has started its execution]
Executing foo.yourMethodAround()
[yourMethodAround has ended its execution]
My IDE IntelliJ IDEA does not quite pick up the binary weaving stuff because it does not know Jcabi, only AspectJ Maven. So I just configured the project to delegate compilation to Maven:
Then the log output is the same when running the application from IDEA directly.

Cucumber Parallel Testing giving weird results

I'm trying to run my cucumber project (two runner classes) in parallel browsers and I am getting weird results. When I do a mvn verify, first it will run each runner class sequentially. The first will pass and the second will fail due to the following error -
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Then right after, it will run both runner classes in parallel (like I want it to), and all will pass just fine. And maven will report the Build Success.
I am not initializing the webdriver in the #Before annotation. Instead I am using cucumber-picocontainer dependency injection right into my step definition classes. I have tried swapping driver.close() and driver.quit() in my #After annotation, but that didn't change the results. Please find some code snippets below and then my POM. Many thanks in advance.
public class GivenSteps {
WebDriver driver;
CustomWaits waits;
public GivenSteps(DependencyInjection dependencyInjection) {
this.driver = dependencyInjection.getDriver();
this.waits = dependencyInjection.getWaits();
}
Hooks -
public class Hooks {
WebDriver driver;
public Hooks(DependencyInjection dependencyInjection) {
this.driver = dependencyInjection.getDriver();
}
#Before("#setup")
public void setUp() {
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
}
#After("#destroy")
public void tearDown() throws Throwable {
//driver.close();
driver.quit();
}
Dependency Injection -
public class DependencyInjection {
private static String browserType = Settings.BROWSER.getValue();
private static WebDriver driver = null;
private static CustomWaits waits = null;
public WebDriver getDriver() {
if (driver == null) {
driver = utilities.DriverFactory.createDriver(browserType);
}
return driver;
}
POM.xml -
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<includes>
<exclude>
**/*Runner.java
</exclude>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>com.github.temyers</groupId>
<artifactId>cucumber-jvm-parallel-plugin</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<id>generateRunners</id>
<phase>generate-test-sources</phase>
<goals>
<goal>generateRunners</goal>
</goals>
<configuration>
<glue>
<package>test.java.stepDefinitions</package>
</glue>
<outputDirectory>target/generated-test-sources/cucumber</outputDirectory>
<featuresDirectory>src/test/resource/</featuresDirectory>
<cucumberOutputDir>target/Reports/</cucumberOutputDir>
<namingPattern>Runner{c}</namingPattern>
<!-- One of [SCENARIO, FEATURE]. SCENARIO generates one runner per
scenario. FEATURE generates a runner per feature. -->
<parallelScheme>FEATURE</parallelScheme>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>acceptance-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<forkCount>10</forkCount>
<reuseForks>true</reuseForks>
<includes>
<include>**/*Runner.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
I had to comment out the section in my POM.xml -> maven-surefire-plugin because now I am only using maven-failsafe-plugin
Originally I had both active in my POM, so both were running sequentially.

ProGuard + Spring Boot + Maven Plugin

Guys, I'm trying to obfuscate a .jar application using the proguard-maven-plugin.
When I try to perform the obfuscate process, I get error messages stating that there are unexpected classes.
I'm using the Spring Boot 1.4.1.RELEASE and Proguard Maven Plugin 2.0.13.
This is my proguard.conf
-injars /workspace/base/target/test-1.0.0.jar
-libraryjars /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/rt.jar
-dontshrink
-dontoptimize
-dontobfuscate
-dontusemixedcaseclassnames
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod
-adaptresourcefilenames **.properties
-adaptresourcefilecontents **.properties,META-INF/MANIFEST.MF
-dontpreverify
-verbose
-keepclasseswithmembers public class * {
public static void main(java.lang.String[]);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * extends java.beans.BeanInfo
-keep class * {
void set*(***);
void set*(int,***);
boolean is*();
boolean is*(int);
*** get*();
*** get*(int);
}
-assumenosideeffects public class java.lang.System {
public static long currentTimeMillis();
static java.lang.Class getCallerClass();
public static int identityHashCode(java.lang.Object);
public static java.lang.SecurityManager getSecurityManager();
public static java.util.Properties getProperties();
public static java.lang.String getProperty(java.lang.String);
public static java.lang.String getenv(java.lang.String);
public static java.lang.String mapLibraryName(java.lang.String);
public static java.lang.String getProperty(java.lang.String,java.lang.String);
}
The pom.xml file. I am only informing the configuration by the plugin.
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.13</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<obfuscate>false</obfuscate>
<outFilter>**/BOOT-INF/classes/ **.class</outFilter>
<proguardInclude>${basedir}/proguard.conf</proguardInclude>
<outputDirectory>${project.build.directory}</outputDirectory>
<injar>${project.build.finalName}.jar</injar>
<outjar>${project.build.finalName}-min.jar</outjar>
</configuration>
</plugin>
However, during the execution process I get the following return for all classes in my application.
Warning: class [BOOT-INF/classes/br/com/base/BaseApplication.class] unexpectedly contains class [br.com.base.BaseApplication]
Warning: class [BOOT-INF/classes/br/com/base/controller/CaixaController.class] unexpectedly contains class [br.com.base.controller.CaixaController]
[...]
And the final output of ProGuard. PS: All classes are in the BOOT-INF/classes directory
Warning: there were 97 classes in incorrectly named files.
You should make sure all file names correspond to their class names.
The directory hierarchies must correspond to the package hierarchies.
(http://proguard.sourceforge.net/manual/troubleshooting.html#unexpectedclass)
If you don't mind the mentioned classes not being written out,
you could try your luck using the '-ignorewarnings' option.
Please correct the above warnings first.
Can anyone imagine any alternatives I can try?
Thanks.
In order to fix this, I made sure to change the order of the plugins in the pom. The proguard plugin should go first, followed by the spring boot plugin.
Additionally, make sure you have the <goal>repackage</goal> specified in the spring boot configuration. With the correct order and the repackage goal specified, the proguard obfuscation/optimization/whatever you have configured will take place and produce a jar. Then the spring boot plugin will repackage that jar as an executable and everything should work.
My plugin configuration from pom.xml:
<project ...>
....
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<proguardInclude>${basedir}/proguard.conf</proguardInclude>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jce.jar</lib>
</libs>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<start-class>org.springframework.boot.loader.JarLauncher</start-class>
</configuration>
</execution>
</executions>
</plugin>
...

How to define #Parameter annotation in Maven POM

I wrote a Mojo Plugin and set two #Parameter (import org.apache.maven.plugins.annotations.Parameter;)
I want to configure the Parameters in the POM of the project where I want to use this plugin.
No matter where everytime I get an error message.
The part of the POM:
<plugin>
<groupId>com.tup.test</groupId>
<artifactId>versionsextra</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>path</id>
<phase>test</phase>
<configuration>
<path>${basedir}/src/main/resources/configsys/dev/etc/deploy_env</path>
</configuration>
</execution>
</executions>
So one of the Parameter is called path:
#Parameter()
private String path;
ok, I got it.
I have to declare it like this:
#Mojo(name="devversion")
public class ParameterMojo extends AbstractMojo {
#Parameter()
private String path;
#Parameter()
private String pathsave;
...
And in POM:
<plugin>
<groupId>com.tup.test</groupId>
<artifactId>versionsextra</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>testen</id>
<phase>initialize</phase>
<goals>
<goal>devversion</goal>
</goals>
<configuration>
<path>${basedir}/src/main/resources/configsys/dev/etc/deploy_env</path>
<pathsave>${basedir}/src/main/resources/configsys/dev/etc/test.txt</pathsave>
</configuration>
</execution>
</executions>
</plugin>

Resources