Setting up a Vert.x Project to Work with Maven and NetBeans IDE 7.1 on OS X and Java 7
Join the DZone community and get the full member experience.
Join For FreeToday I’ll show you one way to use NetBeans IDE 7.1 and Maven to work with a Vert.x project. This is just one way you can go about setting up the project and is how I currently do it. If you haven’t already done so please make sure to check out my previous article on setting up Java 7 on OS X (Lion).
Make sure NetBeans IDE is fully up to date with at least version 7.1.2 or later. Verify that Java 7 is installed as Vert.x only works with Java 7.
Start NetBeans IDE and create a new project: File -> New Project -> Maven -> Java Application
For the project name give it RouteMatchExample as I’m going to borrow some of the example code for Vert.x.
We need to ensure this project uses Java 7 so when the project is generated right-click on it and choose Properties and then select Compile and set the Java Platform to JDK 1.7.
You should see a file called app.java, refactor that file’s name by right-clicking on it and choosing: Refactor -> Rename. Set the name as RouteMatchExample. Go into the file now and add this example code:
package com.giantflyingsaucer.vertxroutes; import org.vertx.java.core.Handler; import org.vertx.java.core.http.HttpServerRequest; import org.vertx.java.core.http.RouteMatcher; import org.vertx.java.deploy.Verticle; public class RouteMatchExample extends Verticle { public void start() { RouteMatcher rm = new RouteMatcher(); rm.get("/details/:user/:id", new Handler<HttpServerRequest>() { public void handle(HttpServerRequest req) { req.response.end("User: " + req.params().get("user") + " ID: " + req.params().get("id")); } }); vertx.createHttpServer().requestHandler(rm).listen(8080); } }
Now, we need to add the Vert.x dependencies. Since Vert.x is not on Maven Central yet you need to do this manually for now. Right-click on the Dependencies in your project and choose: Add Dependency
We will manually fill in the required information for Vert.x. In the dialog that opens fill in the following data:
Group Id: org.vertx
Artifact Id: vertx-core
Version: 1.0.0
Press the Add button and then add another dependency:
Group Id: org.vertx
Artifact Id: vertx-platform
Version: 1.0.0
This will add two dependencies to your project. You need to right-click on those and choose Manually Install Artifact and then navigate to wherever you placed the Vert.x installation (the Vert.x jars are in the lib/jars folder).
Your Maven POM file will have the following dependencies now:
<dependencies> <dependency> <groupId>org.vertx</groupId> <artifactId>vertx-core</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.vertx</groupId> <artifactId>vertx-platform</artifactId> <version>1.0.0</version> </dependency> </dependencies>
In the POM file let’s add some code to make life a little easier which will move over any additional jar files we might add to our project in the future. This will copy those dependent jars from your project into the Vert.x mod location where you can create a new mod. The Maven xml for this looks like the following (tweak as needed for your settings):
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>/Users/chadlung/vert.x-1.0.final/mods/RouteMatchExample/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin>
The important this here is I’m moving my dependent jars to the following folder: /Users/chadlung/vert.x-1.0.final/mods/RouteMatchExample/lib Of course you need to create the folder RouteMatchExample/lib in the Vert.x mod folder. Read up on modules if you don’t understand them before proceeding.
The other thing you need to do is to copy the class files you generate over to the mod project folder. Since the package I used is: com.giantflyingsaucer.vertxroutes you need to create a folder structure in your project’s mod folder like this: /Users/chadlung/vert.x-1.0.final/mods/RouteMatchExample/com/giantflyingsaucer/vertxroutes
In order to get the class file(s) copied over I used Maven again utilizing the maven-resources-plugin. The POM xml looks like this for that part:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <overwrite>true</overwrite> <outputDirectory>/Users/chadlung/vert.x-1.0.final/mods/RouteMatchExample/com/giantflyingsaucer/vertxroutes/</outputDirectory> <resources> <resource> <directory>${project.build.directory}/classes/com/giantflyingsaucer/vertxroutes/</directory> </resource> </resources> </configuration> </execution> </executions> </plugin>
Now every time you build the project any dependencies are moved over and any class files are as well. Keep in mind though the Vert.x jars are moved into the mod folder as well, those are not actually needed as they are already on the classpath. From the Terminal you can easily run a Vert.x mod by doing the following (make sure Vert.x is on your path):
$ vertx run RouteMatchExample
From a web browser you should be able to use the following URL to generate a result: http://localhost:8080/details/Chad/123
Result:
User: Chad ID: 123 |
My entire POM looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.giantflyingsaucer</groupId> <artifactId>VertxRoutes</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>VertxRoutes</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.vertx</groupId> <artifactId>vertx-core</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.vertx</groupId> <artifactId>vertx-platform</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>/Users/chadlung/vert.x-1.0.final/mods/RouteMatchExample/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <overwrite>true</overwrite> <outputDirectory>/Users/chadlung/vert.x-1.0.final/mods/RouteMatchExample/com/giantflyingsaucer/vertxroutes/</outputDirectory> <resources> <resource> <directory>${project.build.directory}/classes/com/giantflyingsaucer/vertxroutes/</directory> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Thats one way to setup a Vert.x mod project in NetBeans IDE 7.1 using Maven. Leave a comment if you have a better way of doing any of this as I’m always trying to make things easier, thanks.
Published at DZone with permission of Chad Lung, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments