建立Clojure开发环境 - 使用IntelliJ Idea和Leiningen

起步Clojure编程.

OS: Ubuntu 14.10

IDEA 14.0.3 Ultimate

安装Leiningen

按照http://leiningen.org/的指南安装好lein

安装La Clojure

安装Idea插件La Clojure。启动Idea,点左上角的File --> Settings  --> Plugins --> 搜"Clojure" , 然后找到La Clojure, 然后Install。

新建项目

在workspace下建立clojure工程。输入模式:  lein new groupId/artifactId。groupId和artifactId和Maven里的概念一致

比如输入 lein new hs.clojure/clojure_sample, 就会依照模版建立一个clojure工程。

导入IDEA

这个工程直接导入IDEA不会被正确识别,因此需要在learn目录下再执行lein pom,生成相应的pom。

pom.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/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>hs.clojure</groupId>

  <artifactId>clojure_sample</artifactId>

  <packaging>jar</packaging>

  <version>0.1.0-SNAPSHOT</version>

  <name>clojure_sample</name>

  <description>FIXME: write description</description>

  <url>http://example.com/FIXME</url>

  <licenses>

    <license>

      <name>Eclipse Public License</name>

      <url>http://www.eclipse.org/legal/epl-v10.html</url>

    </license>

  </licenses>

  <build>

    <sourceDirectory>src</sourceDirectory>

    <testSourceDirectory>test</testSourceDirectory>

    <resources>

      <resource>

        <directory>resources</directory>

      </resource>

    </resources>

    <testResources>

      <testResource>

        <directory>dev-resources</directory>

      </testResource>

      <testResource>

        <directory>resources</directory>

      </testResource>

    </testResources>

    <directory>target</directory>

    <outputDirectory>target/classes</outputDirectory>

    <plugins/>

  </build>

  <repositories>

    <repository>

      <id>central</id>

      <url>https://repo1.maven.org/maven2/</url>

      <snapshots>

        <enabled>false</enabled>

      </snapshots>

      <releases>

        <enabled>true</enabled>

      </releases>

    </repository>

    <repository>

      <id>clojars</id>

      <url>https://clojars.org/repo/</url>

      <snapshots>

        <enabled>true</enabled>

      </snapshots>

      <releases>

        <enabled>true</enabled>

      </releases>

    </repository>

  </repositories>

  <dependencies>

    <dependency>

      <groupId>org.clojure</groupId>

      <artifactId>clojure</artifactId>

      <version>1.6.0</version>

    </dependency>

    <dependency>

      <groupId>org.clojure</groupId>

      <artifactId>tools.nrepl</artifactId>

      <version>0.2.6</version>

      <exclusions>

        <exclusion>

          <groupId>org.clojure</groupId>

          <artifactId>clojure</artifactId>

        </exclusion>

      </exclusions>

      <scope>test</scope>

    </dependency>

    <dependency>

      <groupId>clojure-complete</groupId>

      <artifactId>clojure-complete</artifactId>

      <version>0.2.3</version>

      <exclusions>

        <exclusion>

          <groupId>org.clojure</groupId>

          <artifactId>clojure</artifactId>

        </exclusion>

      </exclusions>

      <scope>test</scope>

    </dependency>

  </dependencies>

</project>



<!-- This file was autogenerated by Leiningen.

  Please do not edit it directly; instead edit project.clj and regenerate it.

  It should not be considered canonical data. For more information see

  https://github.com/technomancy/leiningen -->

然后在IDEA中import project,选择Import project from external model, 然后点Maven,一路点下去。

配置main函数

在打开的IDEA工程中,打开src目录,在hs.clojure包中有clojure_sample.clj文件。

这个clojure文件中,并没有main函数,因此在IDEA中执行run, 什么也不会输出。

把这个文件的内容改一下

(ns hs.clojure.clojure_sample)



(defn -main

  [& args]

  (println "Hello, World!"))

然后在IDEA中最上边的菜单中点Run, 选择Edit Configurations, 选中Run main function in the script namespace,点OK。

在Run菜单中执行Run "learn", 程序会打印出"Hello, World!"。

这时候,如果执行lein run, 会说“No :main namespace specified in project.clj”.

需要修改project.clj

(defproject hs.clojure/clojure_sample "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:main hs.clojure.clojure_sample
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]])

保存后,执行lein run,输出"Hello, world!"

 

Perfect !!!

你可能感兴趣的:(intelliJ idea)