...
 
Commits (166)
.*
*~
.DS_Store
target
Welcome to the clojure-maven-plugin plugin for Apache Maven 2.
## Available goals
* clojure:compile
* clojure:test
* clojure:run
* clojure:repl
* clojure:swank
* clojure:nailgun
## Compiling clojure sources
To use this plugin and start compiling clojure code as part of your maven build, add the following:
<plugins>
<plugin>
<groupId>com.theoryinpractise</groupId>
<artifactId>clojure-maven-plugin</artifactId>
<version>1.3.1</version>
</plugin>
</plugins>
Without any additional configuration, the clojure-maven-plugin will compile any
namespaces in ./src/main/clojure/*.clj and ./src/test/clojure/*.clj.
To change, or add additional source directories you can add the following configuration:
<configuration>
<sourceDirectories>
<sourceDirectory>src/main/clojure</sourceDirectory>
</sourceDirectories>
<testSourceDirectories>
<testSourceDirectory>src/test/clojure</testSourceDirectory>
</testSourceDirectories>
</configuration>
NOTE: The plugin will prepend the project's ${basedir} before each source/testSource directory specified.
The plugin provides a clojure:run goal for run a predefined clojure script defined by:
<configuration>
<script>src/test/clojure/com/jobsheet/jetty.clj</script>
</configuration>
whilst you could easily launch your tests from the clojure:run goal, the clojure:test goal is more appropriate,
without any additional configuration the plugin will generate and execute the following temporary clojure
"test launcher" script:
(require 'one.require.for.each.discovered.namespace)
(use 'clojure.test)
(when-not *compile-files*
(let [results (atom [])]
(let [report-orig report]
(binding [report (fn [x] (report-orig x)
(swap! results conj (:type x)))]
(run-tests 'one.require.for.each.discovered.namespace)))
(shutdown-agents)
(System/exit (if (empty? (filter {:fail :error} @results)) 0 -1))))
The generated script requires any discovered *test* namespaces, runs all the tests, and fails the build when any FAIL or
ERROR cases are found.
If you require different test behaviour, you can provide your own test script with the following configuration:
<configuration>
<testScript>src/test/clojure/com/jobsheet/test.clj</testScript>
</configuration>
If you wish to limit or filter out namespaces during your compile/test, simply add a `<namespaces>` or `<testNamespaces>`
configuration section:
<configuration>
<namespaces>
<namespace>com.foo</namespace>
<namespace>net.*</namespace>
<namespace>!testing.*</namespace>
</namespaces>
</configuration>
The namespace declaration is actually a regex match against discovered namespaces, and can also be
prepended with an ! to filter the matching namespace.
If you wish to further limit test/compile usage to only the namespaces you define, you can enable this with the
configuration block:
<configuration>
<compileDeclaredNamespaceOnly>true</compileDeclaredNamespaceOnly>
<testDeclaredNamespaceOnly>true</testDeclaredNamespaceOnly>
</configuration>
If you want to provide additional arguments to all spawned java/clojure processes, add a
`<clojureOptions>` configuration element. In addition, a `<warnOnReflection>` configuration
element is available as a shortcut to specifying the system property that controls whether or
not the AOT clojure compilation process emits reflection warnings:
<configuration>
<clojureOptions>-Xmx512m</clojureOptions>
<warnOnReflection>true</warnOnReflection>
</configuration>
The plugin can also copy source files to the output directory, filtered using the namespace mechanism
that is used to control compilation. If you want to copy all compiled source files to the output:
<configuration>
<copyAllCompiledNamespaces>true</copyAllCompiledNamespaces>
<configuration>
If you want to copy only a subset:
<configuration>
<copiedNamespaces>
<namespace>com.foo</namespace>
<namespace>!com.foo.private.*</namespace>
</copiedNamespaces>
<copyDeclaredNamespaceOnly>true</copyDeclaredNamespaceOnly>
<configuration>
If you want to do no compilation at all, but copy all source files:
<configuration>
<copyDeclaredNamespaceOnly>true</copyDeclaredNamespaceOnly>
<namespaces>
<namespace>!.*</namespace>
</namespaces>
<compileDeclaredNamespaceOnly>true</compileDeclaredNamespaceOnly>
<configuration>
Note that it will only copy clojure source files, which must a) end in .clj and b) contain a namespace declaration.
Enjoy.
## clojure:run, clojure:repl, clojure:swank and clojure:nailgun goals
clojure-maven-plugin supports four goals intended to make it easier
to developers to run clojure shells in the context of maven projects.
This means that all dependencies in a project's runtime and test scopes
will be automatically added to the classpath and available for experimentation.
By default these goals will use the test classpath, if you wish to only use the
compile classpath/dependencies, you can disable this with:
<configuration>
<runWithTests>false</runWithTests>
</configuration>
or by running maven with:
-Dclojure.runwith.test=false
<table>
<tr>
<th>Goal</th>
<th>Description</th>
</tr>
<tr>
<td>clojure:repl</td>
<td>
Starts an interactive clojure REPL right on the command line. An
initialisation script can be specified in the pom using the
replScript configuration element.
</td>
</tr>
<tr>
<td>clojure:swank</td>
<td>
Starts a Swank server that accepts connections on port 4005
(can be changed using the `-Dclojure.swank.port=X`option). You can
connect to this server from emacs with `M-x slime-connect`.
</td>
</tr>
<tr>
<td>clojure:nailgun</td>
<td>
Starts a nailgun server that accepts connections on port 2113
(can be changed using the `-Dclojure.nailgun.port=X`option). You can
connect to this server from vim using vimclojure
(http://kotka.de/projects/clojure/vimclojure.html).
</td>
</tr>
<tr>
<td>clojure:run</td>
<td>
Runs a clojure script specified in the pom using the &lt;script&gt; and/or &lt;scripts&gt;
configuration element.
</td>
</tr>
</table>
### Dependencies
In order to run clojure:repl, clojure:swank or clojure:nailgun, your project
needs to have a recent (1.0 or later) version of clojure as a dependency in
pom.xml.
#### JLine
If JLine is detected in the classpath, it will be used to provide the
clojure:repl goal with history, tab completion, etc. A simple way of
enabling this is to put the following in your pom.xml:
<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
<version>0.9.94</version>
</dependency>
#### Swank
The clojure:swank goal requires a recent version of swank-clojure as a
dependency. Unfortunatly, this library is currently not available in
the central maven repository, and has to be downloaded and installed
manually:
1. Download `http://cloud.github.com/downloads/jochu/swank-clojure/swank-clojure-1.0-SNAPSHOT-distribution.zip`
2. Unzip the distribution and extract the swank-clojure-1.0-SNAPSHOT.jar file within.
3. Run the following command to install the jar file to your local repository:
mvn install:install-file -DgroupId=com.codestuffs.clojure -DartifactId=swank-clojure -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/jarfile
4. Put the following in your pom.xml
<dependency>
<groupId>com.codestuffs.clojure</groupId>
<artifactId>swank-clojure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
#### Nailgun
The clojure:nailgun goal requires a recent version of vimclojure as a
dependency. Unfortunatly, this library is currently not available in
the central maven repository, and has to be downloaded and installed
manually:
1. Download vimclojure source code from `http://cloud.github.com/downloads/jochu/swank-clojure/swank-clojure-1.0-SNAPSHOT-distribution.zip`.
2. Follow the README to compile and install vimclojure.
3. Locate vimclojure.jar and run the following command to install it to your local repository (replace X.X.X with your version of vimclojure):
mvn install:install-file -DgroupId=de.kotka -DartifactId=vimclojure -Dversion=X.X.X -Dpackaging=jar -Dfile=/path/to/jarfile
4. Put the following in your pom.xml (replace X.X.X with your version of vimclojure)
<dependency>
<groupId>de.kotka</groupId>
<artifactId>vimclojure</artifactId>
<version>X.X.X</version>
</dependency>
### Configuration
The following options that can be configured as system properties:
<table>
<tr>
<th>Property</th>
<th>Default value</th>
<th>Description</th>
</tr>
<tr>
<td>clojure.nailgun.port</td>
<td>4005</td>
<td>
Only applicable for the <code>clojure:nailgun</code> goal.
The port number that the Nailgun server should listen to.
</td>
</tr>
<tr>
<td>clojure.swank.port</td>
<td>4005</td>
<td>
Only applicable for the <code>clojure:swank</code> goal.
The port number that the Swank server should listen to.
</td>
</tr>
<tr>
<td>clojure.swank.protocolVersion</td>
<td>2009-09-14</td>
<td>
Only applicable for the <code>clojure:swank</code> goal.
Specifies the version of the swank protocol.
</td>
</tr>
</table>
### Support
Join the discussion mailing list at:
http://groups.google.com/group/clojure-maven-plugin
This diff is collapsed.
<?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">
<modelVersion>4.0.0</modelVersion>
<name>clojure-maven-plugin Maven Mojo</name>
<groupId>com.theoryinpractise</groupId>
<artifactId>clojure-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.3.3</version>
<description>Maven plugin for compiling clojure source files</description>
<url>http://github.com/talios/clojure-maven-plugin</url>
<scm>
<connection>scm:git:git://github.com/talios/clojure-maven-plugin.git</connection>
<developerConnection>scm:git:git://github.com/talios/clojure-maven-plugin.git</developerConnection>
<url>http://github.com/talios/clojure-maven-plugin</url>
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<developers>
<developer>
<id>talios</id>
<name>Mark Derricutt</name>
<email>mark@talios.com</email>
<url>http://www.talios.com</url>
</developer>
</developers>
<contributors>
<contributor>
<name>Tim Dysinger</name>
<email>tim -on- dysinger.net</email>
</contributor>
<contributor>
<name>Stuart Sierra</name>
<email>mail@stuartsierra.com</email>
</contributor>
<contributor>
<name>Scott Fleckenstein</name>
<email>nullstyle@gmail.com</email>
</contributor>
<contributor>
<name>Jukka Zitting</name>
<email>jukka.zitting@gmail.com</email>
</contributor>
<contributor>
<name>Chas Emerick</name>
<email>cemerick@snowtide.com</email>
</contributor>
</contributors>
<licenses>
<license>
<name>Eclipse Public License</name>
</license>
</licenses>
<distributionManagement>
<repository>
<id>sonatype-staging</id>
<name>oss.sonatype.org Staging Repository</name>
<url>http://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
<snapshotRepository>
<id>github-snapshots</id>
<name>oss.sonatype.org github Release Repository Snapshot Repository</name>
<url>http://oss.sonatype.org/content/repositories/github-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<profiles>
<profile>
<id>release</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>debian</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-toolchain</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>2.0.9</version>
</dependency>
</dependencies>
</project>
/*
* Copyright (c) Mark Derricutt 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import org.apache.maven.plugin.MojoExecutionException;
/**
* @goal compile
* @phase compile
* @requiresDependencyResolution compile
*/
public class ClojureCompilerMojo extends AbstractClojureCompilerMojo {
public void execute() throws MojoExecutionException {
callClojureWith(
getSourceDirectories(SourceDirectory.COMPILE),
outputDirectory, classpathElements, "clojure.lang.Compile",
discoverNamespaces());
copyNamespaceSourceFilesToOutput(outputDirectory, discoverNamespacesToCopy());
}
}
/*
* Copyright (c) Mark Derricutt 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import org.apache.maven.plugin.MojoExecutionException;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @goal gendoc
* @phase package
* @requiresDependencyResolution test
*/
public class ClojureGenDocMojo extends AbstractClojureCompilerMojo {
/**
* Should we compile all namespaces or only those defined?
*
* @parameter default-value="false"
*/
private boolean generateTestDocumentation;
public void execute() throws MojoExecutionException {
File genDocClj;
File docsDir;
try {
genDocClj = File.createTempFile("generate-docs", ".clj");
if (!outputDirectory.getParentFile().exists()) {
outputDirectory.getParentFile().mkdir();
}
docsDir = new File(outputDirectory.getParentFile(), "clojure");
getLog().debug("Creating documentation directory " + docsDir.getPath());
docsDir.mkdir();
System.out.println(docsDir.getPath() + " exists " + docsDir.exists());
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage());
}
StringBuilder sb = new StringBuilder();
sb.append("(use 'clojure.contrib.gen-html-docs)\n");
sb.append("(generate-documentation-to-file \n");
int count = 0;
sb.append(" \"").append(docsDir.getPath().replace('\\', '/')).append("/index.html\"\n");
sb.append(" [");
final NamespaceInFile[] allNamespaces = new NamespaceDiscovery(getLog(), compileDeclaredNamespaceOnly)
.discoverNamespacesIn(namespaces, getSourceDirectories(SourceDirectory.COMPILE, SourceDirectory.TEST));
for (NamespaceInFile namespace : allNamespaces) {
sb.append("'").append(namespace.getName());
if (count++ < allNamespaces.length - 1) {
sb.append("\n ");
}
}
sb.append("])\n");
try {
final PrintWriter pw = new PrintWriter(genDocClj);
pw.print(sb.toString());
pw.close();
getLog().info("Generating docs to " + docsDir.getCanonicalPath() + " with " + genDocClj.getPath());
getLog().debug(sb.toString());
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage());
}
callClojureWith(
getSourceDirectories(SourceDirectory.COMPILE, SourceDirectory.TEST),
outputDirectory, testClasspathElements, "clojure.main",
new String[]{genDocClj.getPath()});
}
}
/*
* Copyright (c) Mark Derricutt 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import org.apache.maven.plugin.MojoExecutionException;
/**
* Mojo to start a clojure REPL running vimclojure's nailgun.
*
* @goal nailgun
* @execute phase="compile"
* @requiresDependencyResolution compile
*/
public class ClojureNailgunMojo extends AbstractClojureCompilerMojo {
/**
* The clojure script to preceding the switch to the repl
*
* @parameter
*/
private String replScript;
/**
* @parameter expression="${clojure.nailgun.port}" default-value="2113"
*/
protected int port;
public void execute() throws MojoExecutionException {
String[] args = new String[]{Integer.toString(port)};
callClojureWith(getSourceDirectories(SourceDirectory.TEST, SourceDirectory.COMPILE),
outputDirectory,
getRunWithClasspathElements(),
"com.martiansoftware.nailgun.NGServer", args);
}
}
/*
* Copyright (c) Mark Derricutt 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import org.apache.maven.plugin.MojoExecutionException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Mojo to start a clojure repl
*
* @goal repl
* @execute phase="compile"
* @requiresDependencyResolution compile
*/
public class ClojureReplMojo extends AbstractClojureCompilerMojo {
/**
* The clojure script to preceding the switch to the repl
*
* @parameter
*/
private String replScript;
private static final Pattern JLINE = Pattern.compile("^.*/jline-[^/]+.jar$");
boolean isJLineAvailable(List<String> elements) {
if (elements != null) {
for (String e : elements) {
Matcher m = JLINE.matcher(e);
if (m.matches())
return true;
}
}
return false;
}
public void execute() throws MojoExecutionException {
List<String> args = new ArrayList<String>();
String mainClass = "clojure.main";
if (isJLineAvailable(classpathElements)) {
getLog().info("Enabling JLine support");
args.add("clojure.main");
mainClass = "jline.ConsoleRunner";
}
if (replScript != null && new File(replScript).exists()) {
args.add("-i");
args.add(replScript);
args.add("-r");
}
callClojureWith(
getSourceDirectories(SourceDirectory.TEST, SourceDirectory.COMPILE),
outputDirectory, getRunWithClasspathElements(), mainClass,
args.toArray(new String[args.size()]));
}
}
/*
* Copyright (c) Mark Derricutt 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import org.apache.maven.plugin.MojoExecutionException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @goal run
* @requiresDependencyResolution compile
*/
public class ClojureRunMojo extends AbstractClojureCompilerMojo {
/**
* The main clojure script to run
*
* @parameter expression="${clojure.script}"
* @required
*/
private String script;
/**
* Additional scripts to run
*
* @parameter
*/
private String[] scripts;
/**
* args specified on the command line.
*
* @parameter expression="${clojure.args}"
*/
private String args;
/**
* Returns either a path to a temp file that loads all of the provided scripts,
* or simply returns the singular <code>script</code> String (which therefore allows
* for @ classpath-loading paths to be passed in as a script).
*
* If multiple scripts are defined, they must all exist; otherwise an exception is thrown.
*/
private static String mergeScripts (String script, String[] scripts) throws MojoExecutionException {
if (script == null || script.trim().equals("")) {
throw new MojoExecutionException("<script> is undefined");
}
if (scripts == null) {
return script;
} else if (scripts.length == 0) {
throw new MojoExecutionException("<scripts> is defined but has no <script> entries");
}
List<String> paths = new ArrayList<String>();
paths.add(script);
paths.addAll(Arrays.asList(scripts));
for (String scriptFile : paths) {
if (scriptFile == null || scriptFile.trim().equals("")) {
throw new MojoExecutionException("<script> entry cannot be empty");
}
if (!(new File(scriptFile).exists())) {
throw new MojoExecutionException(scriptFile + " cannot be found");
}
}
try {
File testFile = File.createTempFile("run", ".clj");
final FileWriter writer = new FileWriter(testFile);
for (String scriptFile : paths) {
writer.write("(load-file \"" + scriptFile + "\")");
writer.write(System.getProperty("line.separator"));
}
writer.close();
return testFile.getPath();
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
public void execute() throws MojoExecutionException {
String path = mergeScripts(script, scripts);
try {
List<String> clojureArguments = new ArrayList<String>();
clojureArguments.add(path);
if (args != null) {
clojureArguments.addAll(Arrays.asList(args.split(" ")));
}
getLog().debug("Running clojure:run against " + path);
callClojureWith(
getSourceDirectories(SourceDirectory.COMPILE),
outputDirectory, getRunWithClasspathElements(), "clojure.main",
clojureArguments.toArray(new String[clojureArguments.size()]));
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
}
/*
* Copyright (c) Mark Derricutt 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import org.apache.maven.plugin.MojoExecutionException;
import java.io.*;
import static org.apache.commons.io.IOUtils.copy;
/**
* @goal test
* @phase test
* @requiresDependencyResolution test
*/
public class ClojureRunTestMojo extends AbstractClojureCompilerMojo {
/**
* Flag to allow test compiliation to be skipped.
*
* @parameter expression="${maven.test.skip}" default-value="false"
* @noinspection UnusedDeclaration
*/
private boolean skip;
/**
* The main clojure script to run
*
* @parameter
*/
private String testScript;
public void execute() throws MojoExecutionException {
if (skip) {
getLog().info("Test execution is skipped");
} else {
final File[] testSourceDirectories = getSourceDirectories(SourceDirectory.TEST);
final File[] allSourceDirectories = getSourceDirectories(SourceDirectory.TEST, SourceDirectory.COMPILE);
if (testScript == null || "".equals(testScript) || !(new File(testScript).exists())) {
// Generate test script
try {
NamespaceInFile[] ns = new NamespaceDiscovery(getLog(), testDeclaredNamespaceOnly).discoverNamespacesIn(testNamespaces, testSourceDirectories);
File testFile = File.createTempFile("run-test", ".clj");
final PrintWriter writer = new PrintWriter(new FileWriter(testFile));
for (NamespaceInFile namespace : ns) {
writer.println("(require '" + namespace.getName() + ")");
}
StringWriter testCljWriter = new StringWriter();
copy(ClojureRunTestMojo.class.getResourceAsStream("/default_test_script.clj"), testCljWriter);
StringBuilder runTestLine = new StringBuilder();
runTestLine.append("(run-tests");
for (NamespaceInFile namespace : ns) {
runTestLine.append(" '" + namespace.getName());
}
runTestLine.append(")");
String testClj = testCljWriter.toString().replace("(run-tests)", runTestLine.toString());
writer.println(testClj);
writer.close();
testScript = testFile.getPath();
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
// throw new MojoExecutionException("testScript is empty or does not exist!");
}
getLog().debug("Running clojure:test against " + testScript);
callClojureWith(allSourceDirectories, outputDirectory, testClasspathElements, "clojure.main", new String[]{testScript});
}
}
}
/*
* Copyright (c) Mark Derricutt 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* @goal swank
* @execute phase="compile"
* @requiresDependencyResolution test
*/
public class ClojureSwankMojo extends AbstractClojureCompilerMojo {
/**
* The clojure script to preceding the switch to the repl
*
* @parameter
*/
private String replScript;
/**
* @parameter expression="${clojure.swank.port}" default-value="4005"
*/
protected int port;
/**
* @parameter expression="${clojure.swank.protocolVersion}"
* default-value="2009-09-14"
*/
protected String protocolVersion;
public void execute() throws MojoExecutionException {
File tempFile;
try {
tempFile = File.createTempFile("runswank", ".clj");
IOUtils.copy(this.getClass().getClassLoader().getResourceAsStream("runswank.clj"),new FileOutputStream(tempFile));
} catch (IOException e) {
throw new MojoExecutionException("unable to load runswank.clj into temporary file",e);
}
List<String> args = new ArrayList<String>();
if (replScript != null && new File(replScript).exists()) {
args.add("-i");
args.add(replScript);
}
args.add(tempFile.getAbsolutePath());
callClojureWith(
getSourceDirectories(SourceDirectory.TEST, SourceDirectory.COMPILE),
outputDirectory, getRunWithClasspathElements(), "clojure.main",
args.toArray(new String[args.size()]));
}
}
/*
* Copyright (c) Mark Derricutt 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NamespaceDiscovery {
private final Pattern nsPattern = Pattern.compile("^\\s*\\(ns(\\s.*|$)");
private Log log;
private boolean compileDeclaredNamespaceOnly;
public NamespaceDiscovery(Log log, boolean compileDeclaredNamespaceOnly) {
this.log = log;
this.compileDeclaredNamespaceOnly = compileDeclaredNamespaceOnly;
}
/**
* Discover namespaces in a list of source directories filtered by a list of namespace regexs
*
* @param namespaceFilterRegexs
* @param paths
* @return
* @throws FileNotFoundException
*/
public NamespaceInFile[] discoverNamespacesIn(String[] namespaceFilterRegexs, File... paths) throws MojoExecutionException {
if (namespaceFilterRegexs == null || namespaceFilterRegexs.length == 0) {
namespaceFilterRegexs = new String[]{".*"};
}
List<NamespaceInFile> namespaces = new ArrayList<NamespaceInFile>();
for (NamespaceInFile namespace : discoverNamespacesInPath(paths)) {
boolean toAdd = !compileDeclaredNamespaceOnly;
for (String regex : namespaceFilterRegexs) {
if (regex.startsWith("!")) {
// exclude regex
if (Pattern.compile("^" + regex.substring(1)).matcher(namespace.getName()).matches()) {
toAdd = false;
break;
}
} else {
// include regex
if (Pattern.compile("^" + regex).matcher(namespace.getName()).matches()) {
toAdd = true;
}
}
}
if (toAdd) {
namespaces.add(namespace);
} else if (log.isDebugEnabled()) {
log.debug("Filtered namespace " + namespace.getName() + " from clojure build.");
}
}
return namespaces.toArray(new NamespaceInFile[]{});
}
public List<NamespaceInFile> discoverNamespacesInPath(File... paths) throws MojoExecutionException {
List<NamespaceInFile> namespaces = new ArrayList<NamespaceInFile>();
for (File path : paths) {
namespaces.addAll(discoverNamespacesIn(path, path));
}
return namespaces;
}
public List<NamespaceInFile> discoverNamespacesIn(File basePath, File scanPath) throws MojoExecutionException {
List<NamespaceInFile> namespaces = new ArrayList<NamespaceInFile>();
File[] files = scanPath.listFiles();
if (files != null && files.length != 0) {
for (File file : files) {
if (!file.getName().startsWith(".")) {
log.debug("Searching " + file.getPath() + " for clojure namespaces");
if (file.isDirectory()) {
namespaces.addAll(discoverNamespacesIn(basePath, file));
} else if (file.getName().endsWith(".clj")) {
namespaces.addAll(findNamespaceInFile(basePath, file));
}
}
}
}
return namespaces;
}
private List<NamespaceInFile>
findNamespaceInFile(File path, File file) throws MojoExecutionException {
List<NamespaceInFile> namespaces = new ArrayList<NamespaceInFile>();
Scanner scanner = null;
try {
scanner = new Scanner(file);
scanner.useDelimiter("\n");
while (scanner.hasNext()) {
String line = scanner.next();
Matcher matcher = nsPattern.matcher(line);
if (matcher.find()) {
String ns = file.getPath();
ns = ns.substring(
path.getPath().length() + 1,
ns.length() - ".clj".length());
ns = ns.replace(File.separatorChar, '.');
ns = ns.replace('_', '-');
log.debug("Found namespace " + ns + " in file " + file.getPath());
namespaces.add(new NamespaceInFile(ns, file));
}
}
} catch (FileNotFoundException e) {
throw new MojoExecutionException(e.getMessage());
}
return namespaces;
}
}
/*
* Copyright (c) Antony Blakey 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import java.io.File;
public class NamespaceInFile {
private String namespace;
private File sourceFile;
public NamespaceInFile(String namespace, File sourceFile) {
this.namespace = namespace;
this.sourceFile = sourceFile;
}
public String getName() {
return namespace;
}
public String getFilename() {
return namespace.replace('.', File.separatorChar).replace('-', '_') + ".clj";
}
public File getSourceFile() {
return sourceFile;
}
}
/*
* Copyright (c) Mark Derricutt 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import org.apache.maven.plugin.MojoExecutionException;
import java.io.File;
/**
* @goal testCompile
* @phase test-compile
* @requiresDependencyResolution test
*/
public class TestClojureCompilerMojo extends AbstractClojureCompilerMojo {
/**
* Flag to allow test compiliation to be skipped.
*
* @parameter expression="${maven.test.skip}" default-value="false"
* @noinspection UnusedDeclaration
*/
private boolean skip;
public void execute() throws MojoExecutionException {
if (skip) {
getLog().info("Test compilation is skipped");
} else {
final File[] testSourceDirectories = getSourceDirectories(SourceDirectory.TEST);
callClojureWith(testSourceDirectories, testOutputDirectory, testClasspathElements, "clojure.lang.Compile",
new NamespaceDiscovery(getLog(), testDeclaredNamespaceOnly).discoverNamespacesIn(testNamespaces, testSourceDirectories));
}
}
}
(use 'clojure.test)
(when-not *compile-files*
(let [results (atom [])]
(let [report-orig report]
(binding [report (fn [x] (report-orig x)
(swap! results conj (:type x)))]
(run-tests)))
(shutdown-agents)
(System/exit (if (empty? (filter #{:fail :error} @results)) 0 -1))))
(require 'swank.swank)
(swank.swank/ignore-protocol-version "2009-09-14")
(do
(swank.swank/start-server (. (java.io.File/createTempFile "swank" ".port") getAbsolutePath) :dont-close true :port 4005))
\ No newline at end of file
/*
* Copyright (c) Mark Derricutt 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import junit.framework.TestCase;
import java.util.Arrays;
import java.util.Collections;
public class ClojureReplMojoTest extends TestCase {
public void testJLineAvailable() throws Exception {
ClojureReplMojo mojo = new ClojureReplMojo();
assertTrue(mojo.isJLineAvailable(Arrays.asList("test/jline-0.9.94.jar")));
assertTrue(mojo.isJLineAvailable(Arrays.asList("test/jline-0.9.95-SNAPSHOT.jar")));
assertTrue(mojo.isJLineAvailable(Arrays.asList("test/test-0.1.jar", "test/jline-0.9.94.jar")));
assertFalse(mojo.isJLineAvailable(Arrays.asList("test/test-0.1.jar")));
assertFalse(mojo.isJLineAvailable(Collections.<String>emptyList()));
assertFalse(mojo.isJLineAvailable(null));
}
}
/*
* Copyright (c) Mark Derricutt 2010.
*
* The use and distribution terms for this software are covered by the Eclipse Public License 1.0
* (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html
* at the root of this distribution.
*
* By using this software in any fashion, you are agreeing to be bound by the terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
package com.theoryinpractise.clojure;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.junit.Test;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@RunWith(Theories.class)
public class NamespaceDiscoveryTest {
@Test
public void testNamespaceDiscovery() throws MojoExecutionException {
final NamespaceDiscovery namespaceDiscovery = new NamespaceDiscovery(mock(Log.class), true);
List<String> namespaces = new ArrayList<String> () {{
for (NamespaceInFile s: namespaceDiscovery.discoverNamespacesInPath(new File("src/test/resources"))) {
System.out.println(s.getName());
add(s.getName());
}
}};
assertThat(namespaces)
.isNotNull()
.isNotEmpty()
.hasSize(4)
.contains("test1")
.contains("test2")
.contains("test.test3")
.contains("nsmeta");
}
public static class NamespaceData {
public String[] namespaces;
public File[] sourceDirectories;
public boolean compileDeclaredNamespaceOnly;
public int expectedSize;
public NamespaceData(String[] namespaces, File[] sourceDirectories, boolean compileDeclaredNamespaceOnly, int expectedSize) {
this.namespaces = namespaces;
this.sourceDirectories = sourceDirectories;
this.compileDeclaredNamespaceOnly = compileDeclaredNamespaceOnly;
this.expectedSize = expectedSize;
}
}
@DataPoint
public static NamespaceData ns1 = new NamespaceData(new String[]{"test.*"}, new File[]{new File("src/test/resources")}, true, 3);
@DataPoint
public static NamespaceData ns2 = new NamespaceData(new String[]{"!test\\..*"}, new File[]{new File("src/test/resources")}, false, 3);
@DataPoint
public static NamespaceData ns3 = new NamespaceData(new String[]{"test1"}, new File[]{new File("src/test/resources")}, true, 1);
@DataPoint
public static NamespaceData ns4 = new NamespaceData(new String[]{"test\\..*"}, new File[]{new File("src/test/resources")}, true, 1);
@DataPoint
public static NamespaceData ns5 = new NamespaceData(new String[]{"!test\\..*", "test.*"}, new File[]{new File("src/test/resources")}, true, 2);
@Theory
public void testNamespaceFiltering(NamespaceData ns) throws MojoExecutionException {
NamespaceDiscovery namespaceDiscovery = new NamespaceDiscovery(mock(Log.class), ns.compileDeclaredNamespaceOnly);
assertThat(namespaceDiscovery.discoverNamespacesIn(ns.namespaces, ns.sourceDirectories))
.describedAs("Discovered Namespaces")
.isNotNull()
.isNotEmpty()
.hasSize(ns.expectedSize);
}
}
(ns #^{:doc "This is my namespace."}
nsmeta)
(ns test.test3)
(defn hello [] (println "hello"))
(ns test1)
(defn test [] (println "test"))
;; (ns your-ns
;; (:use [clojure.http.resourcefully :as resourcefully]))
(ns test2
)
(defn hello [] (println "hello"))