Inject middleware in Spring Webflux - java

I am trying to handle all cross cutting concerns e.g exception, authorization etc. in a project/repo and inject in more than one separate and independent spring-webflux project i.e handling cross-cutting concerns by building a reusable microservice platform.
Can anyone suggest how to accomplish this?
For example:- light4j handle all cross cutting concerns as middleware just need to add as plugin. But it's not compatible with SpringWebflux.
Even using AspectJ we can't use same handlers for different projects until or unless they are under same parent project.
I tried to use load-time weaving feature of AspectJ. I defined aspects in different project and add a plugin of that in current project (in which I want to use) but when exception occurred aspectJ part isn't invoked
Below is my pom.xml for current project
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dummy</groupId>
<artifactId>dummydmanagement</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DummyManagement</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>com.jaway.blog.aspectj</groupId>
<artifactId>annotations-element-value-pair-without-main-class</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>/home/mmt8281/codebase/annotations-element-value-pair-without-main-class/target/annotations-element-value-pair-without-main-class-1.0-SNAPSHOT.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Below is pom.xml of aspect project
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jaway.blog.aspectj</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>annotations-element-value-pair-without-main-class</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<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>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Attaching gitHub links:-
AspectJ Code
DummyMgmt Code

The problem with "copy & paste programming" is that you use something you do not understand. Maybe it would be better to learn the basics first.
Having said that, there are two ways to solve your problem, given the fact that the aspect module you copied from the blog post is native AspectJ:
Configure all applications which want to use the aspect to also use native AspectJ, either by post-compile binary weaving or by load-time weaving. The Spring manual describes how to use LTW.
Trivially convert the aspect module to a Spring component and make all client projects use Spring AOP.
The latter is what I did, because I suppose that you only want to use the aspect in Spring projects and that you only annotate public methods in Spring components.
The native AspectJ solution would be more powerful, because it would also work with static and protected/private methods, even for non-Spring code and even outside of Spring completely. But why use a cannon if a pistol is sufficient?
I sent you two pull requests:
https://github.com/shas2hwat/AspectjMgmt/pull/1
https://github.com/shas2hwat/DummyManagement/pull/1
What you need to change in the aspect module, is basically this:
Remove AspectJ Maven plugin
Add org.springframework:spring-context in order to be able to add #Component to the aspect class.
I also optimised a few more things, such as update the AspectJ version.
Then, in the application module you need to:
Add org.aspectj:aspectjweaver
Add the aspect module's base package name to the component scan
I also removed the ugly system dependency with the fixed path to the aspect module. If you simply run mvn clean install on the aspect module before you build the application, the library will be found in the local Maven repository. (I think, you need to urgently learn some Maven basics.)
Now when running this application
#SpringBootApplication
#ComponentScan(basePackages = {"com.dummy.dummydmanagement", "com.jayway.blog"})
public class DummyManagementApplication {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(DummyManagementApplication.class, args);
context.getBean(ItemService.class).getAllItemsService();
}
// (...)
}
the console log will be:
YourAspect's aroundAdvice's body is now executed Before yourMethodAround is called.
Exception occured
YourAspect's aroundAdvice's body is now executed After yourMethodAround is called.
Of course, annotating the static main method does not work with Spring AOP, which is why I removed the annotation. That would only work with native AspectJ. Please let me know if you need that, but I think you should keep it simple, because you clearly do not understand the tools you are using.
Update: Because for whatever reason the OP is angry with me now, because I told him to stop texting me directly on Telegram, he also deleted his own repositories. For anyone interested, here are my clones, containing both the original code and my modifications fixing the problem, as described above:
https://github.com/kriegaex/AspectjMgmt, relevant commit here
https://github.com/kriegaex/DummyManagement, relevant commit here

Related

Making an executable jar out of spring boot MVC project serving content using JSP and a custom parent set in POM

I had a Spring MVC project that I'm converting to spring boot. This project of mine cannot be given spring-boot-starter-parent as parent because I need to keep a custom parent.
I solved this first issue by injecting spring-boot-dependencies in dependencyManagement.
I also need my project to embed a tomcat, so I've used spring-boot-starter-web. I need to use JSP so i've added the dependencies to jstl and jasper.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-loader -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- ... -->
</dependencies>
Compiling the jar and launching the server using
mvn spring-boot:run
correctly works. Now I'd like to make it work as executable jar, too.
I have read some documentation, posts, web pages, stack overflow questions, but I still cannot make it properly work.
In this question, reading the edited version of the accepted answer and the github project it looks very easy to make an executable jar out of a project having as parent spring-boot-starter-parent. Is it possible to make it work with a custom parent too?
I have already tried to follow these guidelines but it doesn't work with my project.
I tried adding the plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
both with and without the execution node
and the plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifestFile>src/main/webapp/META-INF/MANIFEST.MF</manifestFile>
</archive>
<!-- <archive>-->
<!-- <manifest>-->
<!-- <mainClass>my-boot-application</mainClass>-->
<!-- <springBootClasses>WEB-INF/classes/</springBootClasses>-->
<!-- <springBootLib>WEB-INF/lib/</springBootLib>-->
<!-- </manifest>-->
<!-- </archive>-->
</configuration>
</plugin>
both with and without the manifest customized information.
Eventually I have incurred in this stackoverflow post listing all that is necessary to make a spring boot application using jsp, embedded tomcat and custom parent project work.
So, my pom.xml is now much readable and compact and looks much like the examples in the link.

how to compile spring-boot-maven-plugin without dependencies?

I am trying to do an excercise with micro-services in Java with Spring boot, for this I am developing two web services in different projects with the intention of deploying them in tomcat like two independent files (.war).
I have read about set up tomcat to have the dependencies in an specified folder to share it with other services and this way not to increase the same libraries in all services.
The ploblem is that when I compiled the service with maven through the artifact spring-boot-maven-plugin the .war files always has the dependencies inside. Because of I want to know if someone know how to configure maven to
remove dependencies from .war file..... in Spring Boot.
The .war follows with the dependencies inside, Edited:
I have added the provided like said Michael Potter and the execution. it works fine. My pom.xml is the follow:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
For Maven not to include dependency in your WAR file you need to specify its scope to provided. The description of the scope from official Maven documentation:
This is much like compile, but indicates you expect the JDK or a
container to provide the dependency at runtime. For example, when
building a web application for the Java Enterprise Edition, you would
set the dependency on the Servlet API and related Java EE APIs to
scope provided because the web container provides those classes. This
scope is only available on the compilation and test classpath, and is
not transitive.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
<scope>provided</scope>
</dependency>
The dependency will be downloaded to compile the sources, but not packed in the WAR.
Concerning spring-boot-maven-plugin. By default it makes repackaging of a WAR that allows you to launch it from console. Thus, it packages all required dependencies to the archive - even with the provided scope. You can see in your target directory two files: {project-name}.war which is repackaged and {project-name}.war.original - the one that should not contain provided dependencies. To disable repackaging you should change spring-boot-maven-plugin configuration to the following:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
Then you need to place the required dependency to tomcat/lib folder and restart the Tomcat.

Can't get biojava to work in a Maven Netbeans application

I have a problem getting BioJava to work in a Netbeans RCP application, built using Maven. I've created a Maven module as a wrapper, including org.biojava.* and org.forester.* packages as public in the POM. Then, from another module I set the wrapper as a dependency, and use some of the basic examples from the BioJava cookbook for testing.
Whenever I try to instantiate some object of a class from BioJava, the application freezes and I have to kill it using the Windows task manager.
Here's the wrapper's pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nl.hecklab.bioinformatics</groupId>
<artifactId>Spider-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>BiojavaWrapper</artifactId>
<version>4.1.0</version>
<packaging>nbm</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>nbm-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<useOSGiDependencies>true</useOSGiDependencies>
<publicPackages>
<publicPackage>org.biojava.*</publicPackage>
<publicPackage>org.forester.*</publicPackage>
</publicPackages>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<useDefaultManifestFile>true</useDefaultManifestFile>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-alignment</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Here's some code I try to get to work. This is just a very coarse example, called from a button in a TopComponent. Input and output are just text fields.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Reader r = new Reader(new File("D:\\current\\fastafile.fasta"));
for (ProteinSequence a : r.getSequences()) {
input.append(a.toString());
}
Profile<ProteinSequence, AminoAcidCompound> profile = Alignments.getMultipleSequenceAlignment(r.sequences);
output.setText(String.format("Clustalw:%n%s%n", profile));
ConcurrencyTools.shutdown();
}
Here's the reader class:
public class Reader {
List<ProteinSequence> sequences = new ArrayList<>();
public Reader(File fastaFile) {
try {
FileInputStream inStream = new FileInputStream(fastaFile);
FastaReader<ProteinSequence, AminoAcidCompound> fastaReader
= new FastaReader<>(
inStream,
new GenericFastaHeaderParser<ProteinSequence, AminoAcidCompound>(),
new ProteinSequenceCreator(AminoAcidCompoundSet.getAminoAcidCompoundSet()));
LinkedHashMap<String, ProteinSequence> b = fastaReader.process();
sequences.addAll(b.values());
} catch (IOException ex) {
Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
}
}
public List<ProteinSequence> getSequences() {
return sequences;
}
}
In the (Netbeans) IDE, the classes are found and used in autocompletion, and the project builds successfully, in each case indicating that principally the dependencies are set up correctly.
First of all check the wrapper module's manifest to see if all entries are correctly generated, especially since you define useOSGiDependencies==true. It could be that the biojava jars contain osgi headers and then you are not wrapping the jars in module, but declare a dependency on osgi plugin.
However locking of the app is weird, if there was something wrong with the runtime dependencies I would have expected an early 'unsatisfied dependencies' error. You might want to create a thread dump and check what's going on. Maybe you have a deadlock. Or since your action (jButton1ActionPerformed) is called from AWT, maybe the whole reading thing just takes time and your UI thread is locked.
I've done a lot of searching and found that the actual culprit is slf4j, that's used throughout BioJava.
I don't know why it freezes the platform application, but I'm able to cause my module to not install by creating a slf4j logger in it.
I've seen a solution online for a wrapper module, and it turns out it's enough to create a wrapper for org.slf4j:slf4j-api:x.y.z together with org.slf4j:slf4j-jdk14:x.y.z. Add org.slf4j.* to the public packages. Here's the POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>group</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>slf4jwrapper</artifactId>
<packaging>nbm</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>nbm-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<publicPackages>
<publicPackage>org.slf4j.*</publicPackage>
</publicPackages>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<useDefaultManifestFile>true</useDefaultManifestFile>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-api-annotations-common</artifactId>
<version>${netbeans.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
The wrapper should then be used in the BioJava dependent modules, but it should work for other modules depending on slf4j as well.

OSGI org.slf4j.impl dependency

I'm new to OSGI (sorry) and having a few issues trying to deploy my package and related dependencies.
This is my POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.felix.test</groupId>
<artifactId>com.felix.test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.4</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>
com.felix.test.search
</Export-Package>
<Bundle-SymbolicName>
${project.artifactId}
</Bundle-SymbolicName>
<Bundle-Activator>
com.felix.test.Activator
</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Then I'm bundling this using the Maven command:
mvn org.apache.felix:maven-bundle-plugin:bundleall
This is successful and generates my bundle as well as 3 dependency bundles:
net.sf.ehcache_2.10.0.jar
org.apache.commons.lang3_3.4.0.jar
slf4j.api_1.7.7.jar
This seems OK and I can install and start the first two but when I try and start slf4j I get the following exception:
org.osgi.framework.BundleException: Unable to resolve slf4j.api [25](R
25.0): missing requirement [slf4j.api [25](R 25.0)] osgi.wiring.package;
(&(osgi.wiring.package=org.slf4j.impl)(version>=1.6.0)) Unresolved
requirements: [[slf4j.api [25](R 25.0)] osgi.wiring.package;
(&(osgi.wiring.package=org.slf4j.impl)(version>=1.6.0))]
I'm pretty sure I'm missing something very simple but can't pin it down. Any help would be much appreciated!
Slf4j has an unusual design (some might say a bad design, ahem). It is an API bundle that depends on an implementation package, namely org.slf4j.impl.
You need to install an additional bundle that implements the Slf4j API. There are lots of choices here... for example slf4j-simple is a basic implementation, whereas slf4j-jdk14 uses the Java 1.4 java.util.logging back end, etc.
Logback also contains an implementation of the API.
Need to correct myself as slf4j indeed provides bundles now as Neil pointed out. Not sure how well it works though. I found some explanation how to install slf4j for OSGi here. It does not look very clean to me though. You need to create a bundle fragment for the configuration. Which means you can not change it at runtime.
So I still would rather recommend to use pax-logging at runtime instead. It implements the slf4j api as well as other logging APIs. As backend it uses log4j and configures it via config admin. So you do not need hacks for the logging config and can change it at runtime.
I had this same error message, and found out that it was due to bad bundle plugin configuration (as mentioned by #Christian_Schneider).
My situation
So i had roughly this error message when deploying on Karaf:
missing requirement ... Unresolved requirements: ... osgi.wiring.package=org.slf4j.impl
My modules pom.xml looked like this:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j_version}</version>
<scope>provided</scope>
</dependency>
...
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Private-Package>*</Private-Package>
</instructions>
</configuration>
</plugin>
Solution
credits #jbonofre from the karaf community
I just repaced <Private-Package>*</Private-Package> with <Export-Package></Export-Package>, to get:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Export-Package></Export-Package>
</instructions>
</configuration>
</plugin>
and consequently the import of org.slf4j.impl in my bundles manifest.mf disappeared, leaving only an import to org.slf4j. This is provided by PAX Logging, which is installed by default on karaf.

Maven 3 - How to add annotation processor dependency?

I need to run an annotation processor on my project's sources. The annotation processor should not become a transitive dependency of the project since it's only needed for annotation processing and nothing else.
Here is the complete (non-working) test pom I use for this:
<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>test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test annotations</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate-jpamodelgen.version>1.2.0.Final</hibernate-jpamodelgen.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<annotationProcessors>
<annotationProcessor>
org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
</annotationProcessors>
<debug>true</debug>
<optimize>true</optimize>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<AaddGeneratedAnnotation>true</AaddGeneratedAnnotation>
<Adebug>true</Adebug>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate-jpamodelgen.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
I explicitly defined org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor as an annotation processor in the plugin configuration for tests and I know it shouldn't be required.
The problem I'm encountering is that the hibernate-jpamodelgen dependency is not added to the compiler classpath so the annotation processor is not found and the build fails.
As per this answer, I tried adding the dependency as a build extension (not sure I understand what those are supposed to be!) like so:
<extensions>
<extension>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate-jpamodelgen.version}</version>
</extension>
</extensions>
This also doesn't add hibernate-jpamodelgen to the compiler classpath.
The only thing I found which works so far is adding the dependency to the project in the <dependencies> section. This has the unfortunate side-effect of adding hibernate-jpamodelgen as a transitive dependency afterwards which I want to avoid.
My previous working setup uses the maven-processor-plugin plugin to achieve what I want. However, this plugin is not supported by eclipse m2e and the latest version of the maven-compiler-plugin now handles multiple compiler arguments properly so I'd rather use the latter.
The annotationProcessorPaths option can be used in recent versions of the Maven compiler plug-in:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.2.6.Final</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
That way the processor is separated from the actual project dependencies. This option is also picked up by the Eclipse M2E plug-in if annotation processing is enabled for the project.
Add the dependency as an optional dependency (<optional>true</optional>). This will add the dependency under compilation, but will prevent it for being a transitive dependency:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate-jpamodelgen.version}</version>
<optional>true</optional>
</dependency>
If you're creating an artifact in this module with all your dependencies in it (like a .war), you may use the <scope>provided</scope> instead. This both prevents the dependency to be transitive and to be included in the artifact the module produces.
For JDK 10 I really had to go a bit crazy to get it to work, Hoping someone finds this useful
<jaxb.version>2.3.0</jaxb.version>
<maven.hibernate.version>5.3.2.Final</maven.hibernate.version>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/annotations</outputDirectory>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${maven.hibernate.version}</version>
</annotationProcessorPath>
<annotationProcessorPath>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
</annotationProcessors>
<compilerArgs>
<arg>-AaddGeneratedAnnotation=false</arg>
</compilerArgs>
<compilerArguments>
<AaddGeneratedAnnotation>false</AaddGeneratedAnnotation>
<Adebug>true</Adebug>
</compilerArguments>
<failOnError>true</failOnError>
</configuration>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${maven.hibernate.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.version}</version>
<type>jar</type>
</dependency>
</dependencies>
</plugin>
The problem is really in 3.* version of the maven-compiler-plugin. It acts a bit different from the 2.* version. In particular, it seems that maven-compiler-plugin3.* doesn't add its dependencies and build extensions to the classpath because it uses javax.tools instruments for running compile process. To get back the old behavior for maven-compiler-plugin you should use a new configuration property forceJavacCompilerUse:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
....
</plugin>
Please take a look jpa-metamodels-with-maven
For further visitors, I found that there are some significant changes in maven-compiler-plugin 3.x series.
This is how I do this. (I'm the one who you linked)
The point is that my solution does not work with those 3.x series of maven-compiler-plugin.
<project ...>
<build>
<extensions>
<extension>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>1.3.0.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version> <!-- See this? -->
</plugin>
</plugins>
</build>
</project>
Not sure what kind of build error you got, but here is my case:
I got the following compile error in Idea:
Annotation processor 'org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor' not found error
But, when compiled from Maven, it was all fine.
So, the problem of mine was that somehow I got wrong configuration in Idea settings. Particularly, it appeared that Idea somehow detected the processor and put in into the settings of module processor profiles. It is discussed here.
I fixed it as the following:
Go to Idea > Settings > Annotation Processors.
For each processor profile make sure that:
Enable annotation processing is Yes;
There is no annotation processor FQ name of one you have error about (e.i. "JPAMetaModelEntityProcessor") in the list on the right side. If it is listed there, just select and click '-' minus button to remove it.
I think this is a better way to contain such dependencies in profiles to solve such problems.
<profile>
<id>XXX-profile</id>
<dependencies>
<dependency>
// XXX artifact path
</dependency>
</dependencies>
</profile>

Categories

Resources