项目场景:
scala语言编写的maven项目打jar包运行
问题描述
最近在做项目的时候需要把项目部署到docker上面运行,java、编写的maven项目打成jar包再定义入口函数之后就可以运行了。但是如果是scala语言写的怎么办呢?
原因分析:
springboot项目或者maven项目在打成jar包运行的时候会先找主程序入口,要不然机器不知道整个项目该从哪里运行。因此scala编写的项目也需要定义一个主函数。如上图假如我要运行StreamRecommender.scala这个程序。scala程序也是放在jvm虚拟机里面运行的,编译完成后也会生产.class文件。那么由此可知想把scala代码打成jar包运行也是可以的。
解决方案:
假如以下依赖就行了。该两个依赖提供了maven项目打包和scala代码编译打成jar包运行的依赖。
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.qinyu.realtime.StreamingRecommender</mainClass>
<!--定义主程序运行入口-->
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>