aspectj weaving already compiled JAR artifacts for war web application - jar

I'm trying to wave org.apache.camel-camel-xmpp-2.20.2.jar maven artifact so that in the end my web application (war packaging) to include it into WEB-INF/lib. After building I'm decompiling target/my-web-app-exploded-war/WEB-INF/lib/org.apache.camel-camel-xmpp-2.20.2.jar/org/apache/camel/component/xmpp/XmppMessage.class hoping to find my modification or at least some aspecj processing trace but I don't.
Q: what should I do in order to have org.apache.camel-camel-xmpp-2.20.2.jar changed according to my aspect?
Using java 8, aspectj 1.8.9.
excerpt from building log:
[INFO] --- aspectj-maven-plugin:1.10:compile (default) # ddcapplication-ddcwebservice ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[WARNING] You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.
Your processor is: org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.BatchProcessingEnvImpl
Lombok supports: sun/apple javac 1.6, ECJ
<unknown source file>:<no line information>
[WARNING] advice defined in org.apache.camel.component.xmpp.XmppMessageAspect has not been applied [Xlint:adviceDidNotMatch]
/home/adrianpetre/SAGS/DDCWorkspace/ddc/ddcapplication/ddcwebservice/src/main/java/org/apache/camel/component/xmpp/XmppMessageAspect.aj:16
src/main/java/org/apache/camel/component/xmpp/XmppMessageAspect.aj:
package org.apache.camel.component.xmpp;
public aspect XmppMessageAspect {
pointcut newInstancePointcut():
call(public * org.apache.camel.component.xmpp.XmppMessage.newInstance());
Object around(): newInstancePointcut() {
return new XmppMessage();
}
}
src/main/resources/META-INF/aop.xml:
<aspectj>
<weaver options="-verbose -showWeaveInfo">
<include within="org.apache.camel.component.xmpp.XmppMessage"/>
</weaver>
<aspects>
<aspect name="XmppMessageAspect"/>
</aspects>
</aspectj>
aspectj-maven-plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration combine.self="override">
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<privateScope>true</privateScope>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<xmlConfigured>src/main/resources/META-INF/aop.xml</xmlConfigured>
<weaveMainSourceFolder>false</weaveMainSourceFolder>
<weaveDependencies>
<weaveDependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xmpp</artifactId>
</weaveDependency>
</weaveDependencies>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<!--
I tried with these too:
generate-resources, compile, prepare-package, package
-->
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
My pom.xml includes:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xmpp</artifactId>
<version>2.20.2</version>
<exclusions>
<exclusion>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
</dependency>

Related

javafx gluon mobile : reflection classes not used

I am building a mobile gluon javafx application. App runs fine in the jvm but not on mobile.
I found out that i had ClassNotFoundException when loading the FXML and discovered that it would not find java.net.URL(!!) The unfound classes were not present in the projet arm64_ios reflection files, but i somewhat expected that as i did not directly use those classes in the code. Unfortunately, gluon seems not to be able to parse the fxml to add known classes. But i disgress.
Just to validate the problem, i added this class name to my POM reflectionList. As expected, it would find it but block on an other one, then an other one, this was endless.
I check out that when i ran gluonfx:runagent, the missing classes were correctly added to src/main/resources/META-INF/native-image/reflect-config.json.
As per https://docs.gluonhq.com/#_jni_and_reflection , i added a META-INF/substrate/congif/reflectionconfig.json file that was a copy of the META-INF/native-image/reflect-config.json. That did not help.
I do not know why the runagent files are not used, i think i might have fumbled somewhere. I will be very grateful for any hint. Thanks.
Here are the relevant parts of my POM.
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
<javafx.version>17.0.2</javafx.version>
<attach.version>4.0.13</attach.version>
<gluonfx.plugin.version>1.0.12</gluonfx.plugin.version>
<javafx.plugin.version>0.0.8</javafx.plugin.version>
<mainClassName>com.blah</mainClassName>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>com.gluonhq</groupId>
<artifactId>charm-glisten</artifactId>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>com.gluonhq.attach</groupId>
<artifactId>display</artifactId>
<version>${attach.version}</version>
</dependency>
<dependency>
<groupId>com.gluonhq.attach</groupId>
<artifactId>lifecycle</artifactId>
<version>${attach.version}</version>
</dependency>
<dependency>
<groupId>com.gluonhq.attach</groupId>
<artifactId>statusbar</artifactId>
<version>${attach.version}</version>
</dependency>
<dependency>
<groupId>com.gluonhq.attach</groupId>
<artifactId>storage</artifactId>
<version>${attach.version}</version>
</dependency>
<dependency>
<groupId>com.gluonhq.attach</groupId>
<artifactId>util</artifactId>
<version>${attach.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>Gluon</id>
<url>https://nexus.gluonhq.com/nexus/content/repositories/releases</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>${javafx.plugin.version}</version>
<configuration>
<mainClass>${mainClassName}</mainClass>
</configuration>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
</execution>
<execution>
<!-- Configuration for manual attach debugging -->
<!-- Usage: mvn clean javafx:run#debug -->
<id>debug</id>
<configuration>
<options>
<option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000</option>
</options>
</configuration>
</execution>
<execution>
<!-- Configuration for automatic IDE debugging -->
<id>ide-debug</id>
<configuration>
<options>
<option>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</option>
</options>
</configuration>
</execution>
<execution>
<!-- Configuration for automatic IDE profiling -->
<id>ide-profile</id>
<configuration>
<options>
<option>${profiler.jvmargs.arg1}</option>
<option>${profiler.jvmargs.arg2}</option>
<option>${profiler.jvmargs.arg3}</option>
<option>${profiler.jvmargs.arg4}</option>
<option>${profiler.jvmargs.arg5}</option>
</options>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.gluonhq</groupId>
<artifactId>gluonfx-maven-plugin</artifactId>
<version>${gluonfx.plugin.version}</version>
<configuration>
<verbose>true</verbose>
<target>${gluonfx.target}</target>
<attachList>
<list>display</list>
<list>lifecycle</list>
<list>statusbar</list>
<list>storage</list>
</attachList>
<reflectionList>
<list>java.net.URL</list>
</reflectionList>
<mainClass>${mainClassName}</mainClass>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
<include>**/*.png</include>
<include>*.png</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
<include>**/*.png</include>
<include>*.png</include>
</includes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>ios</id>
<properties>
<gluonfx.target>ios</gluonfx.target>
</properties>
</profile>
<profile>
<id>android</id>
<properties>
<gluonfx.target>android</gluonfx.target>
</properties>
</profile>
</profiles>
Ok, now i found a solution, not sure i understood what happens.
Everything was caused by my resources includes. Not sure why/if i added them, not sure where they came from. It seems that configuring includes removes the default ones and that forbid the inclusion of the json files, and thus i had no other defined reflections than those that were found at compilation.
Removing the includes section of the POM and adding <nativeImageArgs>--allow-incomplete-classpath</nativeImageArgs> within the POM finally cleared all compilation blurps.
This post helped me for this last tip: Why is my JavaFX 17 Application not building with Gluon ? Logged as JNI / Reflection problem
Thanks José for coming by.

not able to build executable jar using maven shade plugin

i need to check the anagram java file using the maven shade plugin and need to add the manifest and build number . i have tried below code.
<project XML="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.fresco.play</groupId>
<artifactId>maven-anagram-finder</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven-anagram-finder</name>
<url>http://maven.apache.org</url>
<!-- Insert test dependency here -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<configuration>
<transformers>
<transformer>
<manifestEntries>
<mainClass>com.fresco.play.Anagram</mainClass>
<Build-Number>10</Build-Number>
</manifestEntries>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
this is the error i am getting while trying to build using the jdk1.8 version. all the test cases are passing but build is failing with this error
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 48.286 s
[INFO] Finished at: 2020-06-25T09:23:24+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.1:shade (default) on project maven-anagram-finder: Unable to parse configuration of mojo org.apache.maven.plugins:maven-shade-plugin:2.1:shade for parameter transformer: Cannot create instance of interface org.apache.maven.plugins.shade.resource.ResourceTransformer: org.apache.maven.plugins.shade.resource.ResourceTransformer.<init>() -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginConfigurationException
i have tried all the ways. but not able to resolve. could you please help on this??
i have resolved the issue by adding the transformer implementation. Thank you.
4.0.0
com.fresco.play
maven-anagram-finder
jar
0.0.1-SNAPSHOT
maven-anagram-finder
http://maven.apache.org
<!-- Insert test dependency here -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
</dependency>
</dependencies>
<build>
<finalName>maven-anagram-finder</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.fresco.play.Anagram</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.fresco.play.Anagram</Main-Class>
<Build-Number>10</Build-Number>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Payara Micro: How to log with slf4j (or log4j2)?

I'm using Payara Micro (bundled ueberjar) for a recent project, but I have difficulties with logging. Seems like Payara Micro uses JUL by default, which does not suit my needs. I'd like to use Log4J 2 instead, preferably through slf4j. Unfortunately, I couldn't find much information. To start with, I'd like refer to the following link...
https://blog.payara.fish/the-basics-of-logging-in-payara-server
... which says: "Payara Micro can also be adjusted to use other logging frameworks like Logback and Log4J2." Sounds great, but the only source that deals with that matter seems to be the following example project: https://github.com/hei1233212000/payara-micro-log4j2. Yet it is from 2017 and seems to be outdated as it doesn't use the payara micro maven plugin. Still, I guess the point is:
add the necessary logging jars to the bundle
adjust Manifest file by adding the jars to the classpath
use the SLF4JBridgeHandler for Payara Micro
I tried my luck adding the jars as customJars via the payara micro maven plugin, which indeed resulted in a bundled jar containing those libs under MICRO-INF/lib. From what I read, the jars should also be on classpath, though they don't appear in the Manifest file. Also, I added the logging.properties tih the following simple content under src/main/resources:
handlers=org.slf4j.bridge.SLF4JBridgeHandler
Now, if I run the bundled jar, it says Can't load log handler "org.slf4j.bridge.SLF4JBridgeHandler", followed by an ugly stacktrace. Yet the class org.slf4j.bridge.SLF4JBridgeHandler is in one of the jars I added. I already experimented with the groovy script from the example I linked above to edit the Manifest file, but I couldn't figure out how to set it up properly. I mean, the script worked and I get an edited Manifest file, but it is not added to the bundled jar - I guess my timing is bad. Not to mention that this is kind of hackish as the author of the example said.
Interestingly, if I don't add the logging.properties to the jar, thus leaving Payara Micros logging setup untouched, I can reroute logging output from 3rd party libraries (such as hibernate) coming with Payara Micro while its own logs are logged to the console. Yet that's not my goal as I am more interested in the latter logs.
So, I'd be thankful if someone could give me a hand. Thanks for reading. For completeness, here is my pom.xml (I am using the package profile, the other is for cucumber tests only):
<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>de.kepes.payara-micro</groupId>
<artifactId>payara-micro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
<skipTests>true</skipTests>
<payara-micro.version>5.194</payara-micro.version>
<payara-micro.plugin.version>1.0.6</payara-micro.plugin.version>
<jakarta.version>8.0.0</jakarta.version>
<maven-failsafe.plugin.version>2.22.2</maven-failsafe.plugin.version>
<cucumber.version>5.4.0</cucumber.version>
<websocket.version>1.4.0</websocket.version>
<log4j.version>2.13.0</log4j.version>
<slf4j.version>1.7.30</slf4j.version>
</properties>
<profiles>
<profile>
<id>package</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>fish.payara.maven.plugins</groupId>
<artifactId>payara-micro-maven-plugin</artifactId>
<version>${payara-micro.plugin.version}</version>
<executions>
<execution>
<id>bundle</id>
<phase>package</phase>
<goals>
<goal>bundle</goal>
</goals>
</execution>
<execution>
<id>start</id>
<goals>
<goal>start</goal>
</goals>
</execution>
</executions>
<configuration>
<useUberJar>true</useUberJar>
<deployWar>true</deployWar>
<payaraVersion>${payara-micro.version}</payaraVersion>
<customJars>
<customJar>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</customJar>
<customJar>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</customJar>
<customJar>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</customJar>
<customJar>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</customJar>
<customJar>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</customJar>
</customJars>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>test</id>
<properties>
<skipTests>false</skipTests>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe.plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<groupId>fish.payara.maven.plugins</groupId>
<artifactId>payara-micro-maven-plugin</artifactId>
<version>${payara-micro.plugin.version}</version>
<executions>
<execution>
<id>pre-integration-payara</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>post-integration-payara</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<payaraVersion>${payara-micro.version}</payaraVersion>
<deployWar>true</deployWar>
<contextRoot>/</contextRoot>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>${jakarta.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>${websocket.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
this isn't possible just by adding logging libraries as custom JARs as logging is initialized before those libraries are loaded.
However, there's a solution how to use alternative logging libraries. You need to run Payara Micro in a different way. If you put it on the classpath and run the Payara Micro main class directly, you can put custom logging libraries on the classpath too and they will be picked up at boot time, before logging is initialized. If you have payara-micro.jar, slf4j.jar, log4j.jar and jul-to-slf4j.jar in the current directory, you can launch Payara Micro like this:
java -cp ./payara-micro.jar:slf4j.jar:log4j2.jar:jul-to-slf4j.jar fish.payara.micro.PayaraMicro some.war
Alternatively, you can move those logging JARs to a subdirectory lib and shorten the command line:
java -cp "./payara-micro.jar:lib/*" fish.payara.micro.PayaraMicro some.war
You can pass the same arguments to the PayaraMicro class which are accepted by the Payara Micro JAR.

ClassNotFoundException in Maven project

There is a maven based project, which can be built by maven install, but throw exception when run the out jar:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/CommandLineParser
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.CommandLineParser
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I want to know why java can not find the maven dependency library ?
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.artofsolving.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>3.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>JODConverter - Core Library</name>
<description>
JODConverter converts office documents using OpenOffice.org
</description>
<url>http://jodconverter.googlecode.com</url>
<licenses>
<license>
<name>GNU Lesser General Public License, Version 3 or later</name>
<url>http://www.gnu.org/licenses/lgpl.html</url>
</license>
</licenses>
<scm>
<url>http://jodconverter.googlecode.com/svn/trunk/jodconverter-core</url>
<connection>scm:svn:https://jodconverter.googlecode.com/svn/trunk/jodconverter-core/</connection>
</scm>
<inceptionYear>2003</inceptionYear>
<developers>
<developer>
<id>mirko.nasato</id>
<name>Mirko Nasato</name>
<email>mirko#artofsolving.com</email>
</developer>
</developers>
<contributors>
<contributor>
<name>Shervin Asgari</name>
<email>shervin#asgari.no</email>
</contributor>
</contributors>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<!-- required for org.hyperic:sigar -->
<id>jboss-public-repository-group</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<dependencies>
<!-- Required dependencies -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<!-- for the command line tool -->
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<!-- Optional dependencies -->
<dependency>
<!-- for more robust process management -->
<groupId>org.hyperic</groupId>
<artifactId>sigar</artifactId>
<version>1.6.5.132</version>
<optional>true</optional>
</dependency>
<dependency>
<!-- for JSON configuration -->
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
<optional>true</optional>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<configuration>
<!-- don't run tests in parallel -->
<perCoreThreadCount>false</perCoreThreadCount>
<threadCount>1</threadCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<mainClass>org.artofsolving.jodconverter.cli.Convert</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/dist.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</reporting>
</project>
When running your Jar with java -jar, you're no longer running with the maven resolving of dependencies. You have to make sure that all the jars you depend on are available at the relative loacation specificied in the MANIFEST.mf file in your Jar. Usually, you would use the assembly plugin to create a distributable archive that creates this structure. Or you could use a uberjar/onejar plugin to included everything inside your Jar.
You have 2 options to fix this:
Option 1 - use both plugins below to copy dependencies to a "lib" folder (see outputDirectory) and make your Runnable JAR know where they are (see classpathPrefix)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>example.MyMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
Option 2 - Use maven-assembly-plugin to create a single JAR with all dependencies included. (all dependencies are extracted and compressed together in the same JAR)
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>compile</scope>
</dependencySet>
</dependencySets>
I've seen the problem, too. Cobertura creates instrumented copies of all compiled classfiles (in target/classes) by bytecode manipulation. My jar file contained the instrumented class files (from target/generated-classes/cobertura), but cobertura.jar was missing. So, ClassNotFoundException was the right reaction.
However, since my POM doesn't contain any hint, which are the intended classes for packaging, Maven has the free choice ... and it uses the instrumented set of files.
My solution was really simple: mvn clean and mvn package. The first call removes all instrumented classes. Then, the second call finds only the uninstrumented classes without any external references to Cobertura.

hbm2java failed:Unable to load class declared as <mapping class="com.dss.domain.Foo"/>

I am implementing generation of the domain/model POJOs from database using the Hibernate3 Maven Plugin. The rationale is to ensure a DBA's updates to the database are automatically mapped to the model layer before a developer starts working on further things. So the way it has to work is that a Hibernate CFG is generated and then POJOs; also since the older implementation consisted of developers using annotations instead of hbm.xml the generated classes are required to be annotated. Here's extract from the POM for Hibernate Maven Plugin
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>hbm2cfgxml</id>
<phase>generate-resources</phase>
<goals>
<goal>hbm2cfgxml</goal>
</goals>
<inherited>false</inherited>
<configuration>
<components>
<component>
<name>hbm2cfgxml</name>
<implementation>jdbcconfiguration</implementation>
</component>
</components>
<componentProperties>
<ejb3>true</ejb3>
<packagename>com.dss.domain</packagename>
</componentProperties>
</configuration>
</execution>
<execution>
<id>hbm2java</id>
<phase>generate-sources</phase>
<goals>
<goal>hbm2java</goal>
</goals>
<inherited>false</inherited>
<configuration>
<components>
<component>
<name>hbm2java</name>
<implementation>annotationconfiguration</implementation>
</component>
</components>
<componentProperties>
<ejb3>true</ejb3>
<packagename>com.dss.domain</packagename>
<configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
</componentProperties>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
</dependency>
</dependencies>
</plugin>
</plugins>
I can see the cfg.xml file is generated; but hbm2java fails with message
Failed to execute goal
org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java (hbm2java) on
project dss-domain: Execution hbm2java of goal
org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java failed: Unable
to load class declared as < mapping
class="com.dss.domain.Foo" / > in the configuration: -> [Help
1]
At a later stage all of this has to be moved the JPA implementation that we currently have, so the other question is do I then have to switch to jpaconfiguration in component properties?
Also none of these seems to work at all if I update the dependencies to the ones recently uopdated in the older project (Hibernate 3.6.6-FINAL); but that's a separate question posted here.
Any pointers or complete solutions are very welcome ;-)
I am using hibernate with mysql built with maven. Instead of running hbm2hbmxml I have changed my execution goals to only run hbm2cfgxml and hbm2java. Now my project generates annotation based pojos and hibernate.cfg.xml.
Hope this helps!
See my configuration:
<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.springpress</groupId>
<artifactId>hibernate</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hibernate</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>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.1.1.RELEASE</version>
<!-- will come with all needed Spring dependencies such as spring-core
and spring-beans -->
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.1.Final</version>
<!-- will come with Hibernate core -->
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>generate-xml-files</id>
<phase>generate-resources</phase>
<goals>
<!-- <goal>hbm2hbmxml</goal> -->
<goal>hbm2cfgxml</goal>
</goals>
</execution>
<execution>
<id>generate-entities</id>
<phase>generate-sources</phase>
<goals>
<goal>hbm2java</goal>
</goals>
</execution>
</executions>
<configuration>
<components>
<component>
<name>hbm2hbmxml</name>
<implementation>jdbcconfiguration</implementation>
<outputDirectory>target/classes</outputDirectory>
</component>
<component>
<name>hbm2cfgxml</name>
<implementation>jdbcconfiguration</implementation>
<outputDirectory>target/classes</outputDirectory>
</component>
<component>
<name>hbm2java</name>
<implementation>jdbcconfiguration</implementation>
<outputDirectory>target/generated-sources/hibernate</outputDirectory>
</component>
</components>
<componentProperties>
<propertyfile>src/main/resources/hibernate.properties</propertyfile>
<jdk5>true</jdk5>
<ejb3>true</ejb3>
<packagename>com.springpress.hibernate.entities</packagename>
<format>true</format>
<haltonerror>true</haltonerror>
</componentProperties>
</configuration>
<dependencies>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.19</version>
</dependency></dependencies>
</plugin>
</plugins>
</build>
</project>
And I have hibernate.properties like:
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/mydb
hibernate.connection.username=root
hibernate.connection.password=pass
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.default_schema=mydb
I was browsing through and saw a similar post (not sure how I missed it in the first place) but anyways, when I add an additional hbm2hbmxml to my build; the build does not fail in error
<execution>
<id>hbm2hbmxml</id>
<phase>generate-resources</phase>
<goals>
<goal>hbm2hbmxml</goal>
</goals>
<inherited>false</inherited>
<configuration>
<components>
<component>
<name>hbm2hbmxml</name>
<outputDirectory>target/classes</outputDirectory>
</component>
</components>
<componentProperties>
<packagename>com.sapient.dss.dbci.domain</packagename>
</componentProperties>
</configuration>
</execution>
But this is not the solution I am looking for. When I see hibernate.cfg.xml it is using mapping resources pointing to .hbm.xmls; and the generated java sources are using JPA annotations!!!
the hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/liquibrain</property>
<property name="hibernate.connection.username">liquibrain</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping resource="com/dss/domain/Foo.hbm.xml" />
<mapping resource="com/dss/domain/Bar.hbm.xml" />
</session-factory>
</hibernate-configuration>
and here's an extract from the generated Java source:
/**
* Foo generated by hbm2java
*/
#Entity
#Table(name="iteration"
,catalog="liquibrain"
)
public class Foo implements java.io.Serializable {
...
...
#Id #GeneratedValue(strategy=IDENTITY)
#Column(name="id", nullable=false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
...
...
...
#ManyToMany(fetch=FetchType.LAZY)
#JoinTable(name="bar_foos", joinColumns = {
#JoinColumn(name="foo_id", nullable=false, updatable=false) }, inverseJoinColumns = {
#JoinColumn(name="bar_id", nullable=false, updatable=false) })
public Set getBars() {
return this.bars;
}
Both the hbm files and java sources get packaged in the JAR, but since the hibernate.cfg.xml mentions mapping through .hbm.xml I belibe thats how it will be reffered. So isn't there a way to generate the java source without having to duplicate the info in form of both mappings and annotation configurations in POJOs? Makes me more confused about the plugin now than before.

Resources