Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

WARNING: When this was copied over from assembla, I couldn't find a way to make pieces of the code bold. Some of this may not make sense.

Creating a Project

Create a new project directory structure like this:

No Format
myproject
myproject/src
myproject/src/main
myproject/src/main/clojure
myproject/src/test
myproject/src/test/clojure

In the top-level myproject directory, create a file named pom.xml, and copy the template below. Replace the bold-face parts with your own domain name, project name, URL, and description.

Code Block
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>myproject</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <url>http://example.com/</url>
  <description>My new project</description>
  <name>${artifactId}</name>
  <licenses>
    <license>
      <name>Eclipse Public License 1.0</name>
      <url>http://opensource.org/licenses/eclipse-1.0.php</url>
      <distribution>repo</distribution>
    </license>
  </licenses>
  <dependencies>
    <dependency>
      <groupId>org.clojure</groupId>
      <artifactId>clojure</artifactId>
      <version>1.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.clojure</groupId>
      <artifactId>clojure-contrib</artifactId>
      <version>1.1.0</version>
    </dependency>
    <dependency>
      <groupId>swank-clojure</groupId>
      <artifactId>swank-clojure</artifactId>
      <version>1.2.1</version>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <id>clojars</id>
      <url>http://clojars.org/repo</url>
    </repository>
    <repository>
      <id>clojure-snapshots</id>
      <url>http://build.clojure.org/snapshots</url>
      <releases>
        <enabled>false</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>clojure-releases</id>
      <url>http://build.clojure.org/releases</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
  <build>
    <plugins>
      <plugin>
        <groupId>com.theoryinpractise</groupId>
        <artifactId>clojure-maven-plugin</artifactId>
        <version>1.3.2</version>
        <executions>
          <execution>
            <id>compile-clojure</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>test-clojure</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

You can also replace the license name/url with a different license if you wish.

Running Clojure

To start a Clojure REPL (read-eval-print-loop) in your new project, type:

mvn clojure:repl

To start a SWANK server (for connecting to Emacs SLIME), type:

mvn clojure:swank

Then run M-x slime-connect from within Emacs

Adding Your Own Code

Add your .clj source files under src/main/clojure. When creating file names from namespace names, periods become directory separators and hyphens become underscores. So a namespace like

(ns com.example.foo-library)

Would go in the file src/main/clojure/com/example/foo_library.clj

Follow the same convention for test source code (using clojure.test), adding files under src/test/clojure.

Adding Dependencies

Open-source Java libraries can be found on search engines such as MVNrepository and Jarvana. Many Clojure libraries can be found on Clojars.

Once you have found a dependency, add its group/artifact/version information to the <dependencies> section of pom.xml.

For example, to add a dependency on Compojure, add the following code to pom.xml:

Code Block
xml
<dependencies>
  <!-- ... other dependencies ... -->
  <dependency>
    <groupId>compojure</groupId>
    <artifactId>compojure</artifactId>
    <version>0.4.0-SNAPSHOT</version>
  </dependency>
  <!-- ... -->
</dependencies>

Note: Be aware that different libraries may be written for different version of Clojure. Check the library documentation to make sure you have the right version.

Using Different Clojure Versions

To use the 1.2.0 development snapshots of Clojure and clojure-contrib, change this block in pom.xml:

Code Block
xml
    <dependency>
      <groupId>org.clojure</groupId>
      <artifactId>clojure</artifactId>
      <version>1.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.clojure</groupId>
      <artifactId>clojure-contrib</artifactId>
      <version>1.1.0</version>
    </dependency>

To this:

Code Block
xml
    <dependency>
      <groupId>org.clojure</groupId>
      <artifactId>clojure</artifactId>
      <version>1.2.0-master-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.clojure</groupId>
      <artifactId>clojure-contrib</artifactId>
      <version>1.2.0-SNAPSHOT</version>
    </dependency>