Skip to content

Lab 07

In this lab session you will practice the basics of the maven build system. You do not need to download any new code for this lab session.

Maven

In class, we've seen that maven is a build system that allows automatic management of dependencies, and modification of the build process by help of plugins. In the remainder of the lab session, you will use the maven command, and simple amendement to the pom.xml configuration file.

Hello World

Maven projects must respect a specific layout of documents, and a specific pom.xml syntax.

  • In class we've seen a command to set up a new maven project, using the mvn command:
      mvn archetype:generate \
      -DgroupId=ca.uqam.info \
      -DartifactId=MavenHelloWorld \
      -DarchetypeArtifactId=maven-archetype-quickstart \
      -DinteractiveMode=false
    

Your turn

  • Customize the command to the group id is a valid package name, representing you as a developer: ca.uqam.info.lastname
  • Run the command and inspect the structure of the newly created project.
  • Go into the new project and compile the sources, using the mvn clean compile command.
    • Why is it good practice to run the clean parameter every time ? Try just mvn clean and mvn compile to spot the difference.

The exec plugin

By default, maven projects only compile and have no main class specified.

Why, should not all java projects have a main class?

Actually, plenty of java projects only provide functionality, but no main class to run / launch the code: These are all java libraries.

In class I've briefly shown how the pom.xml can be edited to specify a main class.

Your turn

  • Revisit the course notes, look up usage of the exec plugin.
  • Edit the pom.xml sources, add the exec plugin and specify the main class.
  • Run your project with mvn clean compile exec:java
  • Verify your program correctly runs and prints the Hello, World message.
Why not just mvn exec:java (which also runs the code) ?

mvn exec java does indeed run the code. However, it does not compile the sources, so possibly you are running something different than what you have currently written in your source editor. Always use mvn clean compile exec:java to run your code.

Using a library

In class, we've seen how maven can be used to add libraries to a project, by adding a dependency block to the pom.xml.

  • Dependencies allow you to conveniently access java libraries.
    • Libraries are great whenever you want to reuse a complex functionality
    • An example is MD5 hashing, which is relevant for checksums and file integrity verification
    • Example: MD5 hashsum of the String Schiedermeier is 50707bed5659e48b953b8b64a64c9f1c

Your turn

  • Integrate the apache commons io library into your maven project.
    • Visit the maven central respository
    • Search and find the apache commons codec artifact using the search field.
    • Copy the dependency block (xml snippet) for the most recent library version
    • Add the snippet to the correct place of your pom.xml
  • Modify your hello world project, so that...
    • It creates a string with your lastname
    • It correctly calls the md5Hex method to convert your last name into a MD5 hashsum
    • It prints the hashsum to screen
  • Verify the result, using this online hashsum calculator

The JavaDoc plugin

At the start of the lecture you've learned how to generate human-readable JavaDoc using the javadoc command.

  • Documentation should be updated, whenever code is changed
  • Of course, it would be tedious to manually call the javadoc command every time you modify code.
  • Luckily javadoc can be invoked as part of the build process with maven. All you need is the javadoc plugin.

Your turn

  • Revisit the course notes on maven plugins, find the plugin snippet to add to your pom.xml.
  • Add the javadoc plugin to your pom.xml configuration.
  • Rerun maven with mvn clean package
  • Carefully read the output of the build process.
  • For every warning or error, fix your documentation until the build passes.

The Jar plugin

Often times you want to ensure your java project can be delivered to the client as a single file. In the last course unit you've learned that JAR files can be created manually, by zipping compiled java bytecode.

  • Like documentation, it is good practice to produce a deliverable jarfile with every code modification.
  • Like with documentation it would be tedious, to do so manually, using the command line, on every code change.
  • Luckily maven can be configured to do so for you.

Your turn

  • Revisit the course notes on maven plugins, find the plugin snippet to add to your pom.xml.
  • Add the maven-jar-plugin plugin to your pom.xml configuration.
    • Make sure to specify the main class in the plugin's configuration.
  • Rerun maven with mvn clean package
  • Carefully read the output of the build process.
  • Inspect the generated target folder, is there a new file?
  • Verify you can run your jarfile, using the java -jar yourfile.jar command.

Test your TP Jarfile

Your next TP submission must produce a valid jarfile on mvn clean package. Test the produced jar before you submit, to make sure you get those points.`

The green triangle

For your TP you certainly want to use an IDE, for example IntelliJ. That is perfectly fine, and we'll also cover IDEs in more detail in a future lecture. However, be very careful with buttons and menues that seem to magically simplify things.

IDEs are powerful, but think before you click 'run'

By default, the green triangle () is not associated with maven. That is, although you may have a pom.xml file, and your project is structured as a maven project, maven is not called when you click the run button. If you want to use the green triangle, you must manually add a maven run configuration, to overload the triangle's behaviour.

Run configurations are user-defined behaviours associated with the green triangle.

Your turn

Let's now add a configuration that compiles and runs your code, using maven.

  • In the top menubar, select Run -> Edit Configurations
  • In the popup, click the + sign, and select Maven
  • You'll see a maven specific configuration formula:
  • Give your configuration a corresponding name, e.g. Build and run
  • In the Run / Command line field, add the maven command you want to invoked, e.g. clean package exec:java (note that the inital mvn command is obsolete)
  • Close the dialogue and click the green triangle ().
  • Verify maven is called and builds and runs your project.