Configure resolution:=optional from maven bundle plugin - osgi-bundle

I need help to configure a dependency as optional,
Using maven-bundle-plugin:3.4.0,
In the Import-packages section, they refereed as resolution:optional
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.4.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
<configuration>
<instructions>
<Export-Package>*</Export-Package>
<Import-Package>
org.junit.*;/resolution=optional/,
junit.framework.*;/resolution=optional/
</Import-Package>
<_nouses>true</_nouses>
</instructions>
</configuration>
</execution>
</executions>
</plugin>
Expectation is generated OSGI manifest file should contains the
resolution:=optional for that library but the changes not reflecting.
Expectation :
junit.framework;resolution:=optional
but in generated manifest file
junit.framework
Did i miss any configuration to reflect it resolution as optional in the
generated manifest file?
On investigation Maven-Bundle-Plugin, BundlePlugin.java File, I realized
that if the artifact contains the optional true then reflected in the
generated manifest file as resolution:=optional
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
But the previous solution should work according to the BundlePlugin.java
source code,
guide me to proceed further.

You just need to remove slashes (/) you have placed around the resolution=optional:
<Import-Package>
org.junit.*;resolution=optional,
junit.framework.*;resolution=optional
</Import-Package>

Related

How to include resource file into osgi bundle from jar dependency with bnd/maven-bundle-plugin?

I'm using maven-bundle-plugin (bnd effectively).
It's straightforward to include a resource file from sources.
For example, a resource file (src/main/resources/some.xml) is moved under target directory (target/classes/some.xml) during build time and can be included into the bundle using <Include-Resource> instruction:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.1</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Include-Resource>
some.xml=target/classes/some.xml,
</Include-Resource>
</instructions>
</configuration>
</plugin>
Let us have a dependency:
<dependency>
<groupId>com.example</groupId>
<artifactId>library</artifactId>
<version>1.0.0</version>
</dependency>
How to reference resource file inside dependent jar?
In other words, how to
specify something like this:
com.example:library:1.0.0:jar/some.xml
instead of this:
target/classes/some.xml
so that resource from one of the dependency appeared in output bundle jar?
You can use the maven-dependency-plugin to un-compress your dependencies jar and then include the resource in your jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<markersDirectory>${project.build.directory}/dependencies/dependency-maven-plugin-markers</markersDirectory>
<artifactItems>
<artifactItem>
<groupId>DEPENDENCY_GROUPID</groupId>
<artifactId>DEPENDENCY_ARTIFACTID</artifactId>
<type>OPTIONAL_DEPENCENCY_TYPE</type>
<outputDirectory>${project.build.directory}/dependencies/DEPENDENCY_ARTIFACTID</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
...
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
...
<instructions>
...
<Include-Resource>target/dependencies/DEPENDENCY_ARTIFACTID/some.xml</Bundle-Activator>
</instructions>
</configuration>
</plugin>
The Include-Resource instructions is supposed to be pom relative, see Include-Resource, you can probably replace targetwith ${project.build.directory}.
If you have a file reference to the jar, you can do
-includeresource: #path/to/file.jar!/some.xml
You use the # prefix to say the resource is in the jar and the !/ syntax from jar urls.
The tricky part will be getting a path to the jar from the project dependencies I suspect.

Integration testing Maven jar in a Jetty container

I have a Maven project which produces a jar file that is meant to be used in a web service. It has integration tests which use the jetty-maven-plugin to run.
In order to run the integration tests on the compiled jar file, I've had to create a dependency with <systemPath>${project.build.directory}/${project.build.finalName}.${project.packaging}</systemPath>. The integration tests run as I had hoped, using the compiled jar file and correctly creating the web-app out of the src/test directory.
So in terms of this projects build, this setup works very well.
The problem is that the POM file, which is deployed during the release process, still has the systemPath dependency. This means that projects which use the jar are reporting an error during the build. The error says that the jar file "must specify an absolute path". These builds don't fail, but the logs are cluttered and misleading.
I'm looking to remove this systemPath from the POM which is deployed to our Maven repository. How can we do this?
For reference, here is the relevant portion of the project's POM.
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.7.v20131107</version>
<configuration>
<webAppSourceDirectory>${project.basedir}/src/test/webapp</webAppSourceDirectory>
<classesDirectory>${project.build.testSourceDirectory}</classesDirectory>
<useTestClasspath>true</useTestClasspath>
</configuration>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<scope>system</scope>
<systemPath>${project.build.directory}/${project.build.finalName}.${project.packaging}</systemPath>
</dependency>
</dependencies>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Jetty's documentation regarding <classesDirectory> reads:
Location of your compiled classes for the webapp. [...]
So, this should be ${project.build.testOutputDirectory} rather than ${project.build.testSourceDirectory}, shouldn't it?
<useTestClasspath> isn't mentioned in Jetty's doc.
Is it possible to install the dependency and use <scope>provided? Since with that:
[the dependency] is only available on the compilation and test classpath, and is not transitive.
The solution was a slight modification from Gerold Broser's answer.
Here are the relevant sections:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.1.5.v20140505</version>
<configuration>
<webAppSourceDirectory>${project.basedir}/src/test/webapp</webAppSourceDirectory>
<classesDirectory>${project.build.testOutputDirectory}</classesDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>

Maven dependency on a file not artifact

Disclaimer: New to Maven and skimmed the maven documentation
How to configure Maven to look at files to determine what happens in a phase?
I am using the cxf-codegen-plugin to generate sources. This is working correctly when I remove the .java files and execute mvn clean generate-sources. However when I update the wsdl/schema that cxf uses to generate the code, the sources are not recreated. I don't want to have to remember to delete the .java files.
Thanks for taking the time to consider my question.
Relevant excerpt from my pom is below.
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>cxf-java</id>
<phase>compile</phase>
<configuration>
<sourceRoot>${basedir}/src/main/cxf-generated</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/DCMotor.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
<vmArgs>
<vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
</vmArgs>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>

how to run maven war dependency using tomcat:run?

I am using maven for my current projects. Now, I have a war dependency:
<dependency>
<groupId>org.dojotoolkit</groupId>
<artifactId>dojo-war</artifactId>
<version>1.8.1</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
I don't have any problem when I built the war artifact. For this, I have added this plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<overlays>
<overlay>
<groupId>org.dojotoolkit</groupId>
<artifactId>dojo-war</artifactId>
<targetPath>js/dojo-1.8.1</targetPath>
<excludes>
<exclude>WEB-INF/**</exclude>
<exclude>META-INF/**</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
But, when I run the following mvn command mvn tomcat7:run I can not see any javascript resource. It looks like war dependency had not been added.
Could someone help me with that?
Thanks in advance.
Welcome to Stack Overflow Emelendez.
You should read this post : http://webtide.intalio.com/2008/07/dojo-toolkit-maven-repository/
Indeed, the dojo-war dependencies allows you to add some files to your sources, but you must specify some extra actions to include it in the final war.
Dependencies, in Maven, can only be in the language you try to compile (well it's not really exact, but this is enough here). If you want to add other resources (files, images, javascript), contained in a zip/war/targz, you must explicitely extract them. This is what is mentionned in the link I provided previously :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack dojo</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.dojotoolkit</groupId>
<artifactId>dojo</artifactId>
<version>${project.version}</version>
<type>zip</type>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/dojo</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
(Be careful, I'm not sure that this configuration is exact, your dependencie seems to be dojo-war according to your pom.xml, and not dojo as per doc said)
This will extract dojo libs into output directory, and then into your war.

How do I execute simple ant call through maven?

My project runs perfectly fine with following commands:
C:\project\<project_name>\ant -lib ant\lib -buildfile applications/<sub-project-path>/ant/build.xml deploy
However, if I wrap this command either in maven-antrun-plugin or exec-maven-plugin in pom, I get all kinds of path issues.
For maven-antrun-plugin, it seems the certain properties can not be loaded due to path issue. In exec-maven-plugin, it seems that ant target never got passed in correctly.
Can someone please advice how I can apply this in a pom file? Much appreciated.
This is my pom for exec:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>ant</executable>
<workingDirectory>${basedir}</workingDirectory>
<arguments>
<argument>'-lib ant/lib'</argument>
<argument>'-buildfile $basedir/<project-path>/build.xml'</argument>
<argument>deploy</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Haven't tried it, but you could do something similar as documented in the maven antrun plugin example.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>ant</id>
<phase>process-resources</phase>
<configuration>
<target>
<ant antfile="${basedir}/<project-path>/build.xml">
<target name="deploy"/>
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Not sure what library you want to pass as argument in -lib in your snippet above, but the same can be declared as plugin dependencies.
Do note that this plugin does not care about the existence of an ant installation on your system. It downloads necessary ant libraries.
You should pass needed dependencies directly into antrun plugin declaration, right after <executions> element.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
...
</executions>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>${ant-contrib.version}</version>
<exclusions>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>jasper</artifactId>
<version>${tomcat.compile.version}</version>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>${java.version}.0</version>
<scope>system</scope>
<systemPath>${jdk.home}/tools.jar</systemPath>
</dependency>
</dependencies>
</plugin>
I've included some libraries that I use in our project, so that you have an example. Note, that if your build uses some non-standard ( i.e. something outside java.lang ) Java API classes, you have to pass tools.jar as a dependency.
Also, if you use ant-contrib do not forget to exclude ant as a dependency, because it is dependent on some ancient version of ant and you will get a version collision.
Another annoying thing is that dependency assigned directly to plugin execution are not part of POM's <dependencyManagement>, so you have to spell out precise versions. One workaround is to declare version properties in the same place as your central <dependencyManagement> and use the properties instead of hardcoded versions.

Resources