Pages

Wednesday, June 27, 2012

I'm following the development of the Scala IDE somewhat closely and report now and then a bug. The plugin gets better and better - there are still some quirks but there are workarounds for that.

For me it works best if I use the sbt compiler via command line in parallel to using Eclipse in order to hunt down compile errors, since sometimes the IDE does show misleading information. This proved itself to be a good approach over time.

Maybe I would get less problems if I would use a stable released version? It speaks for itself If I can use the nightly builds without much hassle anyway ... :)

Sunday, June 24, 2012

I've discovered that embedding videos is quite easy in the blog, so here I go ;-)

If you have only a vague idea what "the java" is all about, you'll enjoy this video:

It's a promo trailer for Javazone 2012, a conference held in Oslo in September 2012.

From their website:

JavaZone is the biggest meeting place for software developers in Scandinavia, and one of Europe's most important. JavaZone has been described as a high quality, independent conference - a leading forum for knowledge exchange for IT-professionals.

A great place to meet people who may share the same interests than you are.

JavaZone is the biggest meeting place for software developers in Scandinavia, and one of Europe's most important. JavaZone has been described as a high quality, independent conference - a leading forum for knowledge exchange for IT-professionals.JavaZone is the biggest meeting place for software developers in Scandinavia, and one of Europe's most important. JavaZone has been described as a high quality, independent conference - a leading forum for knowledge exchange for IT-professionals. Each year around 2,300 conference tickets are sold. This years JavaZone is the eleventh and surely not the last.JavaZone is the biggest meeting place for software developers in Scandinavia, and one of Europe's most important. JavaZone has been described as a high quality, independent conference - a leading forum for knowledge exchange for IT-professionals. Each year around 2,300 conference tickets are sold. This years JavaZone is the eleventh and surely not the last.

Saturday, June 16, 2012

In this post I want to describe how to use the jetty web server in your application.

freedigitalphotos.net

One of the great things about jetty is that it can be used standalone - that means there is some binary which acts as a "server" and then there are "archives" which can be deployed on that server - (web application archives).

As a developer, you just have to obey certain rules for creating such an archive, that is to say you have to implement certain interfaces, fill out xml files with some meta information, and you are good to go.

Of course you could download the jetty server from it's site, bundle your war file which contains your application logic together, create an installer - but it is easier if you hide all the details from your user and provide just a plain jar file which, when double clicked, does the rest.

Running an embedded jetty means:

you have to integrate the server somehow with your sources

you have to get from a main() method to the point where you start the web server

you have to put all relevant classes in the jar file - not only your application logic but also all classes which are needed for the server

you might be interested in delivering a compact binary without 'dead code' - so you might not use every feature jetty provides

you want to have a short start up time and your webapplication should load its welcome screen.

you want to stop the server again.

The first two points make it clear that you have to use internal jetty classes to initialize your main application. You have to tell those classes what web application is to be loaded, which port you want to use, the timeout you want to set, and some other things.

Point three and four clearly indicate that this is a job for proguard, since this is exactly what proguards can do very well.

Point five says something about the start up mechanisms. On the first run, all of your classes are contained in the jar file, they have to be unpacked, verified and what not - this can take some time. It may be more efficient to unpack your jar and start then those classes. Thus you may want to support different scenarios here.

(You could also think about giving your application a nice splash screen, and start a browser in kiosk mode for example. The point is that you have a very fine grained control of the behavior of the server.)

For my example application, I need a configured web application and also means to serve static content.

I'm using jetty8 which means i have to include following dependencies in the maven pom:

As you can see, you can easily start from an jar file (see createServer(port: Int) method, or use a directory with the proper structure for it (means that it has to contain a WEB-INF directory and so on - just unzip a war file and you'll see what is necessary).

The above approach uses a mixture of programmatic configuration of the jetty web server and a declarative one - the web.xml configures the web application - therefore chances are high that above code could work with your war file, too.

Note: When using this approach, you are interested to create a jar file with a main class - this differs from distributing a war file.

This code opens also a door to start and stop a jetty webserver programmatically, which is very useful in respect to executing automated tests.

Thursday, June 14, 2012

Don't underestimate how much effort it needs to get your program properly packaged.

It is nice - but not more - if the program works in your IDE.

It is of no value whatsoever if the code doesn't make it to the production system since there are flaws in the chain after you've committed it in your code repository, or there are some manual steps involved which render a continuous integration scenario impossible.

A great part of maven copes with assembling artifacts together, zipping files, copying, or even deploying them to the proper place. You can get very creative with maven, you can even extend it with your own plugins (in maven speak: mojos). Quite easily.

It is not so easy to always know what is "the right way" to do it.

In my opinion it is nice if you have many options, but always prefer to keep it simple. Therefore, it is better if you use three standard tools which do the work sequentially, than to write your own optimized version of your very special process.

Since the latter isn't so special really, have a look at the existing plugins, it will save you lot of work.

However, reading documentation is often the enemy which stands between you and your deadline. Doing it the right way depends on your knowledge at a specific moment in time.

You can and should always refactor your build definitions.

You could start for example by implementing certain steps of your build using a shell script or a bat file, or implement a process step in your favorite programming language. You can implement it 'natively' by configuring a multitude of maven plugins with a dozen xml files. Doing it the one way or another always depends on your knowledge, the team situation, available resources ...

Of course, you can get religious about this, but you shouldn't. It should be 'good enough'.

Every mentioned plugin has nice features which can serve you in many occasions, I'll highlight some usecases here.

Maven Assembly Plugin

This plugin comes in very handy if you want to create zip files. You can basically collect arbitrary files from the harddisk and put them via an xml configuration file in the zip file. You can even create directory trees inside the zip file for that matter. +1

Maven Shade Plugin

This is a very interesting thing: by providing 10 lines of xml you'll get a jar file with all dependent class files which enables you to have a standalone jar file. Very useful for distributing your application.

Maven Dependency Plugin

If you ever wondered about an easy way to get an artifact out of the central or your lokal repository - maybe to include it in your assembly - this is the way to go.

Maven Proguard Plugin

Proguard! I bet every Android Developer loves its capability to shrink jar files. Even better, it can obfuscate your code in order to make a reverse engineering very painful.

Maven AntRun Plugin

Who doesn't know Ant? Maven and Ant are good friends. This plugin is the bridge to the Ant world - which is definitely worth knowing.

Maven Exec Plugin

By using this plugin you can fork arbitrary java programs - or even native executables of your choice.

Using all the goodness

You can combine the plugins for example to do the following:

create some artifacts with by using the antrun and exec plugin (binding them to certain phases in your build)

get some stuff via the dependency plugin

compile some other part of your system from your sources

obfuscate or shrink the code with the proguard plugin

use the jar plugin to create an executable jar file

You can imagine by this invented example that those tools can be part of a complex build scenario. I wanted to create here an example in order to get a feel how one can use these plugins.

Here is an example pom where aforementioned scala-compiler plugin is in action and also proguard plugin is configured.

This shows how to configure the proguard plugin to use certain dependencies as 'program jars', also the scala library is included. I did not mention yet the maven-jar-plugin, which is configured in the above example in order to create a manifest file and a main class in that file in order to be able to call the artifact with

Monday, June 11, 2012

In case you want to include Scala code in your project, you can do this by using the maven-scala-plugin.The plugin has reasonable defaults, configuring a scala project with maven has become quite trivial.

All you have to do is to include the following code in your build section of your pom.xml:

This suffices if you place your Scala classes in src/main/scala and src/test/scala. The jvm arguments give the Scala compiler a little more space which may speed up the compilation process.

Running Unit Tests with Scala and Maven

I'm using JUnit and ScalaTest in my project. As a matter of fact, it doesn't really matter which framework you choose for your tests, as long as you write tests. If maintenance of tests is more of a concern at a later phase of the project, it will pay of that you apply the same quality standards to your test code as you do for your production code. I've learned that tests need a certain amount of work and attention in order to give you the revenue they promise. In trivial cases it is ok to cut'n paste in order to give test cases some variation, but this gets easily out of control - so be sure you create helper methods and classes which encapsulate complicated setup strategies in order to achieve a code reuse. It's more or less always the same.Coming back to ScalaTest, it is a very nice testing framework which allows you to pursue different styles of testing, whatever fits best for your taste or environment. Here is a taste of ScalaTest:

ScalaTest is really very well documented on their site, have a look. You may have noticed I've using here an annotation org.junit.runner.RunWith - this is solely for the purpose to execute the Specs in Eclipse as tests. As soon as the IDE Support for ScalaTest is fully functional one could remove this annotation and have an equally comfortable integration. There is a video online where Bill Venners shows what can be expected for ScalaTest integration in Eclipse - I'm looking forward to it!To integrate ScalaTest in your maven build you want to have something like this in your pom:

Thursday, June 7, 2012

How to get syntax highlighting to work on blogger.

Not.

Ok, first problems first.

How the heck do I integrate a decent syntax highlighting in blogger? After some googling I found that there are several tutorials to include the syntax highlighter of alex but i didn't find a possibility to do this with the new design / template system of blogger.

On the linked page above are descriptions for the old design I suppose? Lets investigate.

After 1 minute reading I've learned that I have to include the links to alex website (he'll get some traffic if every blog pingbacks to his server ...) right after the <head> section.

Lets try to change the html code of the header.

...

Nope.

Not really surprised, though. Blogger wants you to stay away from HTML, which is perfectly ok, but there must be some kind of expert mode.

After fiddling around for 10+ minutes I managed to find the button to download the raw source code of the template, and, recalling my HTML experiences stemming from Netscape- times i finally managed to insert the 10 lines including the necessary javascript. After happily clicking on upload/restore I finally got an error, again.

Thinking that I somehow got a typo in my inserted script links, I made a second test: Just backup and restore without changing the xml should produce no errors.

Sunday, June 3, 2012

This is my 4th attempt of a personal blog, all previous version got buried somewhere in the digital graveyard.

Not quite sure where to go, I finally decided to try out blogger.

Where to go?

My last blog was titled "rlog" which had quite some visitors and gained some attraction, but it was self hosted with a wordpress installation which had to be patched every week. This was the main reason I've abandoned it.

This problem should not exist with a cloud hosted blog I hope.

This blog will be mainly about programming (my passion), but we'll see. Have fun reading.