接着上面的
的基础之上,来介绍使用IDEA+Maven来开发Scala,这样将是我们的最终开发方式,也是我们正式进入使用Scala开发的开端。1.IntelliJ IDEA配置Maven
如何配置Maven,真得不想多说,很基础得事情,参考上面得帖子就可以了。
2.创建Scala Maven项目
2.1通过maven模板创建项目
IDEA菜单-File-new Model-选择Maven-选择Scala模板-点击Next-填写项目名称就完成了
项目结构
2.2不通过模板来创建
那就直接创建一个maven项目,不同的就是目录结构和pom
目录结构不同你可以自己建目录,通过File-project structure来修改哪个是source目录。
下面我贴出pom,如果你是自己新建的maven项目,可以自己对照修改
主要是build和java不一样,所以只贴build
<build> <defaultGoal>install</defaultGoal> <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory> <plugins> <!-- the Maven compiler plugin will compile Java source files --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- the Maven Scala plugin will compile Scala source files --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.2</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <!-- configure the eclipse plugin to generate eclipse project descriptors for a Scala project --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.10</version> <configuration> <projectnatures> <projectnature>org.scala-ide.sdt.core.scalanature</projectnature> <projectnature>org.eclipse.jdt.core.javanature</projectnature> </projectnatures> <buildcommands> <buildcommand>org.scala-ide.sdt.core.scalabuilder</buildcommand> </buildcommands> <classpathContainers> <classpathContainer>org.scala-ide.sdt.launching.SCALA_CONTAINER</classpathContainer> <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer> </classpathContainers> <excludes> <exclude>org.scala-lang:scala-library</exclude> <exclude>org.scala-lang:scala-compiler</exclude> </excludes> <sourceIncludes> <sourceInclude>**/*.scala</sourceInclude> <sourceInclude>**/*.java</sourceInclude> </sourceIncludes> </configuration> </plugin> <!-- allows the route to be run via 'mvn exec:java' --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <configuration> <mainClass>test.MyRouteMain</mainClass> </configuration> </plugin> </plugins> </build>
3.打包jar并运行
在IDEA下方窗口找到Maven project窗口打开-找到新建的项目-双击Install
然后再项目中的target目录就可以找到生成的jar包。
运行jar就很简单了,有java环境就能运行jar
windows在装好java后双击久能运行
linux配置好java环境运行jar的命令:java -jar itxw.jar