Wednesday, August 8, 2012

Maven is groovy!

Recently, I had another one of those cases where Maven almost does the right thing, but not quite. Let me explain the use case:
I've got a software component that can initialize the database from an SQL script. Such an SQL script (in what follows: The DDL, or data definition language script) is ideally generated by the Hibernate Schema Exporter, aka "hbm2ddl", which in turn is available in Maven by running the Hibernate3 Maven Plugin. But, if just creating the database is not sufficient and you need to run a second SQL script (in what follows: The data script) to populate the DB with some initial entries? Well, I came up with the following solution:
  1. At build time, have Maven create the DDL script (below target/classes, so that it is available at run time)
  2. At development time, manually create the data script (in src/main/db)
  3. At build time, have Maven concatenate these scripts into a third SQL script (in what follows: The concatenated script, also below target/classes, as it must also be available at runtime)
Question: How do we do that last step? The most obvious solution was the Maven Antrun Plugin, Ant even's got a "concat" task, which should do exactly what I want (Including uptodate checks). However, I wasn't really happy with that solution, because Ant, or the "concat" task behaved too unpredictable (For example, no error was produced, if either of the source files didn't exist. An, error checking is, where Ant scripts become really nasty.) In the end, I had to admit: It didn't work.
So I came up with another idea: Why not have a small Groovy Script in the Maven POM. And, as is usually the case, someone else already had that idea and there is a Maven Plugin, which already provides just that:
I can embed a Groovy snippet into my Maven POM and have it executed at a suitable point of my build script. Here's the snippet I came up with:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source><![CDATA[
def concat(s1, s2, t) {
def java.io.File f1 = new java.io.File(s1)
def java.io.File f2 = new java.io.File(s2)
def java.io.File ft = new java.io.File(t)
def long l1 = f1.lastModified()
def long l2 = f2.lastModified()
def long lt = ft.lastModified()
if (l1 == 0) {
throw new IllegalStateException("Source file must exist:" + f1);
} else if (l2 == 0) {
throw new IllegalStateException("Source file must exist:" + f2);
} else if (lt == 0 || l1 > lt || l2 > lt) {
java.io.File pd = ft.getParentFile()
if (pd != null && !pd.isDirectory() && !pd.mkdirs()) {
throw new IOException("Unable to create parent directory: " + pd)
}
println("Creating target file: " + ft)
println("Source1 = " + f1)
println("Source2 = " + f2)
java.io.FileInputStream fi1 = new java.io.FileInputStream(f1)
java.io.FileInputStream fi2 = new java.io.FileInputStream(f2)
ft.append(fi1)
ft.append(fi2)
fi1.close()
fi2.close()
} else {
println("Target file is uptodate: " + ft)
println("Source1 = " + f1)
println("Source2 = " + f2)
}
}
concat("target/classes/com/softwareag/de/s/framework/demo/db/derby/initZero.sql",
"src/main/db/init0.sql",
"target/classes/com/softwareag/de/s/framework/demo/db/hsqldb/init0.sql")
concat("target/classes/com/softwareag/de/s/framework/demo/db/derby/initZero.sql",
"src/main/db/init0.sql",
"target/classes/com/softwareag/de/s/framework/demo/db/hsqldb/init0.sql")
]]></source>
</configuration>
</execution>
</executions>
</plugin>

perhaps in combination with a byte array, for performance reasons, but in Groovy a file has got a method append(InputStream), which does exactly that. And, although I am declaring the variable ft above as an instance of java.io.File, it is nevertheless a Groovy file, with all the added sugar of Groovy! Which is, why embedding Groovy into the POM is much nicer than embedding Java!

In the future. I will most likely never ever write Maven plugins and use Groovy scripts instead.

 Second: We are inside a Maven POM, or, to put it different: Inside an XML file. As a consequence, I've got to be careful with characters like '&', or '!'. Which is why I am using the strings ">" and "&" instead. I might as well use a CDATA section, or, even better: An external script (in src/main/groovy) However, I believed to make this postings point better with an internal (albeit somewhat lengthy) snippet. Hope, you agree, so let's be groovy!


Saturday, November 5, 2011

ICU

Last weekend, I had the unexpected opportunity to participate in the nightly snoring contest held at the intensive care unit (ICU) of the neurological clinic, university of Tübingen. Such a chance comes once in a lifetime, so I could not miss it. Here's how it went:

My wife certainly considered me to be the odds-on favorite. But, alas, even wifes can overestimate their husband: Bed 1 (the contents of which have been yours truly) lost by far. At aboUT 21:00, bed 3 opened with a sonorous snore of about 80 decibel (about enough to be heard in a disco) and immediately took the lead. But even such an awesome competitor had to give in: During the night, bed 2 never ceased to impress with staccati of four to five 70-decibel-snores in a row, taking the first price with him.

Every morning there a friendly female woke me, one of the doctors, who asked to take my blood and apologized in so many words for waking me. When that was done, she continued to do the same at the other beds, effectively waking all of us.

On the last morning there (sunday) I prepared a little speech for her, which I could never hold, because I was moved from the ICU to a normal bed in the night. So, I am trying to do it here and now:

I don't know whether any of my readers has ever spent a night in an ICU bed. It's deeply depressing. The only thing to look at are the bubbles in the bottles over you, which are pooring liquids in your veins, or the monitor, which is showing your blood pressure, heart beat, and stuff like that. With three apoplectic strokes in a row behind you (I promise to stop counting in public now. My inner self is a different matter.) there isn't much to expect or even hope for. Forget about sleep: There is a continuous background noise. Light is never completely lit and every five minutes some machine is beeping alarm, ideally on another bed, but from time to time it's at your own. (Usually, because you turned yourself to the other side.) Think of a Jura coffee machine that requests service to imagine the sound. In my worst moment, the nurse saw fit to blow oxygene in my nose because I seemed to be loosing. (Usually an indication of a heart that no longer works properly, fortunately not in this case.)

After such a night, waking up is a gift! I'm still alive! I can kiss my wife today. With a bit of luck, I can hod our daughter. I can enjoy the smell of coffee. (Something, I couldn't do in the last months even if
I had coffee. But, it works again!) So, don't apologize, Dr., you're more than welcome. I can't tell, whether the other gentlemen share my feelings, but I'll be glad to give a few centiliters of blood, if I can have this day in exchange!

Still there, world!

This is not the end. But, let's face it: This (or any future post) might very well be my last. (In fact, last saturday I'd have been surprised about the additional week that I had since then. So, it seems to be ino order to prepare. So, how can a coder like me leave the world in grace? Like this:


#include <stdio.h>

main()
{
printf("Good Bye, World \n");
}


Sadly, I'm no wizard. For Dennis Ritchie, this would have been



#include <stdio.h>

main()
{
printf("GOOD BYE, WORLD!\n");
}


(One of deaths silly jokes. That'd be style!)

But, for now and me, the only proper thing seems to be:


#include <stdio.h>

main()
{
printf("Still there, World \n");
}

Monday, September 19, 2011

A clash of generations

Yesterday was a relatively minor election in Germany, more precisely in Berlin, in its roles as german capital and as one of the german federal states. The most remarkable thing about that election was this: The german pirate party (direct link in german) got no less than 9% of the votes and 15 seats in Berlins state parliament. (Luckily, they didn't get more because they didn't have more candidates. In other words, additional votes would likely have been lost....)

The reactions fro the mainstream media are remarkably similar to those responding to the first successes of the german green party a (direct link in german) about 35 years ago, along the lines of "This could only happen in a city-state, like Berlin, not in a territorial state." (I should mention that just this year Germany got its first greeen prime minister in a federal state, Baden Württemberg, which is a territorial state. Another typical reaction: "The accountability of being in parliament will quickly dissolve voters illusions.", expecting that the result will be quite different after the next election."

I believe, what most of these responders don't get is that the pirate party is driven by a clash of generations. They won't go away so quickly, if at all.

The pirates voters are mostly people below 40 years. That's exactly the generation that was raised with, or even in, the Internet. To them, the the Internet provides value. It's important. Things like "Vorratsdatenspeicherung" (telecommunications data retention) real name policies, various degrees of censorahip (regardless of the alleged reason: terroriam, child pornography, nazism, not to mention political grounds (Iran, China, Northern Korea) orcopyright violations) are threatening this value. Threatening something important that is.

Take, on the other hand, the elder generation. The internet isn't important to them. It's a toy, that their children or grandchildren are playing with. A real lot of them are even considering a threat. (I remember some politicians assuming that the recent terror attacks in Norway wouldn't have happened withut the Internet. Similar voices can be heard after each and any amok run. Guess the age of such politicians.) They are quixk to call for exactly those things that the younger generation perceives as a threat. To the elders, it's the cure.

To me, that's the same situation that we had when the green party was founded. Our generation considered the protection of the environment as important, our parents and grandparents considered it as a thread (mostly economical). The greens didn't go away. There time came when our generations and those of our children outnumbered our anchestors. I believe the time of the piraztes (or whoever follows them, should they break apart) Perhaps we'll have the first pirate prime minister in another 30 years?

Sunday, August 28, 2011

The mess that is m2e connectors

  • Warning: The following is most likely stubborn, unreasonable,one-sided, and ignores a lot of facts, of which I am unaware.
I am an Eclipse user since approximately 10 years. I am also a user of Maven 2 since may be 4-5 years. As a consequence I am also using the former M2Eclipse (an Eclipse Plugin for using Maven as the Eclipse Builder) since its early days. While I am quite happy with Eclipse and Maven, the experience with M2Eclipse has never been free from annoyance and fights. To be honest, it has always been hard to convince my colleagues to use it. And I fear that battle is lost now. I wasn't the one loosing it: The decisive strike was administrated by M2E itself...

M2Eclipse has recently been moved to the Eclipse Foundation. It is now called M2E, lives at eclipse.org and can be installed from within Eclipse Indigo as a standard plugin, just like CDT, or WTP, which is, of course, a good thing.

So, when Indigo was published (together with M2E 1.0 as a simultaneous release), I rushed to load it down in the hope of a better user experience. But the first thing I noted was: M2E was showing errors in practically every POM I have ever written, and there are quite a few of them, including those of several Apache projects and those at work. So,as a first warning:

M2E 1.0 is incompatible with its predecessors. If you want to carry on using it without problems, don't upgrade to Indigo, or try using an older version of M2Eclipse with it (I haven't tried, whether that works. The reason for this intentional incompatibility (!) are the so-calles M2E connectors, which, I am sure, have driven a lot of people to madness since their invention. In what follows I'll try to outline my understanding of what the connectors are and why I do consider them a real, bloody mess.

I am still not completely sure, what problem the connectors ought to solve, but from my past experiences I guess something like this:

M2E allows you to run Maven manually. You can invoke a goal like "mvn install" from within Eclipse just as you would do it from the command line. That works (and always worked) just fine. Unfortunately, Maven is also invoked automagically from M2E whenever Eclipse builds the project, for example after a clean. In such cases M2E acts as an "Eclipse Builder". It is these latter invocations that people have always had problems with and that the connectors should handle better. First of all, what are these problems?

  1. Builders can be invoked quite frequently. If automatic builds are enabled and you are saving after every 10 keys pressed, the builders can be invoked every 20 seconds or so.
  2. The UI is mainly locked while a builder is running. In conjunction with the frequent invocation that means that the UI can be locked 80% of the time, which a human developer considers extremely painful, in particular, if the builder invokes Maven, which can take quite some time.
  3. Some Maven (I am unaware of any in reality, but the M2E developers are mentioning this quite frequently)plugins assume that they are invoked from the command line. That means, in particular, that System.exit is called once Maven is done. Consequently, they consider use of resources as unproblematic: They acquire a lot, including memory and don't release it properly. The resources are released automatically by System.exit. But that doesn't work in M2E which runs as long as Eclipse does (meaning the whole day for Joe Average Developer) and invokes Maven (and the plugin with it) again and again.
  4. M2E doesn't know whether a plugin (or, more precisely, a plugins goal) should run as part of an automatic build. For example, source and resource generators typicallyshould, artifact generetors typically should not. Consequently, a lot of unnnecessary plugins are invoked by the automatic build, slowing bdown the builder even more, while necessary goals are not. This is not what people expect and leads to invalid behaviour on the side of the developer. For exsmple, I keep telling my colleagues again and again that they shouldd invoke Maven manually, if the test suite depends on a generated property file.
But how should connectors fix this: I am partially speculating here, but my impression is this: When Maven is invoked as a builder, then it is modified by M2E to no longer invoke plugins directly. Instead, Maven invokes the plugins connector, which in turn invokes the plugin. The connector ought to know the plugin and decide, whether a goal must be invoked as part of the automatic build process or not. But that means, in particular:

M2E can invoke a plugin as part of the automatic build process if, and only if, there is a connector for the plugin, or you specially configure the plugin. (More on that configuration later on.)

And that is the main problem we are currently facing: Connectors are missing for a lot of important plugins, for example the JAXB plugins, the JavaCC plugins, the antrun plugin, and so on. The philosophy of the M2E developers seems to be that time will cure this problem, which is why they are mainly ignoring it.See, for example,
bug 350414, bug 347521, bug 350810, bug 350811, bug 352494, bug 350299, and so on. Since my first attempts with Indigo, I am unaware of any new connectors, although the lack of them is currently the biggest issue that most people have with M2E. Try a Google search for "m2e mailing list connector", if you don't believe me.

But even, if the developers were right, they choose to completely ignore another problem: You can no longer use your own plugins in the Eclipse automatic builds, unless you create a connector for the plugin, or create a project-specific configuration. (Again, more on that confuguration in due time.)

At this point, one might argue: If you have written a plugin, it shouldn't be too difficult or too much work to write a connector as well. I'LL handle that aspect below.

First of all, regarding the configuration: Absent a suitable connector, there is currently only one possibility to use a plugin as part of the automatic build: You need to add a plugin-specific configuration snippet ike the following to your POM:


<plugin>
<groupid>org.eclipse.m2e</groupid>
<artifactid>lifecycle-mapping</artifactid>
<version>1.0.0</version>
<configuration>
<ifecyclemappingmetadata>
<pluginexecutions>
<pluginexecution>
<pluginexecutionfilter>
<groupid>org.codehaus.mojo</groupid>
<artifactid>javacc-maven-plugin</artifactid>
<versionrange>[2.6,)</versionrange>
<goals>
<goal>javacc</goal>
</goals>
</pluginexecutionfilter>
<action>
<execute></execute>
</action>
</pluginexecution>
</pluginexecutions>
</lifecyclemappingmetadata>
</configuration>
</plugin>


Neat, isn't it? And so short! This would advice M2E that I want the javacc-maven-plugin to run as a part of the automatic M2E build.

So far, I have tried to be as unbiased as posssible, but now to the points that drive me sick. (As if that were currently required...)

  • The space required for the M2E configuration typically exceeds the actual plugin configuration by far! If there ever was a good example of POM pollution, here's a better one.
  • M2E insists in the presence of such configuration, regardless of whether I want the plugin to run or not.If it is missing, then the automatic builder won't work at all. There is no default handling, as was present in previous versions of M2E. (I won't discuss what the default should be, I'd just like to have any.)
  • The M2E configuration must be stored in the POM, or any parent POM. There is no other possibility, like the Eclipse preferences or some file in .settings. In other words, if you are using IDEA or NetBeans, but there is a single project member using Eclipse, you still have to enjoy the M2E configuration in the POM. As bug 350414 shows, there are a real lot of people who consider this, at best, ugly.
  • I tried to play nice and start creating connectors. But this simply didn't work: I am a Maven developer, not an Eclipse developer. And a connector is an Eclipse plugin. I'm not interested in writing Eclipse plugins. (Which Maven developer is?) But there is nothing like a template peoject or the like, only this well meant Wiki article, which doesn't help too much. For example, it assumes the use of Tycho, which only serves to make Eclipse programming even more complicated.
  • The design of the connectors looks broken to me. Have a look at the AbstractJavaProjectConfigurator, which seems to be the typical superclass of a connector: It contains methods for configuring the Maven classpath, for adding source folders, for creating a list of files that have been created (or must be refreshed): These are all things that are directly duplicating the work of the Maven plugin and should be left to the Maven plugin, or Maven, alone. In other words:
  • Circumventing the Maven plugin is bad. Deciding whether to run or not should be left to the plugin, or Maven. (See, for example, the comment on "short-cutting" code generation on the Wiki page on writing connectors
    .)

To sum it all up:

I fail to see why we can't throw away the whole connector mess and replace it with a configurable Maven goal that should be run by the automatic build ? There is even a reasoable default: "mvn generate-resources". Let's reiterate the reasons for inventing connectors from above and compare it with this solution:

  1. Maven wouldn 't be invoked more frequently
  2. If a single Maven execution takes too long, fix the plugins that don't do a good job at detecting whether they can short-cut. Ant still does a better job here, years after the invention of Maven 2.
  3. If some plugins don't behave well with regard to resources, fix'em. If we can wait months or years for connectors, we might as well wait for bug fixes in plugins.
  4. The question whether to run a plugin or not can be left to the Maven lifecycle, if we choose a lifecycle goal like "generate-resources" Maven knows perfectly well the plugins and goals to include or exclude.

Friday, August 19, 2011

Alive - and kicking

For more serious matters: Last tuesday I was struck by a left sided apoplexy. The good news: I am alive. (Obviously) I am at home, having left the hospital today. Using the keyboard is still very difficult, though (Excuse for any typos ...) Need to get this better over the next weeks to become ready for the job ...

Monday, July 25, 2011

Closing the ticket

Quoting from a support ticket:


As we are still reducing the cost of operations in the IT department, we are currently working on a limited number of service requests. As a consequence, we are unable to work on your ticket. Thanks very much for your understanding.


I won't name the company. (And, just to make sure: No, it wasn't my employer.)