Clojure

The Simplest Pom

(defproject main "org.sonatype:mvn-clojure-test:1.0-SNAPSHOT"
    :dependencies [["org.clojure:clojure-lang:1.1.0-alpha-SNAPSHOT"]
                   ["org.clojure:clojure-contrib:1.0-SNAPSHOT"]]
    :test-dependencies [["org.junit:junit:4.2"]])

This gives you:

  • A basic Mavan POM defining three dependencies, with one of them being scoped as "test", stored in a variable called main (see scripting later on).
  • An implicit plugin configuration for the clojure-maven-plugin for instant "clojure" projects

Artifact References

Polyglot Clojure makes use of artifact references throughout it’s DSL, a reference is just a String containing the combined groupId/artifactId/version of an artifact, eg. "org.sonatype:mvn-clojure-test:1.0-SNAPSHOT" represents:

  • groupId: org.sonatype
  • artifactId: mvn-clojure-test
  • version: 1.0-SNAPSHOT

Properties

Properties are defined as a map against the :properties key:

(defproject main "org.sonatype:mvn-clojure-test:1.0-SNAPSHOT"
    :properties {:author "Mark Derricutt"})

Dependency Configuration

Dependencies are configured by defining a set of dependency keys:

  • :dependencies
  • :compile-dependencies
  • :provided-dependencies
  • :runtime-dependencies
  • :test-dependencies
  • :system-dependencies
  • :import-dependencies

The values for these keys can be either a vector of artifact references (Strings), or a vector of vectors containing an artifact reference and a configuration map.

    :test-dependencies ["org.junit:junit:4.2"]
    :test-dependencies [["org.junit:junit:4.2"]]
    :test-dependencies [["org.junit:junit:4.2" {:classifier "slim"}]]

Plugin Configuration

Configuring plugins in your clojure based POM can be done via the :plugins key, when viewing our minimal POM with the implicit clojure-maven-plugin configuration included we find:

(defproject main "org.sonatype:mvn-clojure-test:1.0-SNAPSHOT"
    :dependencies [["org.clojure:clojure-lang:1.1.0-alpha-SNAPSHOT"]
                   ["org.clojure:clojure-contrib:1.0-SNAPSHOT"]]
    :test-dependencies [["org.junit:junit:4.2"]]
    :plugins [["com.theoryinpractise:clojure-maven-plugin:1.1" {:configuration {:testScript "src/test/clojure/test.clj"}
                                                                :executions [{:id "compile" :phase "compile" :goals ["compile"]}
                                                                             {:id "test" :phase "test" :goals ["test"]}]}]])

Scripting POMs

When we define a project with (defproject…) we the project in a named variable which we can use in order to manipulate the maven Model. By default, the last project defined is the project used by Maven.

; Define the project and store in a variable called 'main'
(defproject main "a:b:c"
    :dependencies [["org.clojure:clojure:1.1.0-alpha-SNAPSHOT"]
                   ["org.clojure:clojure-contrib:1.0-SNAPSHOT"]])

; Use the provided API to add a new dependency
(add-dependency! main "org.testng:testng:5.10")

; Conditionally add a new plugin configuration
(if-not (contains-plugin? main "some.groups:plugin")
        (add-plugin! main ["some.groups:plugin:1.0" {:configuration {:name "value"}}]))

In this short example we first define our project as previously seem, however we then programmatically add a new dependency on TestNG, and finally add a configuration for the mythical "some.groups:plugin:1.0" plugin if there isn’t one already added ( this is exactly how we add the default "com.theoryinpractise:clojure-maven-plugin" plugin.

Scripting API

The scripting API is currently very minimal, and only exposes what’s really used internally:

FunctionDescription
use-projectChanges which project is actually used by maven. By default the last defined project is used.
add-property!Add a name/value property to a model
add-dependency!Adds a new dependency declaration to the POM. This function takes either a string reference to a dependency, or a vector containing the string reference as a map of settings.


Valid settings keys include:

  • :classifier
  • :scope
  • :exclusions (not supported yet)|
add-pluginAdds a new plugin declaration to the POM. Similar to add-dependency, this function takes either a string reference, or a vector containing a string reference and a map of settings.


Valid settings keys include:

  • :configuration - contains a map of configuration items
  • :executions - contains a vector of mas containing execution settings|
contains-plugin?Queries a model for a specific plugin
contains-dependency?Queries a model for a specific dependency

Change Maven with us!