Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Using Amper

  1. The subdirectory tasks/task1_5 in your repository contains yet another version of the ‘Hello World’ application. This is similar to task1_4, except that it uses Amper as the build system rather than Gradle.

    Take a few minutes to explore the files in task1_5 and its various subdirectories.

  2. Open the file module.yaml in your editor and examine it. This is the Amper equivalent of Gradle’s build script. Notice that it is much smaller and simpler. All it does is declare that this is a JVM-based application, with dependencies on a couple of libraries that are needed to run the unit tests.

    To see what you can do with Amper, go to a terminal window, move into the task1_5 subdirectory and enter

    ./amper
    

    This command will work as above on Linux or macOS. If you see a ‘Permission denied’ error on these systems, you can fix this with

    chmod u+x amper
    

    Note: If you are using Windows and your command prompt is provided by cmd.exe, you’ll need to omit the leading ./ from the command to run it. If your command prompt is provided by Windows Powershell then you’ll need to use .\amper.bat to run it.

    Warning

    This will be VERY slow the first time it runs on your PC, or the first time it runs in a newly-created Codespace!

    This is because it will need to download the code for Amper itself, the Kotlin compiler and any application dependencies.

    Subsequent tasks will run much faster.

  3. Enter the following command to run the tests:

    ./amper test
    

    As with Gradle, it isn’t necessary to perform a separate compile step; Amper recognizes that this is a prerequisite to running tests, and it will compile the code first if needed.

    The output generated by Amper is different from that produced by Gradle (and not quite as user-friendly, to be honest), but you should still be able to see details of which tests, if any, have failed.

  4. Now try running the application, with

    ./amper run
    

    You’ll see that Amper is a bit more verbose than Gradle with its logging. This can be distracting. You can suppress it using the --log-level=off option:

    ./amper --log-level=off run
    
  5. Examine the contents of the build/tasks subdirectory. You should see that this contains four subdirectories. Two of them are for the tests and two for the application itself. In each case, one subdirectory holds the individual .class files and the other holds a single JAR file containing those .class files.

  6. Finally, try packaging the application as a portable JAR file:

    ./amper package
    

    This will create a new subdirectory of build/tasks, containing a single file named task1_5-jvm-executable.jar.

    You should be able run the application stored in the JAR file using java -jar followed by the JAR filename.

  7. When you’re done, you can remove all of the build artifacts for the project with

    ./amper clean