Maven

Converting your Vert.x 1.x module to Vert.x 2.x

In this post, I am going to list the number of things we needed to do in order to upgrade our application/modules to Vert.x 2.0.

It is more a list than prose, because I need to create this post as I go along the ride. Because I am sure I will forget something that I did, if I do this post-mortem (Is that the correct way to use that term, or is it supposed to be something else.

1) Make sure all your modules match the module naming standard. Vert.x 2.0 will not run your module unless it matches. It has code to catch it immediately.
2) You probably want to upgrade your build to Gradle, since the Gradle template starts things off nicely. This can be very difficult if you are currently using Maven and have lots of dependencies. You will need to add all those dependencies to the gradle.build file. Which is why at this point we haven’t done it yet, I converted my Maven build to a better Maven pom based on Vert.x Maven Archetype. (see paragraph after next)

Another Gradle recommendation is move all the versioning of dependencies into the gradle.properties file.

Option – If you are converting your application that is currently in Maven, and you want to jump to Gradle. First recommendation, don’t. At least not at first, get your application upgraded to Vert.x 2.0, which requires changes to your Maven pom files (Could be lots of different one). Then once you have it working, then look at converting your maven build to Gradle.

For all the build stuff, the approach I took to change our maven pom file was to create a test Vert.x 2.0 project from the new Vert.x maven archetype. Then look at the pom file it creates and basically copy and paste things. Now each project that you have to update, might need different things changed, removed or added. However, really try to get your pom file to match the pom file that that archetype created.

With Vert.x 2.x you will also really want to take advantage of the testtools jar file as it has now become really easy to write Vert.x eventbus integration tests with TestVerticle. I might create a blog post just on that subject later. One that includes writing the integration tests as well as how you can use a TestVerticle to fire off your Vert.x appication within an IDE.

Overall, I would say those are the two main factors to watch out for. 1) naming of your modules and 2) Bigger issue to update your build files. As far as any code changes, there really shouldn’t be any.

Configuring Maven to build your vertx module

How to create a vertx mod from your Maven project.

So you want to package your vertx application into a module and then make it available in some vertx repository to automatically download. How do you do that? If you are using Gradle for your build, check out all the mods that vertx has already created to examples, then copy and paste and change to your project.

But there aren’t any out there with maven as a build. Well, Brian Lalor does have a maven build for his amqp module, and that is exactly where I stole this information from. Now in his project, as of today, it creates a tar.gz file, I want to create a zip, that I can then copy to my mod repository and rename to mod.zip.

So what are we going to do? Well, for our project I decided to use the maven assembly plugin, maven-assembly-plugin. And create an xml file that declared that I wanted a zip file, what jar dependencies to copy over into a lib directory and where my mod.json file was located, as well as any other scripts or code that wasn’t already being packaged into a jar file by the build. Then in the maven-assembly-plugin, point it to the xml file with that configuration.

First lets look at the maven assembly plugin entry in our pom file.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.3</version>

    <configuration>
        <descriptors>
            <descriptor>src/main/assembly/mod.xml</descriptor>
        </descriptors>
    </configuration>

    <executions>
        <execution>
            <id>assemble</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

This declares the plugin, in the configuration section points to where I will have my xml file, and finally the execution, so that when I run package or beyond that the assembly is done.

So that just leaves us with this mod.xml file thingy. let’s take a look at it. (I create a property in the pom.xml file called

<module-directory>${project.groupId}.${project.artifactId}-v${project.version}</module-directory>

This way can use the property to tell the mod.xml locations to place my vertx module code and stuff.

Now the mod.xml file

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd
          ">

    <id>mod</id>
    <formats>
        <format>zip</format>
    </formats>

    <baseDirectory>mods</baseDirectory>

    <dependencySets>
        <dependencySet>
            <outputDirectory>${module-directory}/lib</outputDirectory>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <fileSet>
            <outputDirectory></outputDirectory>
            <directory>src/main/resources</directory>
            <includes>
                <include>amqp-busmod#1.2.0-SNAPSHOT/**</include>
            </includes>
        </fileSet>
        <fileSet>
            <!-- empty outputDirectory puts mod.json at the root of the structure -->
            <outputDirectory>${module-directory}</outputDirectory>
            <directory>src/main/resources</directory>
            <includes>
                <include>mod.json</include>
            </includes>
        </fileSet>
        <fileSet>
            <outputDirectory>${module-directory}</outputDirectory>
            <directory>scripts</directory>
            <includes>
                <include>**</include>
            </includes>
        </fileSet>
    </fileSets>

</assembly>

WHOA!!!! That’s a lot. But most is boilerplate.

First the id will be appended to the name of the zip file it creates. Now it creates a zip file because of the formats/format section. That is simple.

Now in vertx mods are put into a subdirectory called mods. So in the baseDirectory tag we tell it to put all our files down one level in the zip file, so that the root level in the zip is “mods”

The dependencySets section is just telling where the jar files should be copied to. In this case the module’s directory, but in a lib subdirectory of it. Basically mods/{module-directory}/lib

And the last part is the fun part. This is where we tell the assembly which of our files we want in the module directory, or if we are using other modules where to find those modules and copy those directly into the mods directory. This is the fileSets section

So in my mod.xml I am using Brian’s amqp module (the same one I stole from) and I downloaded it locally and put it in my src/main/resources directory. I am actually also using the MongoDBPersistor module, but I am going to get that from the vertx central repository when I actually run my module. So the fileSet for that has an empty outputDirectory, which means it will be copied into the baseDirectory setting which is set to mods directory.

The second fileSet is for my mod.json, which is required for a vertx module and tells vertx the main script/class and maybe some other configuration.

And finally the last fileSet is my actual verticle code. I am using Groovy scripts, so I created a scripts directory off the root of my project’s directory and placed my Groovy scripts there, in my case there are actually 5 Groovy script files; 1 verticle, 2 worker verticle, 1 main app script to deployVerticle, deployMod etc, and one Groovy config file. The last is not necessary, but will also be part of another blog post for vertx using environment configurations. But I haven’t gotten that to work yet. ANYWAY, since I have a few files in this scripts directory I just use the ant style “**” wildcards to say take everything in that scripts directory and also copy those to my module’s directory. So in the mods/{module-directory/ directory.

Now when I do mvn package, I look in the target directory when it is done and I see a zip file with my module name on it with “-mod.zip” at the end.

So what I usually do next is unzip that file directly in the target directory so I can run my module directly from the target directory and test away.

Due to complete ass wipe spammers, I have to turn off comments and trackbacks.