How to create OSGi bundle from jar library? - java

How to create OSGi bundle from jar library?

In case you are using eclipse: There is a wizard for that.
It allows you to select a number of jar libraries and creates a plug-in project (i.e. OSGi bundle) including these jars.
You can find it here:
File -> New -> Other ... -> Plug-in from Existing jar Archives.

In principle you just need to add OSGi metadata to the manifest
There is a bundle creator for eclipse which gives a very practical way to add these entries which should be part of the Plugin Dev Toolkit.
Here is an article detailing the process and how to do it with the Bnd tool, maven and so forth.
I personally like the pax tools very much. It is all command line based, but very practical. To create an OSGi bundle of an existing jar you can use bnd tool.

First check out if you can find a osgi enabled version of your library from repositories
SpringSource http://www.springsource.com/repository
Fusesource http://repo.fusesource.com/
If you don't find the OSGi enabled versions. You can go ahead with using the pax tool - PaxConstruct or use the aQute's Bnd tool.

Create a new Plug-in project from existing JAR archive.
Add the jar file to be exported
Click next and name the project.
NOTE:
Make sure OSGI framework is selected in target platform.
Unzip the JAR archives into the project is deselected -> deselecting it, will export all the packages of the JAR
if Unzip the JAR archives into the project is selected then you will manually need to export required packages in the MANIFEST.MF file.
Click finish. You will find project with name transport-5.1.1 created in your workspace. Also you can verify, all the packages of the JAR are exported in MANIFEST.MF file.

The Eclipse Bundle Recipe project provides a Maven based approach for adding OSGi meta data to JARs consumed from a Maven repository.
At its core, it uses the bnd tool. This tool is like a swiss army knife. It analyzes jars and class files and properly calculate package import and exports. You should use bnd for converting proprietary jars yourself. It's available in Maven Central.

Late arrival to the party:
If you're using Gradle, you can add the jar as a normal dependency of your project if you apply the osgi-run plugin.
The osgi-run plugin will wrap the jar transparently into a bundle for you, exporting every package in it and calculating all its imports. As Gradle will know the jar's transitive dependencies, it will do the same for them as well, if necessary.
The jar(s) will be part of the OSGi runtime osgi-run creates, which you can then start up with gradle runOsgi or gradle createOsgi, then executing either the run.sh or run.bat scripts.
The actual wrapping is done by Bnd, the swiss knife of the OSGi world, of course.
If you want to configure how the wrapping happens (what should be imported/exported, usually), you can do it easily in the Gradle build file, see the documentation for details.
Example:
wrapInstructions {
// use regex to match file name of dependency
manifest( "c3p0.*" ) {
// import everything except the log4j package - should not be needed
instruction 'Import-Package', '!org.apache.log4j', '*'
instruction 'Bundle-Description', 'c3p0 is an easy-to-use library for making traditional ' +
'JDBC drivers "enterprise-ready" by augmenting them with functionality defined by ' +
'the jdbc3 spec and the optional extensions to jdbc2.'
}
}

Related

Include external jars in ant

I am new to writing build files and currently I am writing the ant for my project. The issue I am facing is to include the jar to in ant build file.
As per the standard 'ant build' the jars need to be kept in lib folder. But the issue is the jar is very huge, more than 100 GB of size and hence cannot be kept in GitHub.
I have put that in another repository and want to include that in my build file.
Could anyone please let me know how to include the jars in my 'pathelement location' from the url.
Apache ivy is a 3rd party dependency manager, a powerful feature built into more modern Java build tools like Maven and Gradle.
The following answer gives a detailed example, using ivy to manage classpaths and help in the creation of an executable jar.
Class not found with Ant, Ivy and JUnit - error in build.xml?
You mentioned the use of another repository. Presumably this is a Maven repository manager like Nexus or Artifactory. Ivy is capable of downloading from these. (which would be another question :-))

Hibernate Validator: Must I use Maven?

From hibernate.org: http://hibernate.org/validator/documentation/getting-started/
I saw Prerequisites:
Java Runtime >= 6,
Apache Maven
However, "maven" is not even mentioned in the following:
http://www.aviyehuda.com/blog/2010/04/14/using-hibernate-validator-to-cover-your-validation-needs/
Our current project is not maven-based, a student project. Could we still use hibernate validator without using maven? Or is there any better choice for hibernate validation?
Maven is not required. The main reason you want to use Maven is that it makes downloading all the package dependencies super easy.
If it's just a small student project, and you don't want to use Maven, then just download the jars that you would otherwise have downloaded with Maven.
You can manually download the jar files here : http://hibernate.org/orm/downloads/
Maven is optional. But it's a very helpful tool for building Java projects (via many phases like compile, test, package, install...). You'd better learn to use it.
In your question, the hibernate-validator jar will depend on other jars such as hibernate-jpa, validation-api, joda-time...
It means you can not run your project with only hibernate-validator jar file. You need to collect all the required jars together.
Maven will automatically resolve it for you.
You can download from:
https://repo1.maven.org/maven2/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar
Then add this file jar to your project manually. As you seen, downloading jar file is an alternative way when not using Maven.

When a 3rd party Java library is supplied as a collection of individual jars, what is the best way to integrate it in a Maven project?

One of the 3rd party libraries used in my company is supplied to us as a zip file with tens of jars in it. The library releases a new version every two weeks, so I am looking for a solution that does not require a lot of manual effort each time a new release is out. I have recently started using Maven for dependency management and I have found it very difficult to deal with this particular 3rd party library. Should I...
add each individual jar to a company-internal repository (e.g. with Nexus) and then declare each one of them as a dependency to any new projects using this library? This is a laborious process and I can't imagine doing this for every new release.
create a jar of jars to add to our repository? If so, how do I create it? The Maven shade plugin would require that I mavenise the library first, right?
any better suggestions?
Your first option is the best practice. I'm not sure why it should be laborious.
create a simple script to upload the various jars to nexus
use a property in your pom to specify the third-party version so all deps can be changed with a simple property change in the pom
Should be all of 5 minutes to add a new release to your build pipeline.
Alternative: convince the third-party to setup a maven repository for their customers to use!
Its possible, but complicated (but only a 1 time effort) and probably a misuse of what maven's meant for:
Upload zip file to your local maven repo with a specific groupid:artifactId
Create a sub-module that has dependency on this zip file. Tis sub module should be used only for creating the uber jar
Use maven-dependency-plugin unpack goal to unpack the zip file into a directory
Use maven antrun unzip to unzip the jars to a dir
Use maven antrun zip to jar the files into a uber jar
Use maven-intall-plugin to install uber jar to your local repo and use as a dependency.

How can I retrieve the classpath string from my Eclipse project that uses Maven?

How can I retrieve the classpath string from my Eclipse project that uses Maven? In eclipse , I have more than 100 .jar files imported via Maven and refrences as a library called "Maven Dependencies". How can I retrieve the "effective" classpath in the form of a string in my project?
The .classpath file does not reveal this.
You can try to use the java development toolkit (JDT) in Eclipse. The class JavaCore in the org.eclipse.jdt.core plugin can take a reference to your java project (an IProject) and return an IJavaProject which adds the behavior of knowing stuff about the classpath. Maven provides a classpath container that should also collaborate with the IJavaProject to answer back the actual source folders and jars on the classpath. Can't provide with any more info off the top of my head, but I think this approach is your only hope.
Unless you write an eclipse plugin that extends the maven plugin. Documentation is sketchy at best, but in theory you can write an extension that is asked to tweak the contents of the maven container in your project's class path. If at that time you were to cache information about the container contents off to your plugin's state folder, you'd be able to reference it when the user wants you to invoke your script. This extension would be invoked whenever maven is asked to update project configuration or dependencies.
I understand that you want to execute that script yourself and you have Maven installed. I guess the easiest way to run your application in this case is to use the Maven Exec Plugin:
http://mojo.codehaus.org/exec-maven-plugin/
(I'm usually using the exec:exec goal to run the application in its own process.)
Edit:
Otherwise have a look at the Maven Dependency Plugin to prepare the classpath if you don't want to use the Maven Exec Plugin:
http://maven.apache.org/plugins/maven-dependency-plugin/

Applying Maven to a project

I've been asked to apply Maven to a project. After browsing a dozen sites it appears that it's quite vast and I'm not familiar as I'd like with similar tools like Ant. Why is it used/preferred and what does it offer over a standard Eclipse project? Also, how could it be added to an existing project?
Why is it used/preferred and what does
it offer over a standard Eclipse
project?
It is a build tool which can build your project without the need for an IDE like Eclipse. It can create a jar or war or other artifacts from the source, performing a bunch of steps like compilation, running unit tests, etc.
Where maven scores over ant is in managing third-party dependencies and in convention over configuration (which mean less lines of build script if you follow convention).
Also, how could it be added to an
existing project?
You start by creating a new maven project, following the step here.
Place it in the root folder of your project
If your source and resource files do not follow maven folder convention, update maven properties suitably referring to this documentation.
Run mvn package
It will fail if it needs any third party dependencies, which you can add as specified in the doc
With some trial and error, you should have your project running with maven, possibly, much quicker than if you were to set up the same with ant.
Others are already provided sufficient resources to read more about maven.
I suggest to start reading here:
http://www.sonatype.com/books/mvnref-book/reference/public-book.html
Maven is a great tool when you know how to use it. Maven (at core) is a dependency manager.
You include in your pom.xml (similar in function to the build.xml from Ant) all the librairies your project depends on (example : apache commons) along with their version and Maven get them directly from a repository (by default, the central maven repository)
Then you do not have to manually install any jar to make your project work. All is downloaded and cached on your local machine. You can even create an enterprise repository where you put all the jars needed by your company
Maven uses the concept of artifacts which are pre-built library projects with their own dependencies
To mavenize a project, you'll have to write a pom.xml describing your project (examples are numerous), get rid of your libs directory (or whatever classpath you described under Eclipse) and add all your dependencies to your pom.xml
You could also check Mavenizer for a first-start
But Maven is a lot more what i've just said. Read the docs, read poms from librairies and you'll get used to it quickly ;-)
If you use the M2Eclipse plugin from Sonatype, it's just a matter of right clicking the project in the package explorer and choosing Enable Dependency Management in the Maven menu. You are also advised to adjust the directories that contain the sources to the Maven standard directory layout but if you absolutely can't, you can configure that later.
Apart from that: Well, look for tutorials and documentation (for example there is the free book Better builds with Maven. Maven is very complex (yes, I don't think it is simple) and very powerful.

Categories

Resources