How to work with frequent local snapshot bundle deployments on Karaf? - maven

I decided to build an application on top of OSGI and Karaf - I really like this stuff. However, I'm struggling a bit with a daily deployment on my local, development machine. I mean.. I make a change and then I would like to test it on my local Karaf instance. And it can happen like couple times per hour.
The way I'm doing it now is a maven build that creates a JAR bundle and then it's copied into the Karaf's deploy directory. I think that it isn't elegant at all.
I was trying to find a way around (google). I read about Karaf's features but it seems that despite the fact that it is a nice mechanism for deploying whole app, it doesn't solve my problem. As I understand it right, it does not check whether new version of my SNAPSHOT jar appeared in my local maven repo, right?

The key to make the update mechanism of karaf work is to deploy from maven instead of using the deploy folder.
Install you bundle like this:
install -s mvn:groupid/artifactID/version
or
install -s mvn:groupid/artifactID/version/typeOfMavenArtifact
Second one is useful for installing for example war/wab artifacts. Full maven protocol specification can be found here.
Then Karaf knows where the bundle came from. You can also check this using la -u. This makes karaf show the update location which now should be a maven uri. You will not that all karaf bundles have an update location like this.
When you now create a new build of your project using maven it will end up in you local maven repository.
Then simply run
update <bundleid>
This makes karaf check the update location (in your case you local maven repo) and reload the bundle from there.
You can even further automate this by using
dev:watch
or for karaf 3+
bundle:watch
This will make karaf check you maven repo for changes in SNAPSHOT bundles it has deployed and automatically redeploy these.
This also works very well together with the remote debugging. Use
export KARAF_DEBUG=true
before starting karaf. It then will listen for a debugger on port 5005.
You can then start a remote debug eclipse session on the same port and nicely debug your application in karaf. This works very well even if you change your bundle using one of the approaches above. So you can debug, find your problem, change the code, build and continue debugging with the changed version.
I also use this frequently when I work at the karaf code base itself as this also works for most of karaf's own bundles.

Related

Is it possible to install the karaf server using maven?

I would like to use maven to deploy a complete karaf server including my own features. But so far I am unable to find a way to add the karaf server itself as a dependency. This would save a lot of manual steps, esp. for the other developers which would not have to setup karaf manually.
This would have the added benefit that I do not have to check in the karaf server into the repository in order to have all required parts available from the repository.
Question: is there a way to configure a project using karaf which will setup the karaf server and all required dependencies without manually downloading the files from the karaf website?
This can be done using the karaf-maven-plugin. There is even an example which does this shipped with karaf: examples/karaf-maven-example.
Here is what the examples do
karaf-maven-example-run uses the karaf-maven-plugin:run goal to download and start a Karaf instance.
karaf-maven-example-run-bundle uses the karaf-maven-plugin:run goal to download, start a Karaf instance and deploy the project bundle in this running instance.
karaf-maven-example-deploy uses the karaf-maven-plugin:deploy goal to deploy the module artifact into a Karaf instance (remote for example).
karaf-maven-example-client uses the karaf-maven-plugin:client goal to execute a shell command on a running Karaf instance.
karaf-maven-example-kar packages a features repository as a kar file, ready to be deployed.
karaf-maven-example-assembly uses the karaf-maven-plugin to create a Karaf distribution.
see here for more details.

Deployment in Jboss fuse using Maven

I have created a Fuse integration project in developer studio 9.0.2 and I'm using jboss-fuse-karaf-6.3.0 as the runtime container.
I want to deploy this project using Maven, but not able to figure out -
How and where to give server URL, user name and password for the deployment ?
Does project gets deployed to fabric or karaf ?
Will appreciate if anybody can help with some pointers.
Thanks in advance.
Is that server running locally or remote?
Locally you can simply define the server in the servers view and then use Add context menu to deploy your project.
it gets deployed to Karaf
When building projects with Maven I use osgi:install and dev:watch commands (available in both JBoss Fuse and vanilla Karaf). The following setup works well for a development machine.
First build with Maven using install goal, so the artifact gets installed in local repository.
Then issue osgi:install (see Manual Deployment in JBoss Fuse docs) command in your container to install your bundle. Job done!
> osgi:install mvn:it.your.package/your-artifact/1.0.0
Once the bundle is installed you will see a message like
Bundle ID: 352
This is the ID of the bundle installed. Issue the following command:
> dev:watch 352
Watched URLs/IDs:
352
Now every time you rebuild with Maven, the bundle gets redeployed automatically.
Watch out some settings to be changed if dev:watch does not reload bundles on JBoss Fuse 6.3.0
edit: use -SNAPSHOT in your version with this setup. Regular versionsmay not redeploy correctly because the container sees the version is the same and may use previously loaded classes, thus causing classloading issues.

Use Maven to start programs

I apologize if this sounds to simple (or the fact that there are other links that define this problem) - but I'm a complete beginner to Maven and even Java.
All that I'm trying to do is to run this code to see what it does:
https://github.com/semanticvectors/semanticvectors/wiki/GettingStarted
The Wiki says that uses can either download the .jar file or use the maven repo. I downloaded their .jar file and tried to run it but failed. I use this code:
java -jar /home/user/semanticvectors-5.6.jar
That .jar file didn't work for me and from other stackoverflow links, it seems that either the .jar file is not setup properly or I have a non-compatitble java version.
In any case, I've decided to try using Maven to get this running. I've installed Maven using:
sudo apt-get install maven
It seems to be working as everything was successful in setup. But now I'm not too sure what to do after. This Wiki (linked above) as go to this Maven repo site (https://oss.sonatype.org/#nexus-search;quick%7Esemanticvectors). To my understanding (and correct me if I'm wrong) I thought Maven is a super repository for developers and testers to work from the same code, so I thought I could use Maven as an alternative to running to program. Anyways, I'm open to any suggestions to get the program running to see what it does, thanks.
If you're interested in knowing more about me: I'm running a 16.04 Ubuntu system with Java 8.
The idea is that you can either build the JAR yourself - get the source from SVN and build it (using maven commands, as maven is a build tool), or you can use the existing JAR that is already "prepared" and ready for use in the maven-repository (nexus, in this case).
The result should be the same - if you use the JAR as a dependency in your code (add it to your pom.xml) or if you build it yourself.
You can learn more about Maven and things will be much clearer...

How to deploy a karaf feature from a local maven repository?

I've created by first project for karaf (4.0.1). So far I've got the following:
an application bundle built with the maven-bundle-plugin. There are going to be more...
a karaf feature created with the karaf-maven-plugin.
a karaf-assembly created with the same plugin
So I'm ready to deliver to the customer - but I'm stuck over the development process. This question answered almost all my remaining questions save one:
After having built my project by running maven install I think I should be able to to get my feature running on a local karaf instance using something like the command feature:install.
I can install my application bundle from local maven by using bundle:install but then I'm down to managing dependencies myself.
Really can't figure out while bundle:install works against my local maven repository and feature:install doesn't.
Found the soloution here. I knew I'd need feature:repo-add - but the name of this command and the official documentation sent me thinking in entirely the wrong direction.
This command does not add a (maven) repository, but adds the location of a feature in the maven repository chain (so I'm thinking the old name addUrl was probably easier to understand).
Anyway - using feature:repo-add mvn:<groupId>/<artifactId>/<version>/xml/features works.
I've just had the similar issue and I've found solution here: http://karaf.922171.n3.nabble.com/Add-additional-local-maven-repository-how-td4028299.html
I've edited the etc/org.ops4j.pax.url.mvn.cfg and uncommented the option org.ops4j.pax.url.mvn.localRepository, setting it to my local repository location:
org.ops4j.pax.url.mvn.localRepository=C:/data/maven-repo
Before installing a feature, you have to register the features repository that provides the feature using feature:repo-add
using feature:repo-add mvn:<groupId>/<artifactId>/<version>/xml

How to update Bundle in Apache Felix OSGi Framework

I'm having troubles updating my bundle from the gogo shell.
I have a gogo shell running on terminal. My bundles are active in there.
Now when I make changes to the code, what steps are necessary to forward these changes into the running OSGi framework?
I ran update <bundleId> but nothing happens. It still runs the same version which was compiled when I started the framework.
Before the update command I opened another terminal and ran mvn clean install (I'm using maven-bundle-plugin along with pax-construct).
When working with Maven, my workflow for checking updated code involves two terminals.
After updating the code, I run mvn install from the project which will build the actual bundle. Keep track of the absolute location on your system where the target bundle is built.
In the running framework, I update the bundle, giving it a URL; something like update 25 file:///Users/you/project/bundle/target/bundle.jar
If you're free not to use Maven and Pax Runner, you can also take a look at bndtools, which is an Eclipse plugin. You can then still take the bundles that bndtools builds for you, and run them with Pax Runner.
Another try is via OBR local repo as follows:
- assume artifact is called test-api, then
a)g! deploy test-api deployed under id: 25
after further changes made to the code and mvn clean install do:
1)g! update 252)g! refreshif no result then before step 1) try g! repos refresh file:///c:/Users/.m2/repository.xml to refresh local OBR repo

Resources