Maven compiling with different JDK versions - java

My application is split between users on Java 1.6u45 and Java 1.8. Our problem is that we cannot specify the project system library and have two different compiler settings for the code at the same time.
Project Structure:
Project >
> src/main/java/com/us/javafx/... (Java 8 code)
> src/main/java/com/us/... (Java 6 code)
> src/main/resources/...
Our POM.xml:
<?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.us</groupId>
<artifactId>PROJECT</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>PROJECT</name>
<description>DESCR</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<!-- Copy all dependencies to jars directory -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/jars</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- Maven Compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<!-- Necessary to avoid setting JRE on Maven project update! -->
<!-- SEE PHOTO FOR WHY THIS IS HERE -->
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<!-- Compile Java 6 directory -->
<execution>
<id>java6</id>
<configuration>
<excludes>
<exclude>com/us/javafx/**/*</exclude>
</excludes>
<!-- Non working attempt to set compiler version -->
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
<!-- Compile Java 8 directory -->
<execution>
<id>java8</id>
<configuration>
<includes>
<include>com/us/javafx/**/*</include>
</includes>
<!-- Non working attempt to set compiler version -->
<source>1.8</source>
<target>1.8</target>
</configuration>
</execution>
</executions>
</plugin>
<!-- Create JAR in jars folder -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>${project.build.directory}/jars</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
If we tell the Maven compiler plugin to use Java 1.8, Java 1.6 users get the unsupported major minor version error. Or we tell the Maven compiler plugin to use Java 1.6 but our Java 8 code will complain about errors when performing Right Click Project > Maven > Update Project... (which causes the screen shot below):
Is there a way we can achieve both:
1) Compile Java 6 and 8 code with their respective compilers
2) Choose Java 8 as our project's JRE System Library

I recently learned about toolchains.xml. Maven has it even documented and supports it from 2.0.9! See toolchains documentation
So I added a toolchain.xml file to my ~/.m2/ folder with following content:
<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/opt/java8</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>1.7</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/opt/java7</jdkHome>
</configuration>
</toolchain>
</toolchains>
It allows you to define what different JDKs Maven can use to build the project irrespective of the JDK Maven runs with. Sort of like when you define JDK on project level in IDE. I used it when I needed to build another project with Java 7 while having Java 8 as a default.
I guess this can solve your issue.

Maven profiles is the simplest and most effective one.
You can elect and run them from your IDE too.
You design a profile for each different JDK you need. You put in each different JDK profile :
properties like source code target
dependencies that are specialised for that profile.
Project
+properties
+dependencies
+Profiles
Profile JDK8
+jdk8-properties
jdk.version
maven.compiler.source
maven.compiler.target
+jdk8-dependencies
Profile JDK11
+jdk11-properties
jdk.version
maven.compiler.source
maven.compiler.target
+jdk11-dependencies
Picture of maven profiles tree
And an example
<project>
<name>x</name>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.210</version>
</dependency>
</dependencies>
<profiles>
<!-- different Java versions -->
<profile>
<id>jdk8</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<jdk.version>1.8</jdk.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core-jakarta</artifactId>
<version>5.6.10.Final</version>
</dependency>
<!-- needed if jakarta is used -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.25</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>jdk11</id>
<properties>
<jdk.version>11</jdk.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.2.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.4.2</version>
</dependency>
</dependencies>
</profile>
</profiles>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
<junit-version>4.13.2</junit-version>
</properties>
</project>

Related

not getting jre system library in maven project in eclipse java

Not getting java support after creating maven project in eclipse
when maven project created I am not getting src/main/java,src/test/java and jre system libraries folders. i am following steps to create new project
steps to create
** maven project: File-->new-->project-->select maven project-->next-->select create a simple project-->next-->give project id and artifact id-->finish.**
<?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>MavenProject1</groupId>
<artifactId>MavenProject1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MavenProject1</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Yes, you wont get that. Its just a basic, very simple maven project that eclipse creates for you, so that you can structure the project as you like.
You now have 2 options:
create all the structuring by yourself
Create a new maven project and instead of selecting select create a simple project, select from archetype, and from there you can you can select what you like (Ex: maven-archetype-quickstart ) according to what you like.
For more info on setting up maven project with arch type search in youtube. You will get a lot of video tutorials.

Artifact of Spring Boot project generated by IntelliJ IDEA causes errors

I am working on a Spring Boot project using IntelliJ IDEA and everything works fine. I have no problem running and testing the application inside IntelliJ IDEA. It runs perfectly whether I configure it as an "Application" or "Spring Boot" (from the Run/Edit Configuration menu)
However, problems occur after I created an artifact and try to run it using "java -jar AppName.jar". It spews out errors like:
Could not find key 'spring.profiles.active' in any property source
and
Caused by: java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
I extracted the jar file and the spring.factories, etc. files are indeed inside the folder "META-INF".
I suspect the cause of the problem is the class path. When I run it from within IntelliJ IDEA, I noticed that it is set to module in "Use Classpath of Module". However, in the artifact, I could not get the settings right.
What do you think is the cause of the problem? I think it is the class path. If not, what else can it be? Please help!
I'm providing my pom.xml file here:
<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>org.kurento.tutorial</groupId>
<artifactId>kurento-tutorial</artifactId>
<version>6.6.1-SNAPSHOT</version>
</parent>
<artifactId>WebRTCLiveApp</artifactId>
<packaging>jar</packaging>
<name>WebRTCLiveApp</name>
<description>Company Live App</description>
<licenses>
<license>
<name>Apache 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
<distribution>repo</distribution>
</license>
</licenses>
<organization>
<name>Kurento</name>
<url>http://www.kurento.org</url>
</organization>
<developers>
<developer>
<id>kurento.org</id>
<name>-kurento.org Community</name>
<organization>Kurento.org</organization>
<organizationUrl>http://www.kurento.org</organizationUrl>
</developer>
</developers>
<properties>
<!-- Main class -->
<start-class>com.company.live.webrtc.WebRTCLiveApp</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
</dependency>
<!-- Kurento -->
<dependency>
<groupId>org.kurento</groupId>
<artifactId>kurento-client</artifactId>
</dependency>
<dependency>
<groupId>org.kurento</groupId>
<artifactId>kurento-utils-js</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>draggabilly</artifactId>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>demo-console</artifactId>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>adapter.js</artifactId>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>jquery</artifactId>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>ekko-lightbox</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
<layout>ZIP</layout>
<classifier>exec</classifier>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>banner.txt</include>
</includes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptor>src/assembly/bin.xml</descriptor>
<finalName>${project.artifactId}-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>no-assembly</id>
</profile>
</profiles>
I have search and tried other examples/solutions and none worked.
Please help! Thanks!
Problem is resolved.
In the IntelliJ Artifacts Create JAR from Modules dialog box, I have to
choose "Copy to output directory and link via manifest" option
make sure the META-INF/MANIFEST.INF file is set to "src/main/resources"
Everything is working now (after spending the entire afternoon).
If it is an maven project, it is recommended to add pom.xml
<packaging>war</packaging>

JOOQ : Generate sources with maven in Eclipse is not generating any file

I am new to JOOQ and Maven. I want to generate Pojo by giving schema, as per JOOQ's documentation says. I tried with commandline way, and it was working perfectly. I added same configuration in Eclipse java project. Below is my pom.mxl
<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>PojoGenerator</groupId>
<artifactId>PojoGenerator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rutherford.pojo</name>
<build>
<sourceDirectory>src</sourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<!-- <artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin> -->
<!-- Specify the maven code generator plugin -->
<!-- Use org.jooq for the Open Source edition org.jooq.pro for commercial
editions, org.jooq.pro-java-6 for commercial editions with Java 6 support,
org.jooq.trial for the free trial edition -->
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.8.4</version>
<!-- The plugin should hook into the generate goal -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<!-- Manage the plugin's dependency. In this example, we'll use a PostgreSQL
database -->
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
</dependency>
</dependencies>
<!-- Specify the plugin configuration. The configuration format is the
same as for the standalone code generator -->
<configuration>
<!-- JDBC connection parameters -->
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432/test</url>
<user>postgres</user>
<password>test</password>
</jdbc>
<!-- Generator parameters -->
<generator>
<database>
<name>org.jooq.util.postgres.PostgresDatabase</name>
<includes>.*</includes>
<excludes></excludes>
<inputSchema>public</inputSchema>
</database>
<target>
<packageName>com.generated.pojo</packageName>
<directory>${project.build.directory}/src</directory>
</target>
</generator>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
I tried generate resources. It says BUILD SUCCESS, but I can't see my generated Pojos anywhere. Please let me know what am I missing.
You still need to add the plugin to your build, as <pluginManagement> only helps you declare common configurations for reuse.
<build>
<pluginManagement>...</pluginManagement>
<plugins>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<execution>...</execution>
</plugin>
</plugins>
</build>
See also this question here: Maven: What is pluginManagement?
In this case, it's probably simpler to just ignore this well-known issue in Eclipse and avoid the <pluginManagement> element.

Maven + Eclipse cannot build path

I have found this solution on stackoverflow. Warning - Build path specifies execution environment J2SE-1.4
I am getting almost the same problem but instead of 1-4 i am having 1-6. Unfortunately I don't really understand this configuration thing. It's new thing to me. I am trying second answer to get this working. I found this pom.xml in src under my project and it's xml looks like 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>eu.jpereira.trainings.designpatterns.creational.singleton</groupId>
<artifactId>singleton</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>singleton</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>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Configure Build Process -->
<build>
<plugins>
<!-- Compiler plugin to use Java 1.6 compatiblity -->
<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>
</configuration>
</plugin>
<!-- Eclipse plugin to force download of source and JavaDoc jars -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<wtpversion>2.0</wtpversion>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>
</project>
What and where should I add sth? I am using Eclipse on Windows 7. My version of java is: 1.8.0.25-b18 Words like plugin and Maven and JUnit are highlighted red. I would be grateful for help!
Try
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
And then regenerate the Eclipse project with mvn eclipse:eclipse.

Not generating OSGI-INF folder

I am new to OSGI.I have been having trouble to get OSGI-INF folder in generated jar file.
I need to have folder structure like as below
META-INF
OSGI-INF
Com.mine.cq
I am using Eclipse and m2e plugin. When I run my project I am getting BUILD SUCCESS. And I am getting the below folder structure in that generated jar file.
META-INF
Com.mine.cq
Here is my POM.xml
<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.mine.cq</groupId>
<artifactId>mineCore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mineCore</name>
<url>http://maven.apache.org</url>
<properties>
<file.encoding>utf-8</file.encoding>
</properties>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0-alpha-3</version>
<executions>
<execution>
<id>enforce-java</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<message>Project must be built with Maven 2.0.7 or higher</message>
<version>2.0.7</version>
</requireMavenVersion>
<requireJavaVersion>
<message>Project must be compiled with Java 5 or higher</message>
<version>1.5.0</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.3</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>
com.mine.cq.mineCore.*
</Export-Package>
<Import-Package>
*;resolution:=optional,
javax.servlet;version=2.4,
javax.servlet.http;version=2.4
</Import-Package>
<Embed-Dependency>
</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Include-Resource>{maven-resources}</Include-Resource>
<Sling-Bundle-Resources>/var/classes</Sling-Bundle-Resources>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.0</version>
<configuration>
<goals>install</goals>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Why OSGI-INF folder is not in the .jar file? I need to set some information in OSGO-INF folder since I have to register my component as a OSGI service.
Please guide me to get it done.
Although being pretty late, I'll post my 2 cents about this issue for future reference.
As already pointed out, you can have the "packaging" of the bundle set to "jar" if you follow the instructions given in Maven bundle plugin documentation.
There is just a little gotcha: with that configuration, you need to explicitly add <exportScr>true</exportScr> inside the plugin configuration in order to properly create the SCR xml file (also remember to adjust manifest location, since in the documentation that piece is absent!).
You can see an example here (that's totally different from yours, but I assume you can easily reduce it on your code, if you're still interested):
<?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.massimobono.karaf.examples</groupId>
<artifactId>user-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<!-- Here you specifiy that you want to use the manifest file generated by maven bundle plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<!-- Here you generate the whole MANIFEST by using maven-bundle-plugin -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.2.0</version>
<extensions>true</extensions> <!-- make sure this is present -->
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<manifestLocation>${project.build.outputDirectory}/META-INF/</manifestLocation> <!-- make sure this is present! in the example of maven bundle plugin documentation, this piece is NOT present -->
<exportScr>true</exportScr> <!-- be sure to add this line as well -->
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<_dsannotations>*</_dsannotations>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.osgi/org.osgi.service.component.annotations -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>
Your pom.xml needs to have a packaging type of "bundle" rather than "jar". If you want the packaging type to be "jar", use this:
http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html#ApacheFelixMavenBundlePlugin%28BND%29-AddingOSGimetadatatoexistingprojectswithoutchangingthepackagingtype
EDIT: Oh! That's only problem one. The other problem is that I don't think you can generate
OSGI-INF with the maven-bundle-plugin. You need to create the OSGI-INF folder yourself within src/main/resources or use a plugin that generates OSGI-INF.
The maven-scr-plugin can generate OSGI-INF, but it's only useful if you are using SCR. Maven SCR Plugin - Not generating OSGI-INF folder

Categories