<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jayway Team Blog &#187; Henrik Larne</title>
	<atom:link href="http://blog.jayway.com/author/henriklarne/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Sat, 11 Feb 2012 10:33:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>One artifact with multiple configurations in Maven</title>
		<link>http://blog.jayway.com/2010/01/21/one-artifact-with-multiple-configurations-in-maven/</link>
		<comments>http://blog.jayway.com/2010/01/21/one-artifact-with-multiple-configurations-in-maven/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 17:54:52 +0000</pubDate>
		<dc:creator>Henrik Larne</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4363</guid>
		<description><![CDATA[Problem When working on www.beertoplist.com I ran into a Maven problem, that is fairly common: Having a project that should be configured differently for different environments. That is for instance you want one configuration for development, one for test and one for production. I wanted a solution that allowed me to make changes to all [...]]]></description>
			<content:encoded><![CDATA[<h3>Problem</h3>
<p>When working on <a href="http://www.beertoplist.com">www.beertoplist.com</a> I ran into a Maven problem, that is fairly common: Having a project that should be configured differently for different environments. That is for instance you want one configuration for development, one for test and one for production. I wanted a solution that allowed me to make changes to all kind of configuration files, for instance property files, Spring context files and Tomcat context files. </p>
<h3>Poor solution</h3>
<p>Googling on the problem I found that most people suggested that you should use a set of Maven profiles, one for each environment. So when you want to build the project for test environment you enable the test profile and get all the test configuration. At first glance this might seem like a good solution, but there is a major problem with this solution. </p>
<p>When you have your artifact, how do tell which environment it is configured for? Even worse what if you forget to enable the production profile when releasing or maybe enable the test profile instead. Then you have released a test version of the project and there is no way you can tell except for checking the details in the artifact. Further more you commonly want to deploy the released version to a test environment, but you can not do that out of the box, because the released artifact in the Maven repository is configured for production (if you enabled the right profile). Thus you need to check out the release tag and build a new version with the test profile enabled. Now you end up with two files with the same name and version, that are configured differently. Not a very appealing solution.</p>
<h3>My solution</h3>
<p>My suggestion is to instead let Maven build one artifact for each configuration and label them accordingly, so when looking at a specific version in the Maven repository you find, one development artifact, one test artifact and one production artifact, each clearly labeled. Then you never run into the problems listed above. So how can we accomplish this?</p>
<p>Maven have the concept of classifiers. This is a suffix that is appended to the name of the artifact to distinguish it from the main artifact. This is used for instance by the source plugin that creates a source jar file for a Maven project, resulting in a file with the name &lt;artifact id&gt;-&lt;version&gt;-sources.jar. The classifier for this file is "sources". The same concept can be used to build a development, a test and a production artifact, which is the solution I choose in <a href="http://www.beertoplist.com">www.beertoplist.com</a>. </p>
<p>I decided to put all the environment specific configuration in a special source tree, with the following structure:</p>
<pre>
+-src/
  +-env/
    +-dev/
    +-test/
    +-prod/
</pre>
<p>Then I configured the maven-war-plugin to have three different executions (the default plus two extra), one for each environment, producing three different war files: beer-1.0-dev.war, beer-1.0-test.war and beer-1.0-prod.war. Each of these configurations used the standard output files from the project and then copied the content from the corresponding src/env/ directory on to the output files, enabling an override file to be placed in the corresponding src/env/ directory. It also supported copying a full tree structure into the output directory. Thus if you for instance wanted to replace the web.xml in test you simply created the following directory:</p>
<p>src/env/test/WEB-INF/</p>
<p>and placed your test specific web.xml in this directory and if you wanted to override a db.property file placed in the classpath root directory for the test environment you created the following directory: </p>
<p>src/env/test/WEB-INF/classes</p>
<p>and placed your test specific db.property file in this directory.</p>
<p>I kept the src/main directory configured for development environment. The reason for this was to be able to use the maven-jetty-plugin without any extra configuration.</p>
<h3>Configuration</h3>
<p>Below you find the maven-war-plugin configuration that I used to accomplish this:</p>
<pre class="brush:xml">
&lt;plugin&gt;
  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
  &lt;configuration&gt;
    &lt;classifier&gt;prod&lt;/classifier&gt;
    &lt;webappDirectory&gt;${project.build.directory}/${project.build.finalName}-prod&lt;/webappDirectory&gt;
    &lt;webResources&gt;
      &lt;resource&gt;
        &lt;directory&gt;src/env/prod&lt;/directory&gt;
      &lt;/resource&gt;
    &lt;/webResources&gt;
  &lt;/configuration&gt;
  &lt;executions&gt;
    &lt;execution&gt;
      &lt;id&gt;package-test&lt;/id&gt;
      &lt;phase&gt;package&lt;/phase&gt;
      &lt;configuration&gt;
        &lt;classifier&gt;test&lt;/classifier&gt;
        &lt;webappDirectory&gt;${project.build.directory}/${project.build.finalName}-test&lt;/webappDirectory&gt;
        &lt;webResources&gt;
          &lt;resource&gt;
            &lt;directory&gt;src/env/test&lt;/directory&gt;
          &lt;/resource&gt;
        &lt;/webResources&gt;
      &lt;/configuration&gt;
      &lt;goals&gt;
        &lt;goal&gt;war&lt;/goal&gt;
      &lt;/goals&gt;
    &lt;/execution&gt;
    &lt;execution&gt;
      &lt;id&gt;package-dev&lt;/id&gt;
      &lt;phase&gt;package&lt;/phase&gt;
      &lt;configuration&gt;
        &lt;classifier&gt;dev&lt;/classifier&gt;
        &lt;webappDirectory&gt;${project.build.directory}/${project.build.finalName}-dev&lt;/webappDirectory&gt;
        &lt;webResources&gt;
          &lt;resource&gt;
            &lt;directory&gt;src/env/dev&lt;/directory&gt;
          &lt;/resource&gt;
        &lt;/webResources&gt;
      &lt;/configuration&gt;
      &lt;goals&gt;
        &lt;goal&gt;war&lt;/goal&gt;
      &lt;/goals&gt;
    &lt;/execution&gt;
  &lt;/executions&gt;
&lt;/plugin&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/01/21/one-artifact-with-multiple-configurations-in-maven/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Java ME Logging over Bluetooth using MicroLog</title>
		<link>http://blog.jayway.com/2008/12/17/java-me-logging-over-bluetooth-using-microlog/</link>
		<comments>http://blog.jayway.com/2008/12/17/java-me-logging-over-bluetooth-using-microlog/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 16:17:25 +0000</pubDate>
		<dc:creator>Henrik Larne</dc:creator>
				<category><![CDATA[Embedded]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[bluetooth]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=604</guid>
		<description><![CDATA[When developing in Java ME and wanting to support several different devices, you wil no doubt run into problems with devices behaving differently. In some cases it is only the appearance that changes and in others the actual functionality varies or breaks. The first step in solving this is to run your Midlet in the [...]]]></description>
			<content:encoded><![CDATA[<p>When developing in Java ME and wanting to support several different devices, you wil no doubt run into problems with devices behaving differently. In some cases it is only the appearance that changes and in others the actual functionality varies or breaks. The first step in solving this is to run your Midlet in the emulators for the devices that you want to support and hopefully you will be able to solve some of the problems here, but unless you are really lucky there will be some problems remaining that needs to be debugged on the actual device. Depending on which device it is you might be able to run On Device Debugging  to solve it, but in many cases you don't have that option or the On Device Debugger is not stable enough. So now you are left with logging as your only tool to debug the midlet. Logging on a small screen device with very limited ram is not trivial however. </p>
<p>What you really want is to have the midlet write the log to your development PC instantaneously and free of charge, then you can log whatever you want to pinpoint your bug. This could be accomplished by logging over Bluetooth. Previously you had to write your own code to handle the logging and Bluetooth communication as well as a server to which the midlet connects and sends the log messages, but now there is a powerful logging framework for Java ME that offers this support out of the box. The framework is <a href="http://microlog.sourceforge.net">MicroLog</a> and is open source under the Apache Software License, so it is free to use as you see fit. MicroLog can be configured with one or more appenders that takes care of the logging messages and sends them to the right destination and each appender can have its own formatter to format the messages appropriately. This is very similar to log4j, so if you have used log4j before, MicroLog is very easy to learn. If you are using Maven 2 you will be happy to know that MicroLog is built using Maven 2 and is available in a public repository.</p>
<p>Setting up MicroLog to use Bluetooth and log to your PC is simple. It only requires that you setup your Logger:</p>
<pre>
private final static Logger log = Logger.getLogger(TestMidlet.class);
</pre>
<p>and that you configure it to use a <code>BluetoothSerialAppender</code>, which is done like this:</p>
<pre>
log.addAppender(new BluetoothSerialAppender());
</pre>
<p>Now your midlet is all set to start logging over Bluetooth. For a full midlet example see the following code:</p>
<pre>
package se.jayway.me;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import net.sf.microlog.Logger;
import net.sf.microlog.bluetooth.BluetoothSerialAppender;

public class TestMidlet extends MIDlet
{

	private final static Logger log = Logger.getLogger(TestMidlet.class);

	public TestMidlet()
	{
		configureMicroLog();
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException
	{
		log.info("Destroying application...");
	}

	protected void pauseApp()
	{
		log.info("Pausing application...");
	}

	protected void startApp() throws MIDletStateChangeException
	{
		log.info("Starting application...");
		final TextBox textBox = new TextBox("Text", "My text", 100, 0);
		textBox.addCommand(new Command("Log", Command.SCREEN, 0));
		textBox.setCommandListener(new CommandListener() {
			private int counter;

			public void commandAction(Command c, Displayable d)
			{
				log.debug("You clicked " + counter++);
				log.debug(textBox.getString());
			}
		});
		Display.getDisplay(this).setCurrent(textBox);
	}

	private void configureMicroLog()
	{
		log.addAppender(new BluetoothSerialAppender());
	}
}
</pre>
<p>Build and deploy this midlet to your Bluetooth enabled device, supporting JSR-82, the Bluetooth API and make sure that Bluetooth is turned on.</p>
<p>Next you need to get the Bluetooth server that will receive and print the logging messages to standard out. The server is also available in a public <a href="http://microlog.sourceforge.net/repo/m2-snapshot-repository/net/sf/microlog-servers/">Maven repository</a>. Just download the version that ends with -jar-with-dependencies.jar. This file contains all its dependencies. Now make sure that your Bluetooth unit is on and discoverable on your PC. Then start the server with the following command:</p>
<pre>
java -cp microlog-servers-1.1.0-SNAPSHOT-jar-with-dependencies.jar net.sf.microlog.server.btspp.MicrologBtsppServer
</pre>
<p>You might have to change the jar file name if it is not the same as the one you downloaded, then just replace it with the file name of your own microlog-server jar file.</p>
<p>The server starts up and registers a logging service with a unique UUID that the phone will try to locate and connect to. Now start the midlet and see how it will start logging over the Bluetooth connection.</p>
<p>The first phone I tested was a SonyEricsson C905 and it worked perfectly from the start. Then I tried to use a SonyEricsson P990i and for some reason it failed to locate the logging server with the unique UUID. I was unable to determine the reason why it failed to locate the service, so instead I added support to the <code>BluetoothSerialAppender</code> to specify the url of the Bluetooth logger server. Using this and setting the server url to my PC it connected flawlessly to it and started logging. So if you experience problems when your midlet connects to the PC or if you are using multiple logger servers within the Bluetooth range of your device try specifying the server url that the <code>BluetoothSerialAppender</code> should connect to. The url of the server is shown when starting the logger server. This url does however contain a channel that is not possible to determine through the used API's, but it does start on 1 and increases. For each session I always got the same channel and it was only the next day that it changed and I got channel 2 instead, so try with 1 to start with and if that does not work increase it by one and try again. It will most likely work first or second time.</p>
<p>Now you are free to log as much as you want and do not have to restrict your logging during the development phase of your project. Neither is there a reason for writing your own Bluetooth logger, just use MicroLog.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/12/17/java-me-logging-over-bluetooth-using-microlog/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

