My mostly technical blog. An on-line journal of my activity in the IT industry and any other technical interests or techy things that happen to catch my attention.

Saturday, 30 June 2012

Introduction to Maker, part 2

Part 2, building our first project with Maker:

In the second part of this series of blogs we'll turn from fetching and bootstrapping Maker itself (from sources) as covered in part 1 to actually using Maker to build a simple project. This will introduce us to the process of defining projects in Maker and in particular the key data structures and APIs involved.

Ok, so starting with the basics. A Maker based build project definition is essentially a Scala file defining the project structures and dependencies. Perhaps the easiest way to get started with a project definition file is to treat it as a Scala script and let the maker script (bin/maker.sh) load the project into the Scala REPL for you.

Maker can pre-compile project definitions for speed, say where a more complex build is perhaps slow in terms of loading/interpretation in the REPL. For now we'll stick to the simple case, a simple project defined in a single Scala file, loaded by the maker script. The example project used in this blog will be a simple Hello world web application. Compiling project definitions for speed will be covered later in this series of blogs.

Defining the application sources:

Let's start by making a directory to hold the whole project. We'll call it testwebapp.

$mkdir testwebapp

While we're at it, let's make the directory structures for the source code.

$mkdir -p src/main/scala$mkdir -p src/main/webapp

The location of the source directories and output directories is configurable in Maker. The default is a standard Maven style layout. Layouts are easy to customise and other layouts can be defined, more on this later.

Ivy files:

Maker uses Ivy for dependency management, so lets add an Ivy settings file to define the settings for Ivy (resolvers and properties, etc). Using your favourite editor, create the file ivysettings.xml in the root of the testwebapp directory.

The above defined the version of Jetty we want to use as a property, turns off transitive dependencies and defines the name of the resolver we want to use (Maven central). This blog isn't meant to be a tutorial on Ivy, but it's fairly simple to get to grips with and you can find documentation here as required. Many concepts are similar to those in Maven, so if you've ever user Maven then you won't feel too out of place.

[A quick aside on transitive dependencies; Whilst transitive dependencies can be used (since Maker uses Ivy and Ivy it supports this feature) a suggestion would be to switch this feature off and define dependencies explicitly - that way there can be no nasty surprises as your projects grow in size and complexity]

Let's now define the ivy.xml file. This defines the dependencies specific to the project module. Generally in a single build there would be one ivysettings.xml and then one ivy.xml per project module (that has external dependencies).

ivysettings.xml and ivy.xml are the default file names Maker will look for when using Ivy to resolve dependencies. Again the name and location of these defaults can be changed.

Ok, so now we have put the files necessary for Ivy in place and the source directory structure, lets have a look at defining a minimal Maker project file.

Creating a Maker project definition:

Create a file called say maker.scala, in the root of the project directory.

$vi maker.scala

import maker.project.Project

import maker.utils.FileUtils._

import maker.Props

lazy val properties : Props = file("Maker.conf")

val webApp = Project(

file("."),

"test-webapp",

props = properties,

webAppDir = Some(file(".", "src/main/webapp"))

)

So this file defines one Maker project, called "test-webapp", as an instance called webApp. Maker projects take a number of arguments but most can be defaulted. At a minimum just the project root directory must be specified. Since we're making this project a single self contained project, the root directory is just the current project directory. The presence of the webAppDir means this application will be treated as a web application, not a regular jar module. In case you're wondering, file(...) is just one of a number of helper function defined in the object maker.utils.FileUtils that make working with Java files a bit less tedious.

A Maker Project is a regular Scala case class defining a build module. All tasks, such as clean, compile and test are invoked on a project instance.

The key parameters to a Maker Project definition are:

root : File - a File representing the root of this project

name : String - a name for the project, can be omitted in which case it defaults to the root file name

props : Props - a key-value property file defining any properties for maker, this may be omitted as Maker does default most properties

webAppDir :Option[File] - this is an optional file, when provided it represents the directory containing the webapp content (WEB-INF etc) and means the project acts like a web application (produces a war packaged artifact, and can be run using the embedded Jetty runner, for example).

Which wires up our test servlet. So that's all the plumbing to put together a trivial test hello-world web application.

Booting the project in Maker:

Let's now boot Maker with the project definition. This is done by running the bin/maker.sh script that you should have from part one of this blog. Unlike in the last part where we used the -b switch to get Maker to build and bootstrap itself from sources, we're now using that build Maker to load a user defined project, with the -p option.

$path/to/maker/bin/maker/.sh -p $path/to/testwebapp/maker/scala

for example, if Maker was a sibling to our test project directory, we might type the following from the root of the test project directory:

$ ../maker/bin/maker.sh -p maker.scala

remembering that the maker.sh script is the entry point for maker and the -p option specifies a Maker project definition file to load - you should be greeted with the following REPL prompt:

From here we can issue Maker commands as required. If you type webApp (the name of our project instance) and the dot (.) and then tab you should get a list of available methods. There are many, but some will jump out as immediately familiar, such as clean, test and so on. For a new project, the first thing to do is invoke Ivy to update any project dependencies:

scala> webApp.update

which should then output some details about the dependencies as they are resolved (via Ivy) and downloaded, ending in some output like:

1 Maker only invokes Ivy when instructed (this can be overridden to be fully automatic before compilation)

2 Maker puts the dependencies in a sub-directory of the project, by default called lib_managed (like SBT) - again this is configurable as you'll see shortly

We can check this. Every Maker project has a layout, by default Maker uses the Maven style layout. Within the layout is a definition of where the managed libraries go (managed libraries being the name of those 'managed' by Ivy, as opposed to user managed libraries that have been provided by hand)

Within a project's layout the field called managedLibDir is a regular java.io.File, so we could use it perhaps to list the files in that directory:

scala> webApp.layout.managedLibDir.listFiles.foreach(println)

./lib_managed/jetty-http-7.6.3.v20120416-sources.jar

./lib_managed/jetty-http-7.6.3.v20120416.jar

./lib_managed/jetty-io-7.6.3.v20120416-sources.jar

./lib_managed/jetty-io-7.6.3.v20120416.jar

./lib_managed/jetty-security-7.6.3.v20120416-sources.jar

./lib_managed/jetty-security-7.6.3.v20120416.jar

./lib_managed/jetty-server-7.6.3.v20120416-sources.jar

./lib_managed/jetty-server-7.6.3.v20120416.jar

./lib_managed/jetty-servlet-7.6.3.v20120416-so

...

These are the dependency files that Maker has fetched for our project (using Ivy) and put in the lib_managed directory of our project. Of course these libraries will be cached in the ~/.ivy2 Ivy cache directory first, so once populated the libraries will only be fetched from the Internet when new dependencies are added. All unmanaged and managed libraries are added to the classpath for compilation and to packaged artifacts (e.g. .wars) as would be expected.

Ok, so far so good. Now we have a loaded project with dependencies resolved, we can compile it:

If we had added some tests then we could run webApp.test to compile and run the tests - but as we've not added any for this simple example we'll skip this step.

The presence of the webAppDir on the project definition triggers certain web application specific features that are not available for standard Java projects. For example, webApp.package will package up a standard format WebARchive (.war) file for us rather than a JavaARchive (.jar).

Running the web-app within Maker:

For web applications, Maker can run the application using an embedded Jetty container using the runJetty task. By default this runs your project Jetty on port 8080, but a new port number can optionally be supplied to the task if required.

Now Jetty is running and Maker is waiting for that process to finish. Since Web Applications don't really terminate from inside the user code, the way to quit this long-running process is to type ctrl-] when we're done. Don't do that just yet, we'll check it works first.

From your favourite browser we should be able to view the web content here.

Ok, so it works. Press ctrl-] in the REPL to quit the Jetty runner and return to the REPL prompt.

So far so good. We've seen how to define a simple project (a web application in this example) and define the necessary minimal Maker project definition necessary to build, test and run it.

Before we wrap up this second bog in the series, it's worth just highlighting the project layout as it's quite likely you might want to customise it for your real-world projects at some stage.

Customising project layouts:

Within Project, the layout field is a ProjectLayout (as mentioned earlier). Maker currently provides two default layouts; 'maven' and 'maker', these are regular values (val fields) in the companion ProjectLayout class. The default maven layout follows the standard maven layout in terms of sources and output classes. Similarly the maker layout is similar but 'flattened' out and is the layout maker uses for its own project.

Any existing layout can be modified, or you're free to create your own instance of ProjectLayout in any way you want to meet your project needs. Being a simple case class we can use the free copy function to make modifications to the immutable class.

Here's a quick example. Say we want a Maven based layout, but with a couple of changes. We want to add another source directory and rename the managed lib directory - this is one way to do that:

Now that we've covered project definitions and using maker to compile and run, a few quick words on properties.

Maker provides defaults for most parameters so that it works out-of-the-box with minimal configuration, but most aspects can be configured either using properties or via optional arguments to the tasks.

In our simple maker project definition you'll notice that we created a Maker.conf based Props class and passed it to the project.

Maker properties:

Various things can be configured using the property file. For example it was previously mentioned that the default Ivy update on compile could be changed. So in this example we could achieve that by adding the property to the Maker.conf file.

$vi Maker.conf

UpdateOnCompile=true

Now when we run any task that compiles (or depends on compilation) the Ivy update task will run first.

Wrapping up:

That about concludes this second blog in the series of byte-sized introductions to Maker. We've still not covered several key aspects, but these should get covered in up and coming blogs of this series. Some of the topics still to be covered are:

Accessing Maker documentation

Continually running tasks - e.g. being able to run tests continually while you develop code to make them pass

Testing - how unit tests can be run and inspected. Re-running failing tests

Packaging - examples of how packaging works and what can be configured to give different packaging options

Documentation - producing scala-doc documentation from Maker projects

Publishing - how to publish Maven style artifacts from Maker to repositories like Nexus.

And, finally - I'd like to cover getting started with Maker as a packaged tool, distributed from binaries - but we're still working on this part. So for now the sequence described in blog part one is a valid way to 'bootstrap' maker straight from sources. Hopefully we'll have a good solution for using Maker from a binary distribution soon and a forthcoming blog will then cover how to install and use it in this way.

1 comment:

Really, these quotes are the holistic approach towards mindfulness. In fact, all of your posts are. Proudly saying I’m getting fruitfulness out of it what you write and share. Thank you so much to both of you.