2010-05-11 82 views
7

目前我的整个工作周期是:斯卡拉简单的虚拟项目

  1. 编辑foo.scala
  2. FSC foo.scala & &阶-cp。 FooMain

但我的项目变得越来越大,我想分裂文件,进行单元测试等 但我阅读SBT文件和做任何需要做的事情得到一个SBT的太懒惰“ Makefile文件”。同样的单元测试(有太多的框架,可供选择)

什么会让我的一天是一个简单的压缩虚拟项目与虚拟单元测试使用sbt。

你知道这样的东西是否存在?

+0

刚刚为Scala项目添加了另一个模板,这次只基于Maven。 – VonC 2010-05-12 08:31:41

+0

我不明白:我的答案的第一部分主张使用sbt,除非它带有一个* template * sbt项目已经准备好去完成测试。那有什么问题?另外,为了完成,我还添加了几个Maven模板项目。 – VonC 2010-05-22 07:07:26

+0

您的答案和项目非常好。 但是我的需求只是通过按照手册使用SBT来实现,因为SBT将为我创建项目和测试,所以不需要什么。 – 2010-05-22 09:46:42

回答

9

那么,你应该使用SBT。您不需要为它编写任何东西:它会创建基本项目所需的所有内容,只会问您项目名称和您将使用的Scala版本。

之后,只需将文件放在正确的位置。查找目录布局,尽管简短回答是src/main/scala中的主要源文件和src/test/scala中的测试源文件。尽管如此,必须创建一个非常小的“生成文件”来获取测试库。

+1

良好的指导:http://code.google.com/p/simple-build-tool/wiki/Setup – 2010-05-19 17:35:51

9

更新答案(2016)

这些天,你有Giter8项目,与SBT的发射版本0.13.13或以上,and its command new结合。

sbt new ... 

原来的答复(2010)

是的,这样的模板工程的基础上,SBT,完全使用Scala的测试中,存在:

看到Get Started With Scala, Sbt And Eclipse及其template project

  • 1)从SBT-控制台模板
 
    % git clone git://github.com/mgutz/sbt-console-template.git your-project 
  • 2)从SBT控制台克隆或下载/提取源
 
     # update dependencies 
     > update 

     # run project 
     > run 

     # test project continuously 
     > ~test 

     # eclipsify 
     > eclipse 

(以下简称 “蚀”部分是可选的,只有在这里如果你想从你的sbt项目生成一个Scala eclipse项目)


另一个Scala的模板项目:

Build a mixed Scala 2.8/Java application from scratch with Maven

它使用下面的模板(这里是zip file with the full Maven-Scala project):

+-scalajavatut/ 
    +-pom.xml 
    +-src/ 
    | +-main/ 
    | | +-java/ 
    | | | +-de/ 
    | | | +-mackaz/ 
    | | |  +-HelloScala.java 
    | | +-scala/ 
    | | +-de/ 
    | |  +-mackaz/ 
    | |  +-App.scala 
    | +-test/ 
    | +-scala/ 
    |  +-de/ 
    |  +-mackaz/ 
    |   +-AppTest.scala 
    |   +-MySpec.scala 

而下面pom.xml

<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> 
    <groupId>de.mackaz</groupId> 
    <artifactId>tutorial1</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <inceptionYear>2008</inceptionYear> 
    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <scala.version>2.8.0-SNAPSHOT</scala.version> 
    </properties> 

    <repositories> 
    <repository> 
     <id>scala-tools.org</id> 
     <name>Scala-Tools Maven2 Repository</name> 
     <url>http://scala-tools.org/repo-releases</url> 
    </repository> 

    <!-- Scala 2.8 Latest --> 
    <repository> 
     <id>scala-tools.org.snapshots</id> 
     <name>Scala Tools Maven2 Repository</name> 
     <url>http://scala-tools.org/repo-snapshots</url> 
     <snapshots /> 
    </repository> 

    </repositories> 

    <pluginRepositories> 
    <pluginRepository> 
     <id>scala-tools.org</id> 
     <name>Scala-Tools Maven2 Repository</name> 
     <url>http://scala-tools.org/repo-releases</url> 
    </pluginRepository> 
    </pluginRepositories> 

    <dependencies> 
    <dependency> 
     <groupId>org.scala-lang</groupId> 
     <artifactId>scala-library</artifactId> 
     <version>${scala.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.4</version> 
     <scope>test</scope> 
    </dependency> 
    <!-- Specs (Behavior Driven Testing through JUnit) --> 
    <dependency> 
    <groupId>org.scala-tools.testing</groupId> 
    <artifactId>specs</artifactId> 
    <version>1.6.1-2.8.0.Beta1-RC1</version> 
    <scope>test</scope> 
    </dependency> 
    </dependencies> 

    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.scala-tools</groupId> 
     <artifactId>maven-scala-plugin</artifactId> 
     <version>2.13.1</version> 
     <executions> 
      <execution> 
      <id>compile</id> 
      <goals><goal>compile</goal></goals> 
      <phase>compile</phase> 
      </execution> 
      <execution> 
      <id>test-compile</id> 
      <goals><goal>testCompile</goal></goals> 
      <phase>test-compile</phase> 
      </execution> 
      <execution> 
      <phase>process-resources</phase> 
      <goals><goal>compile</goal></goals> 
      </execution> 
     </executions> 
     <configuration> 
      <scalaVersion>${scala.version}</scalaVersion> 
      <launchers> 
      <launcher> 
       <id>myLauncher</id> 
       <mainClass>de.mackaz.App</mainClass> 
      </launcher> 
      </launchers> 
      <args> 
      <arg>-target:jvm-1.5</arg> 
      <!-- to support mix java/scala only --> 
      <arg>-make:transitivenocp</arg> 
      <arg>-dependencyfile</arg> 
      <arg>${project.build.directory}/.scala_dependencies</arg> 
      </args> 
     </configuration> 
     </plugin> 

     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-eclipse-plugin</artifactId> 
     <configuration> 
      <downloadSources>true</downloadSources> 
      <buildcommands> 
      <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand> 
      </buildcommands> 
      <additionalProjectnatures> 
      <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature> 
      </additionalProjectnatures> 
      <classpathContainers> 
      <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer> 
      <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer> 
      </classpathContainers> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
    <reporting> 
    <plugins> 
     <plugin> 
     <groupId>org.scala-tools</groupId> 
     <artifactId>maven-scala-plugin</artifactId> 
     <configuration> 
      <scalaVersion>${scala.version}</scalaVersion> 
     </configuration> 
     </plugin> 
    </plugins> 
    </reporting> 
</project> 

你可以运行它:

mvn scala:run 

,并在输出的末尾,你应该看到

[INFO] launcher 'myLauncher' selected => de.mackaz.App 
Hello from Java 
Scala says: Hello from Scala! 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESSFUL 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 7 seconds 
[INFO] Finished at: Wed Mar 24 18:14:22 CET 2010 
[INFO] Final Memory: 14M/33M 
[INFO] ------------------------------------------------------------------------ 

Fanf's blogFrancois Armand礼物:

Maven2 bootstrap pom.xml for Scala with SLF4J and no-commons-logging

<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> 
<groupId>org.test</groupId> 
<artifactId>test</artifactId> 
<packaging>jar</packaging> 

<version>0.1-SNAPSHOT</version> 

<properties> 
    <!-- UTF-8 for everyone --> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 

    <!-- Other general properties --> 
    <slf4j-version>1.6.0</slf4j-version> 
    <logback-version>0.9.20</logback-version> 
    <scala-version>2.8.0.RC2</scala-version> 
    <scala-maven-plugin-version>2.13.1</scala-maven-plugin-version> 
    </properties> 

<description> 
    Starting pom 
</description> 

<repositories> 
    <repository> 
    <id>scala-tools.org</id> 
    <name>Scala-tools Maven2 Repository</name> 
    <url>http://scala-tools.org/repo-releases</url> 
    </repository> 
    <repository> 
    <id>scala-snapshots.org</id> 
    <name>Scala-tools Maven2 Repository snapshots</name> 
    <url>http://scala-tools.org/repo-snapshots</url> 
    </repository> 

    <repository> 
    <id>no-commons-logging</id> 
    <name>No-commons-logging Maven Repository</name> 
    <layout>default</layout> 
    <url>http://no-commons-logging.zapto.org/mvn2</url> 
    <snapshots><enabled>false</enabled></snapshots> 
    </repository> 

</repositories> 

<pluginRepositories> 
    <pluginRepository> 
    <id>scala-tools.org</id> 
    <name>Scala-tools Maven2 Repository</name> 
    <url>http://scala-tools.org/repo-releases</url> 
    <snapshots><enabled>false</enabled></snapshots> 
    </pluginRepository> 
    <pluginRepository> 
    <id>scala-snapshots.org</id> 
    <name>Scala-tools Maven2 Repository snapshots</name> 
    <url>http://scala-tools.org/repo-snapshots</url> 
    </pluginRepository> 
</pluginRepositories> 

<build> 
    <sourceDirectory>src/main/scala</sourceDirectory> 
    <testSourceDirectory>src/test/scala</testSourceDirectory> 
    <plugins> 
    <plugin> 
    <groupId>org.scala-tools</groupId> 
    <artifactId>maven-scala-plugin</artifactId> 
    <version>${scala-maven-plugin-version}</version> 
    <executions> 
    <execution> 
     <goals> 
     <goal>compile</goal> 
     <goal>testCompile</goal> 
     </goals> 
    </execution> 
    </executions> 
    <configuration> 
    <args> 
     <arg>-target:jvm-1.5</arg> 
     <arg>-make:transitivenocp</arg> 
     <arg>-dependencyfile</arg> 
     <arg>${project.build.directory}/.scala_dependencies</arg> 
    </args> 
    <jvmArgs> 
     <jvmArg>-client</jvmArg> 
     <jvmArg>-Xmx1G</jvmArg> 
    </jvmArgs> 
    </configuration> 
    </plugin> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <version>2.1</version> 
    <configuration> 
    <source>1.6</source> 
    </configuration> 
    </plugin> 

    </plugins> 
</build> 

<dependencies> 
    <dependency> 
    <groupId>org.scala-lang</groupId> 
    <artifactId>scala-library</artifactId> 
    <version>${scala-version}</version> 
    </dependency> 
    <dependency> 
    <groupId>joda-time</groupId> 
    <artifactId>joda-time</artifactId> 
    <version>1.6</version> 
    </dependency> 
    <!-- test --> 
    <dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>4.7</version> 
    <scope>test</scope> 
    </dependency> 


    <!-- 
    All the following is related to our will to NOT use Commong-logging 
    --> 
    <!-- use no-commons-logging --> 
    <dependency> 
    <groupId>commons-logging</groupId> 
    <artifactId>commons-logging</artifactId> 
    <version>99.0-does-not-exist</version> 
    </dependency> 
    <!-- no-commons-logging-api, if you need it --> 
    <dependency> 
    <groupId>commons-logging</groupId> 
    <artifactId>commons-logging-api</artifactId> 
    <version>99.0-does-not-exist</version> 
    </dependency> 
    <!-- the slf4j commons-logging replacement --> 
    <dependency> 
    <groupId>org.slf4j</groupId> 
    <artifactId>jcl-over-slf4j</artifactId> 
    <version>${slf4j-version}</version> 
    </dependency> 
    <!-- the other slf4j jars --> 
    <dependency> 
    <groupId>org.slf4j</groupId> 
    <artifactId>slf4j-api</artifactId> 
    <version>${slf4j-version}</version> 
    </dependency> 
    <!-- using slf4j native backend --> 
    <dependency> 
    <groupId>ch.qos.logback</groupId> 
    <artifactId>logback-core</artifactId> 
    <version>${logback-version}</version> 
    </dependency> 
    <dependency> 
    <groupId>ch.qos.logback</groupId> 
    <artifactId>logback-classic</artifactId> 
    <version>${logback-version}</version> 
    </dependency> 
</dependencies> 
</project> 
+0

谢谢。我认为它应该是: git://github.com/mgutz/sbt-console-template.git – 2010-05-11 10:38:21

+0

@Łukasz:是的。我已经修改了答案以反映正确的git回购地址。 – VonC 2010-05-11 10:50:55