...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:
Ant-Version: Apache Ant 1.9.2 Created-By: 1.8.0_05-b13 (Oracle Corporation)
X-COMMENT: Main-Class will be added automatically by build
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.
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.
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.
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.