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 17 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 1)

07.28.2014
| 3528 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!

I was late to the party. Just one year ago, I finally decided to use Maven in all my courses. If you can say that you will work your entire career with just one development tool, like a .Net developer must do, then Maven is not for you. For the other 99.9% of Java programmers who find themselves needing to use different development environments such as NetBeans, Eclipse or IntelliJ, occasionally at the same time, then Maven becomes mandatory.

Including 3rd Party Libraries in Student Projects

My initial reason for using Maven was to resolve the problem of including third party libraries that my students needed for their projects. It was necessary to track down the library’s website and download the appropriate files. These files then needed to be copied into the appropriate directory of the project and then added to the IDE’s project build path.

As a teacher, the problem arose when it was time to grade a student’s code. I have been using a repository for many years so students do not submit anything to me. At the project deadline, I simply import their projects from the repository into the IDE we were using. Now the fun began. Projects were missing libraries.

Students are quite clever. Mine are frequently smarter than me. What I have is experience. In this "download the library" environment my students figured out that once a library was added to one project then it could be referenced in other projects. Some even figured out that all they needed was one folder on their computer to hold all the downloaded libraries to which they could reference any project they were working on.

I tell my students that after making the final commit of a project, they should test how it will work when I import it. It doesn’t even have to be after the last commit since that usually happens at midnight of the day before it is due. At any time during the development process, all they needed to do is use a college computer to import the project. The college computers always reset to a clean state so any libraries they installed are not present. If their project will import, build and execute then it is in a format suitable to send to me. Most students do not bother and I find myself adding the missing files. Of course now I am in a bad mood and this may affect how I grade their work.

Manifest Files and JAR Files

A second problem concerns the JAR file. When using NetBeans and performing a "Clean and Build" an executable JAR file is created. The manifest file reads as follows:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2 Created-By: 1.8.0_05-b13 (Oracle Corporation)
Class-Path: lib/mysql-connector-java-5.1.23-bin.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: com.kenfogel.StandardJavaProjectJar

The JAR file does not include the mysql-connector-java-5.1.23-bin.jar file. Instead, the file is in a sub-folder of the folder containing the program jar file. This means that to distribute this program you must distribute the folder and not just the JAR.

I’m sure that somewhere there is a setting to include the libraries in the executable JAR but who has the time to track down every obscure setting of an IDE. I guess I am as lazy as my students can be sometimes.

Managing Versions

A third problem with dealing with libraries manually is that you are responsible for ensuring that you have the latest version. What if you must use a specific version? There have been many a tale of code breaking with specific versions of libraries.

I should point out that all of these problems are magnified when developing server-side code in Java EE or Spring. Add in Arquillian for testing and managing the build can be a nightmare.

What is the solution? The solution is Maven. From the Sonatype Maven by Example book Maven is defined as:

Maven is a project management tool which encompasses a project object model, a set of standards, a project lifecycle, a dependency management system, and logic for executing plugin goals at defined phases in a lifecycle. When you use Maven, you describe your project using a well-defined project object model, Maven can then apply cross-cutting logic from a set of shared (or custom) plugins.

http://books.sonatype.com/mvnex-book/reference/introduction-sect-whatIsMaven.html

From this description, Maven appears to be a complex tool. The reality is quite different.

In my next article I will look at a POM.xml file for a JavaFX project. This Project Object Model file is where you describe what is necessary to compile, build, execute, test and deploy a Java project. Done right, this POM.xml file will allow a project to be used in any IDE and not just the one used for the initial development.

The Sonatype books on Maven are some of the best documentation that I have ever read. You can find all their information about Maven at http://www.sonatype.org/maven.

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.)