DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Maven Simplifies the Reuse of NetBeans Platform Components in Plain Applications

Maven Simplifies the Reuse of NetBeans Platform Components in Plain Applications

Fabrizio Giudici user avatar by
Fabrizio Giudici
·
Jan. 11, 10 · Java Zone · Interview
Like (0)
Save
Tweet
4.88K Views

Join the DZone community and get the full member experience.

Join For Free
Did you ever need to reuse a component you've written for the NetBeans Platform in a plain Java application? It happened to me a couple of times in the past - not a big deal, but a bit of annoyance in having to manage duplicated artifacts, that is a .jar file used by the plain application for every .nbm produced for the Platform applications.

With Maven, the annoyance is completely over, because a Maven Platform project produces and deploys both the two types of artifacts (jar and nbm).

In this 30-sec tutorial (with the exception of the time required to download artifacts from the network) we're going to learn how to use two components from forceTen in a simple, plain Java application.

First copy the following XML to the pom.xml in your working directory:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>it.tidalwave.geo.examples</groupId>
<artifactId>example2</artifactId>
<version>1.0-SNAPSHOT</version>
<name>forceTen Example2 (master)</name>
<url>http://maven.apache.org</url>

<repositories>
<repository>
<id>netbeans</id>
<name>NetBeans Platform Maven Repository</name>
<url>http://bits.netbeans.org/maven2/</url>
</repository>
<repository>
<id>maven2-release-repository.nbpwr.kenai.com</id>
<name>NBPWR Maven Release Repository</name>
<url>https://kenai.com/website/nbpwr/maven-repository/releases</url>
</repository>
<repository>
<id>maven2-release-repository.openbluesky.kenai.com</id>
<name>OpenBlueSky Maven Release Repository</name>
<url>https://kenai.com/website/openbluesky/maven-repository/releases</url>
</repository>
<repository>
<id>maven2-release-repository.forceten.kenai.com</id>
<name>forceTen Maven Release Repository</name>
<url>https://kenai.com/website/forceten/maven-repository/releases</url>
</repository>
</repositories>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>it.tidalwave.geo</groupId>
<artifactId>it-tidalwave-geo-geocoding</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>it.tidalwave.geo</groupId>
<artifactId>it-tidalwave-geo-geocoding-geonamesprovider</artifactId>
<version>1.0.44</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>it.tidalwave.geo</groupId>
<artifactId>it-tidalwave-geo-geocoding</artifactId>
</dependency>
<dependency>
<groupId>it.tidalwave.geo</groupId>
<artifactId>it-tidalwave-geo-geocoding-geonamesprovider</artifactId>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

Note that, repositories apart, there's nothing about NetBeans; it's just a simple pom where I declare that I want to use two artifacts (the GeoCoding API and a service provider for it).  

Now copy the following Java code into a file named Main.java in a subdirectory src/main/java of your current path:

import javax.annotation.Nonnull;
import it.tidalwave.netbeans.util.Finder;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.netbeans.util.NotFoundException;
import it.tidalwave.geo.Coordinate;
import it.tidalwave.geo.geocoding.GeoCoder;
import it.tidalwave.geo.geocoding.GeoCoderEntity;
import it.tidalwave.geo.geocoding.GeoCoderEntity.FactSheet;
import static it.tidalwave.netbeans.util.Displayable.Displayable;
import static it.tidalwave.geo.geocoding.GeoCoder.GeoCoder;
import static it.tidalwave.geo.geocoding.GeoCoderEntity.FactSheet.*;
import static it.tidalwave.geo.geocoding.GeoCoderEntity.FactSheet.FactSheet;

public class Main
{
public static void main (final @Nonnull String ... args)
throws NotFoundException
{
final GeoCoder geoCoder = Locator.find(GeoCoder);
final Coordinate coordinate = new Coordinate(43.5, 6);
final Finder<GeoCoderEntity> nearbyPlaces = geoCoder.findNearByEntities(coordinate, 10);

for (final GeoCoderEntity place : nearbyPlaces.results())
{
final String displayName = place.as(Displayable).getDisplayName();
final String type = place.getTypeAsString();
final Coordinate c = place.getCoordinate();
final FactSheet factSheet = place.as(FactSheet);

System.err.printf("%-30s %-5s: %s\n", displayName, type, c);

if (factSheet.contains(POPULATION))
{
System.err.printf("%42s %d\n", "population:", factSheet.get(POPULATION));
}

if (factSheet.contains(ELEVATION))
{
System.err.printf("%42s %d\n", "elevation: ", factSheet.get(ELEVATION));
}
}
}
}

Now run:

mvn clean install exec:java -Dexec.mainClass=Main

You should see this application output:

Châteauvert                    PPL  : N 43° 30' 00.000" E 6° 01' 00.000", 0.0
population: 147
elevation: 0
Miraval PPL : N 43° 28' 00.000" E 6° 02' 00.000", 0.0
elevation: 0
Saint-Estève PPL : N 43° 30' 00.000" E 5° 56' 00.000", 0.0
elevation: 0
Brue-Auriac PPL : N 43° 32' 00.000" E 5° 57' 00.000", 0.0
population: 959
elevation: 0
Bras PPL : N 43° 28' 00.000" E 5° 57' 00.000", 0.0
population: 1888
elevation: 0
Barjols PPL : N 43° 33' 00.000" E 6° 00' 00.000", 0.0
population: 2619
elevation: 0
Correns PPL : N 43° 29' 00.000" E 6° 05' 00.000", 0.0
population: 723
elevation: 0
Pontevès PPL : N 43° 34' 00.000" E 6° 03' 00.000", 0.0
population: 624
elevation: 0
Seillons-Source-d'Argens PPL : N 43° 29' 00.000" E 5° 53' 00.000", 0.0
elevation: 0

What are you bringing into your application? You can discover it by generating the dependency tree with:

mvn dependency:tree 

The output is:

[INFO] [dependency:tree {execution: default-cli}]
[INFO] it.tidalwave.geo.examples:example2:jar:1.1-SNAPSHOT
[INFO] +- it.tidalwave.geo:it-tidalwave-geo-geocoding:jar:3.1.0:compile
[INFO] |  +- it.tidalwave.netbeans:it-tidalwave-netbeans-util:jar:1.10.20:compile
[INFO] |  |  \- com.kenai.nbpwr:javax-inject:jar:1.0:compile
[INFO] |  |     \- javax.inject:javax.inject:jar:1:compile
[INFO] |  +- it.tidalwave.geo:it-tidalwave-geo:jar:2.0.32:compile
[INFO] |  +- org.netbeans.api:org-openide-util:jar:RELEASE68:compile
[INFO] |  +- it.tidalwave.netbeans:it-tidalwave-netbeans-capabilitiesprovider:jar:1.1.8:compile
[INFO] |  +- com.kenai.nbpwr:edu-umd-cs-findbugs-annotations:jar:1.3.2.3:compile
[INFO] |  |  \- net.sourceforge.findbugs:annotations:jar:1.3.2:compile
[INFO] |  \- com.kenai.nbpwr:javax-annotation:jar:1.3.7.3:compile
[INFO] |     \- com.google.code.findbugs:jsr305:jar:1.3.7:compile
[INFO] \- it.tidalwave.geo:it-tidalwave-geo-geocoding-geonamesprovider:jar:1.0.44:compile
[INFO]    +- it.tidalwave.netbeans:it-tidalwave-netbeans-workspacemanager:jar:2.2.16:compile
[INFO]    +- it.tidalwave.thesefoolishthings:it-tidalwave-util-logging:jar:1.0.5:compile
[INFO]    |  \- it.tidalwave.thesefoolishthings:it.tidalwave.util.logging:jar:1.0.5:compile
[INFO]    \- com.kenai.nbpwr:org-apache-commons-io:jar:1.3.1.4:compile
[INFO]       \- commons-io:commons-io:jar:1.3.1:compile

so there are just a few libraries used by forceTen and a single module from the NetBeans Platform, org-openide-util which contains Lookup. In this case, no problems, as it will work in a plain application. In other cases, some more complex components of the Platform might be required and it would not be necessarily simple to have them working out of the box.

Also, please note that the service discovery mechanism based on the META-INF/services facility is working fine, since it's a standard feature of Java 6: in fact, you're using the abstract GeoCoding API and in the code there's no reference to the GeoNames provider; still, it's discovered at runtime.

application NetBeans Apache Maven

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why Performance Projects Fail
  • Enough Already With ‘Event Streaming’
  • Pattern Matching for Switch
  • How to Optimize MySQL Queries for Speed and Performance

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo