Partial compile time weaving third party jar with maven - java

I've configured weaving third party jar with maven plugin aspectj-maven-plugin.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<!--<proceedOnError>true</proceedOnError>-->
<weaveDependencies>
<weaveDependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</weaveDependency>
</weaveDependencies>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
There is a problem with references to missing java classes. Quartz jar has some integration with JMS but my application doesn't use JMS so those quartz files are never loaded.
I've found a crutch with proceedOnError = true but I think due errors spring injection into aspect annotated class stops workings.
shade-maven-plugin doesn't fit here because it could be triggered by package phase only and aspectj launches on compile one.
[INFO] --- aspectj-maven-plugin:1.4:compile (default) # aspectj-demo ---
[ERROR] can't determine implemented interfaces of missing type javax.servlet.ServletContextListener
when processing declare parents org.quartz.ee.servlet.QuartzInitializerListener
when processing type mungers
when weaving
when batch building BuildConfig[null] #Files=5 AopXmls=#0
[Xlint:cantFindType]
[ERROR] can't determine implemented interfaces of missing type javax.servlet.http.HttpServlet
when processing declare parents org.quartz.ee.servlet.QuartzInitializerServlet
when processing type mungers
when weaving
when batch building BuildConfig[null] #Files=5 AopXmls=#0
[Xlint:cantFindType]

Any Maven coordinate listed in weaveDependencies must also be mentioned as a normal dependency. Based on the download page, you will need to have the following dependencies defined under <build>:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-oracle</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-weblogic</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jboss</artifactId>
<version>2.1.6</version>
</dependency>
Then under your configuration section for the aspectj-maven-plugin plugin, you can reference the JAR file that contains the aspects that you want to weave:
<weaveDependencies>
<weaveDependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</weaveDependency>
</weaveDependencies>
You may also need to use the 1.2 version of the aspectj-maven-plugin. Supposedly there is a bug in 1.4 about "declare parents" (but not sure if that's been fixed), and early versions of 1.3 suffered from the bug known as MASPECTJ-90.

Related

process-resource-bundles maven error calssnotfoundexception org.apache.commons.collections.ExtendedProperties

Trying to build an example from camel via mvn package
apache-camel-2.22.0/examples/camel-example-cdi-xml
Getting an error never seen before, i checked dependency tree for transitive deps and apache-collections is 3.2.1 and not corrupt so can't explain it, obviously i know what it means, just can't figure out why it's happening because everything that should be required is provided:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-remote-resources-plugin:1.5:process (process-resource-bundles) on project camel-example-cdi-xml: Execution process-resource-bundles of goal org.apache.maven.plugins:maven-remote-resources-plugin:1.5:process failed: A required class was missing while executing org.apache.maven.plugins:maven-remote-resources-plugin:1.5:process: org/apache/commons/collections/ExtendedProperties
Not really sure how to trouble shoot it.
I tried building with several different versions of maven. JDK = 1.8
update pom.xml add the following code
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.6.0</version>
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
</plugin>

HystrixCommandAspect Resulting into NoSuchMethodError in plain Java application

I am trying to use Hystrix in my Java Application, its a Non spring java application.
Used following Maven Dependencies in POM to enable Hystrix commands :
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>com.netflix.rxjava</groupId>
<artifactId>rxjava-core</artifactId>
<version>0.20.7</version>
</dependency>
Used following Dependencies to enable AspectJ :
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
Created a aop.xml in META-INF with following configuration :
<aspectj>
<aspects>
<aspect name="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect" />
</aspects>
<weaver options="-verbose">
<include within="*" />
</weaver>
</aspectj>
Used Hystrix Command in my Service Class :
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
#Component
#Service
public class TestHystrix
#HystrixCommand(commandKey = "testHystrix", threadPoolKey = "testHystrix", commandProperties = {
#HystrixProperty(name = "hystrix.command.testHystrix.execution.isolation.thread.timeoutInMilliseconds", value = "30") }, threadPoolProperties = {
#HystrixProperty(name = "hystrix.threadpool.testHystrix.maximumSize", value = "3") })
public void testHystrix() {
Added following JVM Argument :
-DWeavingMode=compile
But at both Junit testing and application Runtime, its resulting into following error :
java.lang.NoSuchMethodError: com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect.aspectOf()Lcom/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect;
Please help.
Before asking a question, you should first consult the manual of any tool you like to use. I am just quoting form there:
Aspect weaving
Javanica supports two weaving modes: compile and runtime. (...)
CTW. To use CTW mode you need to use specific jar version: hystrix-javanica-ctw-X.Y.Z. This jar is assembled with aspects compiled with using AJC compiler. If you will try to use regular hystrix-javanica-X.Y.Z with CTW then you get NoSuchMethodError aspectOf() at runtime from building with iajc. Also, you need to start your app with using java property: -DWeavingMode=compile. (...)
So maybe you want to switch your library.
BTW, if you use compile-time weaving (CTW), you should not need aop.xml because AspectJ only uses it for load-time weaving (LTW).
I was able to fix the issue by using following AspectJ Plugin configuration along with above maven dependencies :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<!-- <showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>-->
<Xlint>ignore</Xlint>
<encoding>UTF-8 </encoding>
<!-- Provide the Source information. -->
<!-- <aspectLibraries>
<aspectLibrary>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
</aspectLibrary>
</aspectLibraries> -->
<!--Weaving already compiled JAR artifacts -->
<weaveDependencies>
<weaveDependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
</weaveDependency>
</weaveDependencies>
</configuration>
<executions>
<execution>
<goals>
<!-- use this goal to weave all your main classes -->
<goal>compile</goal>
<!-- use this goal to weave all your test classes -->
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
This plugin will enable Post Compile weaving, for more details refer a very good article # http://www.baeldung.com/aspectj https://www.mojohaus.org/aspectj-maven-plugin/examples/weaveJars.html
With this plugin, aop.xml and -DWeavingMode=compile are also not required

Struggle with OSGi dependencies for Pellet and OWL-API

I wrote a facade for the owl-api paired with the pellet reasoner to easily access an ontology. It worked quiet well. Then I tried to pack it as an OSGi Bundle and use it via an TrackerService from other services. The owl-api bundle needs guava and trove to run. For trove there is no bundle. Even after reloading the target platform, the pellet jars were not recognized as bundle in eclipse (I took them from jpm4j). So I thought about just take all the jar- stuff and squeeze it into the bundle.
I read a lot of how-to's for packaging third-party jars with a bundle. I use the maven-bundle-plugin and after reading some documentation from this plugin and bndtools I decided to use conditional-package, because here it was described that all entries in the pom are passed to the bndtools. After the compilation, no jar was packaged with my bundle :(. Then I used embed-dependencies. It didnt't work because the transient dependiencies were not available at runtime. So I used the embed-transient tag (I already read that this is bad style). The Import-Package part was filled automatically with all transient dependencies, so I overwrote it by hand to ged rid of the errors regarding the unsolved imports. This worked and I was able to use the owl-api to insert new properties,individuals, etc (yeah).
When I tryed to start the pellet reasoner, I got the error message below at runtime. I think it is about the dependency-mess in my project again. I already know that the current way is the wrong one, but I was not able to find a better one. Please help me to get this dependency management in OSGi right.
Cheers,
Stephan
ERROR MESSAGE:
!ENTRY org.apache.felix.configadmin 4 0 2015-11-16 11:28:45.218
!MESSAGE [org.osgi.service.cm.ManagedService, id=28, bundle=7/initial#reference:file:..vdg.iCar.service]: Unexpected problem updating configuration vdg.iCar.service
!STACK 0
java.lang.Error: javax.xml.datatype.DatatypeConfigurationException: Provider for class javax.xml.datatype.DatatypeFactory cannot be found
at javax.xml.bind.DatatypeConverterImpl.<clinit>(DatatypeConverterImpl.java:892)
at javax.xml.bind.DatatypeConverter.initConverter(DatatypeConverter.java:140)
at javax.xml.bind.DatatypeConverter.parseFloat(DatatypeConverter.java:243)
at com.clarkparsia.pellet.datatypes.types.floating.XSDFloat.getValue(XSDFloat.java:81)
at com.clarkparsia.pellet.datatypes.types.floating.XSDFloat.getCanonicalRepresentation(XSDFloat.java:58)
at com.clarkparsia.pellet.datatypes.DatatypeReasonerImpl.getCanonicalRepresentation(DatatypeReasonerImpl.java:365)
at org.mindswap.pellet.ABox.createLiteral(ABox.java:1746)
at org.mindswap.pellet.ABox.addLiteral(ABox.java:1725)
at org.mindswap.pellet.KnowledgeBase.addPropertyValue(KnowledgeBase.java:974)
at com.clarkparsia.pellet.owlapiv3.PelletVisitor.visit(PelletVisitor.java:1103)
at uk.ac.manchester.cs.owl.owlapi.OWLDataPropertyAssertionAxiomImpl.accept(OWLDataPropertyAssertionAxiomImpl.java:119)
at com.clarkparsia.pellet.owlapiv3.PelletVisitor.visit(PelletVisitor.java:699)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyImpl.accept(OWLOntologyImpl.java:1516)
at com.clarkparsia.pellet.owlapiv3.PelletReasoner.refresh(PelletReasoner.java:967)
at com.clarkparsia.pellet.owlapiv3.PelletReasoner.<init>(PelletReasoner.java:345)
at com.clarkparsia.pellet.owlapiv3.PelletReasoner.<init>(PelletReasoner.java:304)
at com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory.createReasoner(PelletReasonerFactory.java:71)
at de.dlr.vdg.ontology.osgi.impl.util.ReasonerUtil.getInferredOntology(ReasonerUtil.java:56)
at de.dlr.vdg.ontology.osgi.impl.util.ReasonerUtil.getInferredRelations(ReasonerUtil.java:304)
at de.dlr.vdg.ontology.osgi.impl.OntologyServiceImpl.startReasoning(OntologyServiceImpl.java:106)
at de.dlr.vdg.iCar.osgi.impl.iCarServiceImpl.start(iCarServiceImpl.java:67)
at de.dlr.vdg.iCar.osgi.Activator.updated(Activator.java:65)
at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updated(ManagedServiceTracker.java:189)
at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updateService(ManagedServiceTracker.java:152)
at org.apache.felix.cm.impl.helper.ManagedServiceTracker.provideConfiguration(ManagedServiceTracker.java:85)
at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.provide(ConfigurationManager.java:1444)
at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.run(ConfigurationManager.java:1400)
at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:103)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.xml.datatype.DatatypeConfigurationException: Provider for class javax.xml.datatype.DatatypeFactory cannot be found
at javax.xml.datatype.FactoryFinder.findServiceProvider(FactoryFinder.java:304)
at javax.xml.datatype.FactoryFinder.find(FactoryFinder.java:268)
at javax.xml.datatype.DatatypeFactory.newInstance(DatatypeFactory.java:145)
at javax.xml.bind.DatatypeConverterImpl.<clinit>(DatatypeConverterImpl.java:890)
... 28 more
Caused by: java.util.ServiceConfigurationError: javax.xml.datatype.DatatypeFactory: Provider org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl not a subtype
at java.util.ServiceLoader.fail(ServiceLoader.java:239)
at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
at javax.xml.datatype.FactoryFinder$1.run(FactoryFinder.java:297)
at java.security.AccessController.doPrivileged(Native Method)
at javax.xml.datatype.FactoryFinder.findServiceProvider(FactoryFinder.java:292)
... 31 more
POM-File (important parts):
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.1</version>
<extensions>true</extensions>
<configuration>
<manifestLocation>META-INF</manifestLocation>
<instructions>
<Private-Package>${bundle.namespace}.*</Private-Package>
<Export-Package>${bundle.namespaceShared}</Export-Package>
<Bundle-SymbolicName>${bundle.symbolicName}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>${bundle.namespace}.Activator</Bundle-Activator>
<Bundle-RequiredExecutionEnvironment>JavaSE-1.8</Bundle-RequiredExecutionEnvironment>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Import-Package>org.osgi.framework;version="[1.6,2)",org.osgi.se
rvice.cm;version="[1.4,2)",org.slf4j;version="[1.7,2)"</Import-Package>
<Bundle-ClassPath>.,{maven-dependencies}</Bundle-ClassPath>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>prepare-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>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>4.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>owlapi-distribution</artifactId>
<version>3.5.2</version>
<type>bundle</type>
</dependency>
<dependency>
<groupId>com.github.ansell.pellet</groupId>
<artifactId>pellet-owlapiv3</artifactId>
<version>2.3.6-ansell</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.github.ansell.pellet</groupId>
<artifactId>pellet-query</artifactId>
<version>2.3.6-ansell</version>
<type>jar</type>
</dependency>
</dependencies>
After an additional day of research I came across this thread at oracle.
The pellet-api references the xerces and xml-api libs and due to the transient inclusion of dependencies, these libs were included in my bundle. But these libs are already present in the java runtime environment. Therefore I excluded them in the Embed-Dependency section and everything works.
<Embed-Dependency>*;scope=compile|runtime;inline=false;groupId=!xml-apis|xerces</Embed-Dependency>

Aspectj getting compile error when using the Java 8 class Supplier

I'm trying to use some nifty lazy logging tricks in my logging layer but AspectJ is choking on it. I have a facade in front of log4j. Here's the code:
public void debug ( Supplier<String> message )
{
if( isDebugEnabled() )
{
debug( message.get() );
}
}
The error:
[ERROR] The type java.util.function.Supplier cannot be resolved. It is indirectly referenced from required .class files
/home/Build/src/Core/Database/src/com/BasicDao.java:1006
LOGGER.debug( "Retry number: "+retryCount+"DB Lock Conflict, sleeping "+retrySleepTime );
Here's my pom bits:
<plugin>
<!-- This plugin integrates aspectj into our build cycle -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
And:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.5</version>
</dependency>
Other interesting facts are that this compiles fine within Eclipse, but I get this error when running mvn package from a Linux command line.
Upon further trial/error we have discovered that if we manually set JAVA_HOME to point to Java 8 then it compiles. It looks like AspectJ requires your JAVA_HOME to point to the right version of Java. In the main pom we are directing maven to use the specific version of Java with:
<executable>${JAVA_1_8_HOME}/bin/javac</executable>
<jvm>${JAVA_1_8_HOME}/jre/bin/java</jvm>
Neither of those seemed to work with the aspectj-maven-plugin configuration.

Using JDO Persistent classes from a separate module with datanucleus maven plugin

I have a working application that persists to a database using JDO - I want to use a PersistenceCapable class that's in a second java module. Although the app compiles a simple test gives the error:
The class "com.hello.world.Foo" is not persistable This means that it either hasnt been enhanced, or that the enhanced version of the file is not in the CLASSPATH (or is hidden by an unenhanced version), or the Meta-Data/annotations for the class are not found.
Ok, so the enhancer plugin is not running on the classes in the second module. I'm not sure what i need to do to point the enhancer at that module during the build.
ParentProject
Second Module: com.hello.world.Foo
ParentProject pom.xml with relevant parts - problem is how do i point the enhancer at the second module containing my persistent class?
<dependencies>
<dependency>
<groupId>Second Module</groupId>
<artifactId>Second Module</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.jdo</groupId>
<artifactId>jdo-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-core</artifactId>
<version>[3.2.0, 3.2.99)</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-api-jdo</artifactId>
<version>[3.2.0, 3.2.99)</version>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-rdbms</artifactId>
<version>[3.2.0, 3.2.99)</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-maven-plugin</artifactId>
<version>3.3.0-release</version>
<configuration>
<api>JDO</api>
<props>${basedir}/datanucleus.properties</props>
<log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
got it - i had to add the exact same pom.xml config in the question to the second module and do a mvn install to get those classes enahnced before compiling the parent module. Working on both Jetty and GAE. I thought, wrongly, that the parent module would enhance classes included in the module dependencies.
Thanks for your help DataNucleus!

Categories