Chairperson and Program Coordinator for the Computer Science Technology Program at Dawson College Instructor and Program Consultant for the School of Extended Learning Computer Institute at Concordia University I have been passionate about programming since buying an Apple][+ in 1980. I paid the extra $450 to bring the RAM up to 48K! Ken has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

NetBeans in the Classroom: You Must Use Maven in the Classroom (Part 2)

08.02.2014
| 2016 views |
  • submit to reddit
Ken Fogel is the Program Coordinator and Chairperson of the Computer Science Technology program at Dawson College in Montreal, Canada. He is also a Program Consultant to and part-time instructor in the Computer Institute of Concordia University's School of Extended Learning. He blogs at omniprogrammer.com and tweets @omniprof. His regular columns about NetBeans in education are listed here.

...and On The Job!

Following on from part 1, in this next article on Maven in the classroom, we will start looking at the Maven pom.xml file that NetBeans 8 provides when it creates a Maven-based JavaFX project. By looking at the whole file, this article will also be an introduction to Maven in general.

To begin, we need to create a new Maven project. As we are focusing on JavaFX, we choose a Maven JavaFX Application project template:

The next dialog requires you to name your project. In a Maven project, there are two parts to the project designation. The first is the Artifact Id and the project name you enter will be used for this. The second part is the Group Id. It should be a two word name in all lower case characters with a period as a separator. The name/group id and artifact id become the default package name in the project. You can choose a different package name rather than use the group id and artifact but that will just confuse anyone working on the project with you.

In the world of Maven, the libraries your project may need, such as the MySQL driver or the slf4j logger, are stored in a repository. Every entry in the repository is expected to have a unique name. The combination of Group Id and Artifact Id must make up a name unique in the repository.

A repository is a directory either on your local disk or on a remote server. As a side note, remote servers are now called ‘The Cloud’. Don’t you love marketing! Files that are needed by your project that are not in the local repository are retrieved from the remote server and stored in your local repository. The retrieval of the needed files is carried out when you use Maven to build the project. The standard repository for Maven is found at maven.org. The location of the local repository on your disk is in a folder named .m2 and usually found in your home directory. Your projects can be added to your local repository but not the remote server repository.

Once you have finished with this dialog, NetBeans will create the project. In NetBeans, the project that is created contains a basic “Hello World” program made up of the standard components of a JavaFX program. Right mouse-click on the project name and select Run from the context menu.

If this is the first time you are using Maven, then there will be a delay before the program is built and runs. Maven must create the local repository and download its plugins from its own personal Internet repository at repo.maven.apache.org. Here is just a portion of downloads that occur the first time you use Maven (click to enlarge the screenshot):

If all has gone well you should see the following:

If you run this project again, you will see that it does not download any files. They are in the local repository, so there is no need to retrieve them again.

Project Directory Structure

The directory structure of a Maven project is different from the standard structure of a NetBeans project. This structure allows you to use a Maven project in any IDE without the need to convert it to an IDE native format. In the next image you see three projects. The first is a standard NetBeans Java application. The second is a Maven JavaFX application and the third is a NetBeans JavaFX application.

The Maven JavaFX project also creates the sample application that we ran to test the environment. Of greater interest to us is that this directory structure represents the best practice for developing a JavaFX application. You do not require any of the files that have been created and you will probably delete them. Most beginner tutorials do not use fxml and css files in their first examples. With this layout, you know where they should be placed when you will use them.

The pom.xml file

The file that I am interested in is the Project Object Model (POM), named pom.xml. This determines what actions Maven will carry out. Let’s look at it one section at a time:

<?xml version="1.0" encoding="UTF-8"?>
<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

These first lines are the XML declaration. It tells us that we planning to use XML version 1.0, the only version of XML that I am aware of. The encoding refers to the character set that is used in the document. UTF-8 is the most common form of encoding and although you may think it means 8 bit encoding it actually means that the document fully supports Unicode. This all means that the document may be 8 bit ASCII, 16 bit Unicode, or 32 bit Unicode.

The next line contains the XML root tag named project. Following the tag are references to the namespace and schema used in the document. All you need to know about this is that the XML editor uses this information to verify that the tags you use in the document are legal Maven tags.

<modelVersion>4.0.0</modelVersion>

The modelVersion tag defines the version of the object model that the POM is using. It is required and has been 4.0.0 for as long as I have been using Maven.

<groupId>com.kenfogel</groupId>
<artifactId>FXMavenDemo</artifactId> 

The groupId and artifactId were defined when we created the project.

<version>1.0-SNAPSHOT</version>

The version is the current version of the project. Whatever you place here will be appended to the JAR file Maven creates unless you override it with other tags. You are free to put anything here that you want. One common convention in versioning is to include the string ‘-SNAPSHOT’ to whatever versioning scheme you are using. SNAPSHOT implies a project under development.

 <packaging>jar</packaging>

The packaging tag tells us the format of the file that will be built. The choices are jar, war, and ear.

<name>FXMavenDemo</name> 

The name tag allows us to associate a name with the build. The artifactId is usually the name of the project. The name is an optional tag for internal use. Now for a quick history lesson. The internal name for Windows 95 was Chicago.

There are a number of other optional tags that can be used to provide additional information about the project such as url and description. As you will see when we look at the new version of the pom file in a later article I use some of them.

 <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding
   <mainClass>com.kenfogel.fxmavendemo.MainApp</mainClass>
 </properties> 

The properties section allows you to declare variables akin to environment variables that can be used in later sections of the pom. Within the properties tag you can make up any tag names you want. The two that are declared in this file are:

${project.build.sourceEncoding}
${mainClass} 

Later in this pom file is <argument>${mainClass}</argument>. The variable will be replaced with com.kenfogel.fxmavendemo.MainApp.

The next section reads

 <organization>
   <!-- Used as the 'Vendor' for JNLP generation -->
   <name>Your Organisation</name>
 </organization> 

JNLP stands for Java Network Launch Protocol. It is used to enable Java applets or applications to be delivered via the web to a client computer. To be able to use this method of application delivery you must be able to sign your application with a valid certificate just like those used for SSL security for web transactions. If you are developing commercial Java applications then you will need to have a certificate.

In the next article we will look at the meat of the POM file, which provides the plugins listed in the "build" section.

Published at DZone with permission of its author, Ken Fogel.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)