NetBeans+Maven 搭建Scala开发环境
吴嘉禧
2023-12-01
软件以及操作系统要求
下载Netbeans6.9.1 zip版本
下载maven2.2.1<maven3 本人还没有试过>,解压缩,设置路径
下载netbeans scala插件http://sourceforge.net/projects/erlybird/files/nb-scala/6.9v1.1.0/ 下载后安装
详细可以参考:http://wiki.netbeans.org/Scala69
操作系统:UBuntu
注:这里最好使用mac或者linux环境,并且是解压缩版本。windows环境工程大的时候,会有问题。
建立scala工程
文件->新建项目->Maven->Maven项目 两个下一步后,项目名称为scala-test,而后完成
编辑pom.xml文件
添加scala repository
<repositories>
<repository>
<id>scala-tools.org</id>
<name>Scala-tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</repository>
</repositories>
添加dependencies
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.8.1</version>
</dependency>
添加pluginRepositories
<pluginRepositories>
<pluginRepository>
<id>scala-tools.org</id>
<name>Scala-tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</pluginRepository>
</pluginRepositories>
添加build
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
编写代码
新建包scala
添加Object:ListTest
编辑代码
package scala
object ListTest {
val list = List("a","b","c")
def main(args: Array[String]): Unit = {
list match{
case n :: h =>
println("n====" + n)
println("h====" + h.mkString(","))
}
}
}
在maven-scala-plugin中添加测试脚本
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx512m</jvmArg>
<jvmArg>-Xss10m</jvmArg>
</jvmArgs>
<launchers>
<launcher>
<id>list</id>
<mainClass>scala.ListTest</mainClass>
</launcher>
</launchers>
</configuration>
</plugin>
编译运行
cd 到工程根目录下: /home/sujx/NetBeansProjects/scala-test
运行 mvn clean install 第一次因为要下载依赖包,所以会比较慢
测试 mvn scala:run -Dlauncher=list
会在之后的命令行看到如下:
......
[INFO] n====a
[INFO] h====b,c
......
编译成功。
源代码包如下: