SpringBoot官方文档翻译(十):创建一个可执行jar包

11.5 Creating an Executable Jar(运行示例)

1
2
3
4
5
We finish our example by creating a completely self-contained     
executable jar file that we could run in production. Executable
jars (sometimes called “fat jars”) are archives containing your
compiled classes along with all of the jar dependencies that your
code needs to run.

我们通过创建一个完全独立的可执行JAR文件来完成我们的示例,我们可以在生产中运行它。可执行jars(有时候也叫做“重量级jars”)是包含了您自身编译的类和其所依赖的所有jar包的一个整合归档包。

Executable jars and Java

1
2
3
4
5
6
7
8
9
10
11
Java does not provide a standard way to load nested jar files (jar     
files that are themselves contained within a jar). This can be
problematic if you are looking to distribute a self-contained application.
To solve this problem, many developers use “uber” jars. An uber
jar packages all the classes from all the application’s dependencies
into a single archive. The problem with this approach is that it
becomes hard to see which libraries are in your application.
It can also be problematic if the same filename is used (but
with different content) in multiple jars.
Spring Boot takes a different approach and lets you actually
nest jars directly.

Java并不提供一个标准的方式去嵌套jar文件(jar文件本身又包含另一个jar)。这将成为一个阻碍,如果您打算发布一个独立运行的jar应用。
为了解决这个问题,很多开发者使用“uber”包。一个“uber”包根据应用的依赖关系打包了所有的类到一个单独的归档中。采用这种途径打包之后产生的另一个问题是,您无法知道在您的应用中有哪些库。
这还产生一个新的问题就是一些同名的文件会被应用到多个不同的(但包含不同的内容)jars当中。
Spring Boot 采用了一个不同的方式让您可以直接嵌套jars包。

1
2
3
To create an executable jar, we need to add the     
spring-boot-maven-plugin to our pom.xml. To do so,
insert the following lines just below the dependenciessection:

创建一个可执行jar,我们需要增减一个spring-boot-maven-plugin到pom文件中。可以这样做,插入下面的配置行到依赖关系部分下面。

1
2
3
4
5
6
7
8
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
1
2
3
4
The spring-boot-starter-parent POM includes <executions>     
configuration to bind the repackage goal. If you do not
use the parent POM, you need to declare this configuration
yourself. See the plugin documentation for details.

spring-boot-starter-parent的pom文件包含配置,可以绑定执行repackage对象。如果您不使用父POM,您需要自己声明这个配置。您可以在plugin的文档中得到更为详细的信息(https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/maven-plugin/usage.html)

1
Save your pom.xml and run mvn package from the command line, as follows:

保存您的pom.xml,在命令行中运行mvn package,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ mvn package

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] .... ..
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---
[INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.0.BUILD-SNAPSHOT:repackage (default) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

1
2
3
4
If you look in the target directory, you should see     
myproject-0.0.1-SNAPSHOT.jar. The file should be around
10 MB in size. If you want to peek inside, you can use 
jar tvf, as follows:

如果您在target目录下查看,你将会看到myproject-0.0.1-SNAPSHOT.jar。这个文件大概10M左右的样子。如果您想一窥内部构造,您可以使用 jar tvf命令,如下:

1
$ jar tvf target/myproject-0.0.1-SNAPSHOT.jar
1
2
3
4
You should also see a much smaller file named myproject-0.0.1-    
SNAPSHOT.jar.original in the target directory. This is the original
jar file that Maven created before it was repackaged by Spring Boot.
To run that application, use the java -jar command, as follows:

您应该同样的会看到一个比较小的名称为myproject-0.0.1-SNAPSHOT.jar.original的文件在target目录下。这是maven在Spring Boot进行repackaged之前创建的原版的jar文件。运行这个应用,使用java -jar命令,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.BUILD-SNAPSHOT)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.536 seconds (JVM running for 2.864)
1
As before, to exit the application, press ctrl-c.

跟之前一样,如果想退出应用,按ctrl+c

12. What to Read Next(接下去阅读什么)

1
2
3
4
5
Hopefully, this section provided some of the Spring Boot basics     
and got you on your way to writing your own applications. If you
are a task-oriented type of developer, you might want to jump over to spring.io and check out some of the getting started guides that
solve specific “How do I do that with Spring?” problems. We also
have Spring Boot-specific “How-to” reference documentation.

希望,本节提供了一些Spring Boot的基础知识和能帮助您以您自己的方式编写自己的应用程序。
如果您是一个面向任务的开发人员类型,您可能想跳到spring.io并查看一些入门指南解决具体的“我怎样用Spring做这件事”的问题。 我们也具有特定于Spring Boot的“操作方法”参考文档。

1
2
3
The Spring Boot repository also has a bunch of samples you can run.     
The samples are independent of the rest of the code (that is, you
do not need to build the rest to run or use the samples).

Spring Boot存储库还有一堆你可以运行的样本。样本与代码的其余部分无关(即,您
无需要构建其余的代码去运行或使用样本)

1
2
3
Otherwise, the next logical step is to read Part III, “Using Spring     
Boot”. If you are really impatient, you could also jump ahead and
read about Spring Boot features.

除此之外,下一个合乎逻辑的步骤是阅读第三部分,“使用Spring Boot”。如果您真的不耐烦,您可以跳过此章节,阅读Spring Boot的功能部分。

分享到