咋说呢 ,还是使用scala做springboot开发比较流畅一些
按道理来说根据打包模式 maven sbt gradle来说三种方式都可以
maven 是最简单的
gradle 也比较简单,但是我并没有尝试gradle
sbt 其实也还可以 ,你要找到方向基本就直到套路了,经过参考别人的博客,自己搭建了个demo 验证是可以正常使用的,不过maven 版的打jar 使用还是有点问题,我尝试使用多种方式指定主函数还是有问题
两种方式的我都上传到了github上,大家如果想模仿学习 可以git clone 下来泡一泡
https://github.com/mullerhai/scala-springboot-sbt
https://github.com/mullerhai/springboot-scala-maven
另外搭建逻辑 大家可以参考这篇博客 google搜出来的
https://afoo.me/posts/2015-07-21-scala-developers-springboot-guide.html
需要主要的,现在的springboot 的版本是2.0.5 release ,之前都是1.× ,不过大同小异,
springboot 本身是支持 java 8 kotlin groovy,大家也可以尝试一下 kotlin ,kotlin 以后可能会崛起
说一下打包部署
正常来说我们做java 都会打个jar包 部署到生产环境上,
springboot 据说他打包的文件生成方式和普通的 是有差别的,确实,我点击jar 解压看到的和普通有很大差别,所以才会有一个 springboot-maven-plugin
有了这个插件所以 springboot部署成jar包比较简单
但是吧 springboot对sbt 没有特殊支持 也没有这个插件,我耗费三体还是没有实现主就是报这个错
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
那我们说 springboot sbt scala 部署到底如何实现呢
其实在不打jar 包的情况下还是有多种实现的,比如 git 整个项目到生产环境,直接sbt run 就可以启动整个项目
,另外还要说一下,springboot如果不启动web ,比如做定时任务,使用sbt-assembly插件 其实打成jar包也是可以使用的
还有一种就是是使用sbt-native-package插件,这个插件超级强大,简直可以用震惊!!!,他娘的,什么都可以打包,就是打不成jar包,rpm docker image zip tar.gz
macos dmg win exe,这些他都可以
logLevel := Level.Warn
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")
//addSbtPlugin("org.scala-sbt.plugins" % "sbt-onejar" % "0.9.7")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.10")
//addSbtPlugin("org.ensime" % "sbt-ensime" % "1.12.14")
参考
https://stackoverflow.com/questions/45410630/spring-boot-how-can-i-build-a-runnable-jar-with-sbt
I solved the issue by moving to sbt-native-packager
plugins.sbt
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.2.0")
build.sbt
scriptClasspath := Seq("*")
mainClass in Compile := Some("com.x.app.XETL")
enablePlugins(JavaAppPackaging)
Running:
packaging sbt universal:stage
starting the app: target\universal\stage\bin\x.bat
这个会生产一个脚本,直接执行这个脚本就可以,不过有时候也会失败,这个需要在
对于 maven scala springboot 项目来说,要想打成jar包运行,必须包含其中的两个maven 插件,缺一个也不行
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<recompileMode>incremental</recompileMode>
<scalaVersion>${scala.version}</scalaVersion>
<launchers>
<launcher>
<id>app</id>
<mainClass>com.tz.app.tzApplication</mainClass>
<args>
<arg>-deprecation</arg>
</args>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
</launcher>
</launchers>
</configuration>
<dependencies>
<!-- spring热部署-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--<configuration>-->
<!--<fork>true</fork><!– 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart –>-->
<!--</configuration>-->
</plugin>
完整的
<?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>com.xiecheng</groupId>
<artifactId>spring-scala</artifactId>
<version>0.2</version>
<packaging>jar</packaging>
<name>spring-scala</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<start-class>com.tz.app.tzApplication</start-class>
<java.version>1.8</java.version>
<scala.version>2.11.7</scala.version>
<platform-bom.version>2.0.5.RELEASE</platform-bom.version>
<spring-boot-starter-bom.version>2.0.5.RELEASE</spring-boot-starter-bom.version>
<spring-boot.version>2.0.5.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--<dependency>-->
<!--<groupId>com.company.common</groupId>-->
<!--<artifactId>common-files</artifactId>-->
<!--<version>0.0.1-SNAPSHOT</version>-->
<!--</dependency>-->
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<recompileMode>incremental</recompileMode>
<scalaVersion>${scala.version}</scalaVersion>
<launchers>
<launcher>
<id>app</id>
<mainClass>com.tz.app.tzApplication</mainClass>
<args>
<arg>-deprecation</arg>
</args>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
</launcher>
</launchers>
</configuration>
<dependencies>
<!-- spring热部署-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--<configuration>-->
<!--<fork>true</fork><!– 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart –>-->
<!--</configuration>-->
</plugin>
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-dependency-plugin</artifactId>-->
<!--<executions>-->
<!--<execution>-->
<!--<phase>package</phase>-->
<!--<goals>-->
<!--<goal>copy-dependencies</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--<configuration>-->
<!--<includeScope>system</includeScope>-->
<!--</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>
<repositories>
<repository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>${platform-bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
在 IDEA 里面新建 SpringBoot 启动配置,运行后启动成功。
在 POM 目录 执行 mvn spring-boot:run 运行项目,启动成功;
线下环境 通过 java -jar jar_name.jar,运行成功;
用下面命令重新打包试试看
mvn clean package spring-boot:repackage -Dmaven.test.skip
在sbt 中
这两个是罪魁祸首
//libraryDependencies += "org.springframework" % "spring-webflux" % "5.1.1.RELEASE"
////libraryDependencies += "org.springframework.boot" % "spring-boot-starter-actuator" % "2.0.5.RELEASE"
一引用就出问题