OSGi and Virgo tools : impossible to add a maven project (using bnd maven plugin) - osgi

I'm new to OSGi, coded only a few bundles and deployed them manually.
Some friends of mine told me about Virgo and Virgo tools, which allows you to auto-deploy bundles you manage with eclipse.
I'm currently trying to set all this up. I have virgo-tomcat-server-3.5.0.RELEASE, along with virgo tools 1.0.0, all of this installed on a Spring Tool Suite 3.1.0.RELEASE (in case you don't know, this last one includes the m2eclipse plugin).
My bundle is a maven project. It uses the bnd plugin and here's its configuration
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>fr.tpepio.mtg.model</Export-Package>
</instructions>
</configuration>
<executions>
<execution>
<id>build-manifest</id>
<phase>compile</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
You can see that I export only one package. I also try to make m2eclipse to dynamically generate my manifest.mf file when eclipse compiles my classes.
I finally get to the issues I'm facing.
Since I import my bundle as a maven project into STS, I have to add the Virgo facet to it. And as soon as I update my maven configuration, it kind of screws my projects and I get the following error :
Java compiler level does not match the version of the installed Java project facet.
Appart from my (shitty) maven configuration, I have found myself unable to add my project into the virgo server, which endlessly tells me
null reason : null
Does someone has any clue ?

Hurray ! Problems solved.
First : about the java compiler and facet's java version. Telling maven that your sources are coded in 1.6, and must be compiled in 1.6 will resolve the problem. Code is the following :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<inherited>true</inherited>
<configuration>
<verbose>false</verbose>
<encoding>${java.compiler.encoding}</encoding>
<fork>true</fork>
<source>${java.compile.version}</source>
<target>${java.compile.version}</target>
</configuration>
I still don't understand why it resolves this problem, since both my project configuration and STS configuration could only use java 6... I believe this is because my sources where compiled as 1.5 java sources.
Second : adding the maven bundle project to virgo.
Configure correctly bnd, by running its "manifest" goal in the right maven phase (e.g. : after your classes have been compiled), and tell him where he can find the manifest. This solves the "null reason : null" issue (maybe virgo should have said : "could not find manifest.mf... ?).
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.name}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-ClassPath>.</Bundle-ClassPath>
<Export-Package>
[packages you want to export]
</Export-Package>
</instructions>
</configuration>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
<configuration>
<manifestLocation>src/main/resources/META-INF/</manifestLocation>
</configuration>
</execution>
</executions>
Add this goal to the m2eclipse lifecycle mapping. This refreshes your manifest after each occurence of the phase you indicated in your bnd configuration (here : the process-classes phases). Otherwise, m2eclipse has no way to understand when it should call your goal.
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<!-- Maven Bundle Plugin -->
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<versionRange>[2.3.7,)</versionRange>
<goals>
<goal>manifest</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>false</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
Hope this wil help.

Related

JavaFX Application with Maven in Eclipse

I want to ask if there is any method to add JavaFX into Maven Archetype list in Eclipse or any plugin to use Maven to build JavaFX Application.
There is the javafx-maven-plugin which is available for maven.
When developing with Java 8 you just put that plugin as some build-plugin, without further dependencies.
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<mainClass>your.main.class.which.extends.javafx.Application</mainClass>
</configuration>
</plugin>
Calling mvn jfx:jar creates your javafx-application-jar inside target/jfx/app/yourapp-jfx.jar, or even creates native launcher (like EXE-file) when calling mvn jfx:native.
Disclaimer: I'm the maintainer of the javafx-maven-plugin.
The only thing I add to my pom.xml in order to build JavaFX Application is this dependency :
<dependency>
<groupId>com.oracle</groupId>
<artifactId>javafx</artifactId>
<version>2.2</version>
<systemPath>${java.home}/lib/ext/jfxrt.jar</systemPath>
<scope>system</scope>
</dependency>
It is simply fetching the javafx jar in my Java8 JRE to add it to the project.
Then I use the maven-assembly-plugin to build the jar with dependencies.
Hope it helps.
This answer is copied from the documentation at https://openjfx.io/openjfx-docs/#maven. More detailed information (including a full sample pom.xml reference) is available at the link provided.
The pom uses the JavaFX Maven plugin:
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>HelloFX</mainClass>
</configuration>
</plugin>
</plugins>
Add the maven dependencies:
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
</dependencies>
Run the application (e.g. based on the HelloFX.java from the referred sample):
mvn clean javafx:run
Note regarding other outdated answers
Previous (highly-voted) answers which reference the com.zenjava javafx-maven-plugin are outdated, as that plugin is not coded to work with recent JavaFX versions. For Java versions 10+, the org.openjfx javafx-maven-plugin should be used.
Also, for Java 10+, answers which reference only the assembly plugin and state that JavaFX is included in the JDK, are also outdated. JavaFX is no longer bundled in recent JDK releases, instead it is available as a separate SDK from openjfx.io or as a set of library dependencies from the maven central repository.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
just do as a common Java application because JavaFX version jumped to 8.0. Supports for JavaFX are built-in.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>run application</id>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>cn.keepfight.intro.FXParamApp</mainClass>
<arguments>
<!--<argument>-Dsun.java2d.opengl=true</argument>-->
</arguments>
</configuration>
</execution>
</executions>
</plugin>

Using maven-bundle-plugin with the maven-shade-plugin

I'm using the maven-shade-plugin to relocate some packages during the package phase of my build. I'm also using the maven-bundle-plugin to generate a manifest. The problem is the bundle plugin runs before the shade plugin (during the process-classes phase), and doesn't include any of my shaded packages in the generated manifest's exports.
How can I get these two plugins to play nice with each other, so that my relocated packages are treated like any other package by the bundle plugin?
--
By request, the Shade and bundle sections of my POM:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<filters>
<filter>
<artifact>cglib:cglib</artifact>
<includes>
<include>net/sf/cglib/core/**</include>
<include>net/sf/cglib/proxy/**</include>
</includes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>net.sf.cglib</pattern>
<shadedPattern>org.modelmapper.internal.cglib</shadedPattern>
</relocation>
<relocation>
<pattern>org.objectweb.asm</pattern>
<shadedPattern>org.modelmapper.internal.asm</shadedPattern>
</relocation>
</relocations>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Export-Package>
org.modelmapper,
org.modelmapper.builder,
org.modelmapper.config,
org.modelmapper.convention,
org.modelmapper.spi
</Export-Package>
<Private-Package>
org.modelmapper.internal.**
</Private-Package>
<Import-Package>
*
</Import-Package>
<Include-Resource>
{maven-resources},
{maven-dependencies}
</Include-Resource>
</instructions>
</configuration>
</plugin>
Taken from here
Another option is to dump maven bundle plugin altogether and use Maven Shade Plugin ManifestResourceTransformer to add the desired OSGI metadata to the manifest.
Take a look at xbean-asm-shaded/pom.xml for a example.
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Export-Package>
org.apache.xbean.asm;org.modelmapper.builder; ...
</Export-Package>
<Import-Package>*</Import-Package>
<Private-Package>org.modelmapper.internal ...</Private-Package>
</manifestEntries>
</transformer>
Solution is very simple. You still can use maven-bundle-plugin and maven-shade-plugin at the same time. You just need to remember about the order. If you use bundle packaging maven bundle plugin will get executed during package phase before maven-shade. But that's not so wrong.
Here is the deal.
Use Private-Package: pkg.name.before.shading
Make maven-shade-plugin with one <include>null:null</include> - this will prevent shade plugin from creating empty jar
use maven-shade-plugin relocations - from pkg.name.before.shading to other.pkg.
You may see this trick working in FasterXML jackson-module-paranamer
I assume that after the compile phase is done you want to:
relocate some classes with the shade plugin
create a manifest with the bundle plugin
pack it all up with the jar plugin
The problem is the bundle plugin runs before the shade plugin
The bundle plugin is binded to process-classes phase which comes before the package phase to which the shade plugin is bound.
I suggest that you bind the shade plugin to process-classes phase also. Change shade plugin configuration like this:
<phase>process-classes</phase>
Since the shade plugin definition comes before the bundle plugin definition in the pom file, the shade plugin will run before the bundle plugin during the process-classes phase.
There is a neat transformer implementing just this functionality from Hazelcast - HazelcastManifestTransformer (ver 3.9). What it does is thoughtfully merging the Import-Package and Export-Package attributes, enabling the user to exclude extend/reduce the default merged result.
How to use it in your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-build-utils</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="com.hazelcast.buildutils.HazelcastManifestTransformer">
<mainClass>...</mainClass>
<!-- the tag below is required due to a missing null-check it seems -->
<overrideInstructions></overrideInstructions>
</configuration>
</executions>
</plugin>
The override instructions (Export/Import-Package) are comma delimited package names, preceded with an exclamation mark whenever we want to exclude those specific ones from the list.
Hope this helps! I do realize that's an old question, but seems the Hazelcast's transformer isn't gaining much publicity.

Jacoco Maven Plugin - Plugin execution not covered by lifecycle configuration

I'm new to Maven and want to use the Jacoco Maven Plugin to build my projects.
I've set up an example project with TestNG the only dependency.
Here is part of the pom.xml:
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.2.201302030002</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And i get this error:
Plugin execution not covered by lifecycle configuration: org.jacoco:jacoco-maven-
plugin:0.6.2.201302030002:prepare-agent (execution: default, phase: initialize)
What am I doing wrong ?
Cheers
You can ignore the plugin goal, adding something like this to your pom.xml
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only.
It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<versionRange>[0.5,)
</versionRange>
<goals>
<goal>prepare-agent</goal>
</goals>
</pluginExecutionFilter>
<action>
<!-- m2e doesn't know what to do with jacoco,
let's ignore it or annoying error markers appear
see http://wiki.eclipse.org/M2E_plugin_execution_not_covered
-->
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
As this is related to the Eclipse Maven plugin, alternatively this can be set locally in Eclipse's preferences. Moving the configuration out of the project's pom file helps the code simple and clean, free of IDE particulars.
Go to Eclipse --> Preferences --> Maven --> Lifecycle Mappings. Add lifecycle-mapping-metadata.xml as the following:
<?xml version="1.0" encoding="UTF-8"?>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<versionRange>[0.5,)</versionRange>
<goals>
<goal>prepare-agent</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
Reload the life-cycle mappings file and then Maven --> Update Project
Eclipse now offers a quick fix to disable the warning and save those in user preferences (in Eclipse --> Preferences --> Maven --> Lifecycle Mappings lifecycle-mapping-metadata.xml as noted by #iker-aguayo ) so you don't have to manually create or edit the file. This is useful in cases where you can't update the pom (such as using an open source project where you cannot commit.)
I eventually chose to ignore the plugin and use the CLI mvn command instead for the test with code coverage.
Inside your Eclipse IDE, right click on the red color-marked warning for the
jacoco-maven-plugin. You should three options in the popup to fix the warning, choose to ignore the warning, that would result an automatically generated section in your pom.xml, that started with a line of comments,
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
At a command line, run the mvn command before each checkin, and that should trigger the test goal with coverage,
$mvn clean package
This problem is specific to Eclipse, as outlined on the M2E wiki. Sorry I can't help more than that, as I don't use Eclipse.

How to tell YUI compressor maven plugin to output to WAR file and/or source directories?

I'm trying to add YUI compressor to my JSF eclipse maven project. I'm not a maven professional, I just use the m2e maven plugin to resolve my dependencies and to right click my project und export it to a .WAR file. At this point, the YUI compressor plugin is working and compresses all css and javascript files as soon as I change and save a resource (i.e. a html file). My problem is, that it's neither putting the compressed files back to MyProject/src/main/webapp/resources/css|js nor into the resulting .WAR file. It puts them into MyProject/target/app-0.01-SNAPSHOT/resources/css|js which seems to be of no (?) use. How can I set it up to put the files into the .WAR file and into the original input-directories (so i can see and include them in my html files)? I have read a couple of threads here, but couldn't find the solution.
Here is the relevant part from my pom.xml:
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<!-- I was playing around with this. At this point, the following does the same as the default
setting, i.e. putting the files into the /target/ directory, which is not what i want/need. -->
<configuration>
<sourceDirectory>${project.basedir}/src/main/webapp/resources/js</sourceDirectory>
<outputDirectory>${project.build.directory}/${project.build.finalName}/resources/js</outputDirectory>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<versionRange>[1.3.0,)</versionRange>
<goals>
<goal>compress</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>true</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

maven-site-plugin not generating apidocs folder with javadocs

I inherited a project that is supposed to build javadoc files and place them in the site directory. This is not being done. I have looked at all the examples I can find and I can't figure out where the configuration is broken.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
</plugin>
</reportPlugins>
</configuration>
</execution>
</executions>
</plugin>
Any and all help is greatly appreciated.
You have bound maven site plugin's site goal to prepare-package phase. You have configured javadoc generation in this plugin configuration.
As such, if you run maven's default lifecycle goals like mvn package or mvn install you should get site report with javadocs.
If you ran mvn site, it would skip prepare-package phase to which your plugin configuration is bound and hence would not generate javadoc.

Resources