龙空技术网

Spring Boot中文参考指南(2.1.6)67.3、打包应用程序

IT荠荠菜 261

前言:

当前咱们对“java打包war”可能比较重视,你们都需要分析一些“java打包war”的相关文章。那么小编在网络上搜集了一些对于“java打包war””的相关知识,希望你们能喜欢,咱们快快来学习一下吧!

上一篇[67、使用 CLI]

下一篇[69、使用settings.xml配置 CLI]

英文原文:
GitHub:

67.1.3、默认导入语句

为了帮助减少 Groovy 代码的大小,会自动包含几个 import 语句。请注意上面的示例如何引用 @Component、 @RestController 和 @RequestMapping,而不需要使用完全限定的名称或 import 语句。

提示:许多 Spring 注解无需使用 import 语句即可工作。尝试运行你的应用程序,以查看在添加导入之前失败的内容。

67.1.4、自动主方法

与等效的 Java 应用程序不同,Groovy 脚本不需要包含 public static void main(String[] args) 方法。系统会自动创建一个 SpringApplication,并将编译后的代码作为源代码。

67.1.5、自定义依赖项管理

默认情况下,CLI 在解析 @Grab 依赖项时使用 spring-boot-dependencies 中声明的依赖项管理。其他依赖项管理(覆盖默认依赖项管理)可以通过使用 @DependencyManagementBom 注解进行配置。注解的值应指定一个或多个 Maven BOMs 的坐标(groupId:artifactId:version)。

例如,考虑以下声明:

@DependencyManagementBom("com.example.custom-bom:1.0.0")

前面的声明在 com/example/custom-versions/1.0.0/ 下的 Maven 存储库中获取 custom-bom-1.0.0.pom。

当你指定多个 BOMs 时,它们将按照你声明它们的顺序应用,如下面示例所示:

@DependencyManagementBom(["com.example.custom-bom:1.0.0",		"com.example.another-bom:1.0.0"])

上述示例表明,another-bom 中的依赖项管理覆盖了 custom-bom 中的依赖项管理。

你可以在任何可以使用 @Grab 的地方使用 @DependencyManagementBom。但是,为了确保依赖项管理的顺序一致,你最多可以在应用程序中使用 @DependencyManagementBom 一次。依赖项管理的一个有用的来源(它是 Spring Boot 依赖项管理的超集)是 Spring IO 平台,你可以将其包含在以下行中:

@DependencyManagementBom('io.spring.platform:platform-bom:1.1.2.RELEASE')

67.2、具有多个源文件的应用程序

你可以将 “shell globbing” 与接受文件输入的所有命令一起使用。这样做可以使用单个目录中的多个文件,如下面示例所示:

$ spring run *.groovy

67.3、打包应用程序

你可以使用 jar 命令将应用程序打包到一个独立的可执行 jar 文件中,如下面示例所示:

$ spring jar my-app.jar *.groovy

生成的 jar 包含编译应用程序生成的类和应用程序的所有依赖项,以便可以使用 java -jar 运行它。jar 文件还包含来自应用程序类路径的条目。你可以通过使用 --include 和 --exclude 来添加和删除 jar 的显式路径。两者都是逗号分隔的,并且都接受“+”和“-”形式的前缀,以表示它们应该从默认值中删除。默认包括如下:

public/**, resources/**, static/**, templates/**, META-INF/**, *

默认排除如下:

.*, repository/**, build/**, target/**, **/*.jar, **/*.groovy

在命令行上键入 spring help jar 以获取更多信息。

67.4、初始化新项目

init 命令允许你使用 start.spring.io 创建新项目,而无需离开 shell,如下面示例所示:

$ spring init --dependencies=web,data-jpa my-projectUsing service at  extracted to '/Users/developer/example/my-project'

前面的示例使用基于 Maven 的项目创建了一个 my-project 目录,该项目使用 spring-boot-starter-web 和 spring-boot-starter-data-jpa。你可以使用 --list 标志列出服务的功能,如下面示例所示:

$ spring init --list=======================================Capabilities of  dependencies:-----------------------actuator - Actuator: Production ready features to help you monitor and manage your application...web - Web: Support for full-stack web development, including Tomcat and spring-webmvcwebsocket - Websocket: Support for WebSocket developmentws - WS: Support for Spring Web ServicesAvailable project types:------------------------gradle-build -  Gradle Config [format:build, build:gradle]gradle-project -  Gradle Project [format:project, build:gradle]maven-build -  Maven POM [format:build, build:maven]maven-project -  Maven Project [format:project, build:maven] (default)...

init 命令支持许多选项。有关更多详细信息,请参见 help 输出。例如,以下命令创建一个使用 Java 8 和 war 打包的 Gradle 项目:

$ spring init --build=gradle --java-version=1.8 --dependencies=websocket --packaging=war sample-app.zipUsing service at  saved to 'sample-app.zip'

67.5、使用嵌入式 Shell

Spring Boot 包括 BASH 和 zsh shell 的命令行完成脚本。如果你不使用这些 shell 中的任何一个 (也许你是 Windows 用户),则可以使用 shell 命令启动集成 shell,如下面示例所示:

$ spring shellSpring Boot (v2.1.6.RELEASE)Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.

从嵌入式 shell 内部,你可以直接运行其他命令:

$ versionSpring CLI v2.1.6.RELEASE

嵌入式 shell 支持 ANSI 颜色输出以及 tab 完成。如果需要运行本机命令,可以使用 ! 前缀。要退出嵌入式 shell,请按 ctrl-c。

67.6、向 CLI 添加扩展

你可以使用 install 命令向 CLI 添加扩展。该命令采用格式为 group:artifact:version 的一组或多组工件坐标,如下面示例所示:

$ spring install com.example:spring-boot-cli-extension:1.0.0.RELEASE

除了安装由你提供的坐标标识的工件之外,还安装了所有工件的依赖项。

要卸载依赖项,请使用 uninstall 命令。与 install 命令一样,它采用 group:artifact:version 格式的一组或多组工件坐标,如下面示例所示:

$ spring uninstall com.example:spring-boot-cli-extension:1.0.0.RELEASE

它卸载由你提供的坐标及其依赖项标识的工件。

要卸载所有其他依赖项,可以使用 --all 选项,如下面示例所示:

$ spring uninstall --all

上一篇[67、使用 CLI]

下一篇[69、使用settings.xml配置 CLI]

标签: #java打包war