11.5 创建可执行程序(Executable Jar)

2018-08-14 15:05 更新

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文件来完成我们的示例。 可执行jar(有时候叫做“fat jar”)是一个归档(压缩)文件,包含了编译的类以及为运行您的代码所依赖的所有jar包。

Executable jars and Java
可执行jar和Java

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.
Java没有提供一个标准的方式去加载一个内嵌jar文件(jar文件本身包含在一个jar文件中)。 如果您想发布一个独立的应用程序,这可能是有问题的。

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.

为解决这个问题, 许多开发者使用“uber” jar。 一个“uber”jar打包了所有这个应用程序依赖的类文件到一个单独的归档(压缩)文件中。 这种方法带来的问题是很难看出这个应用程序中有哪些库。 如果在多个jar包中使用了相同文件名(但是拥有不同内容),这将是有问题的。

Spring Boot takes a different approach and lets you actually nest jars directly.
SpringBoot采取了一个不同方法, 可以让您直接内嵌jar包。

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 dependencies section: 为了创建一个可执行Jar, 我们需要添加spring-boot-maven-plugin到我们的 pom.xml。如此, 仅在dependencies节点后面,插入下面几行(代码):

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

说明

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-parentPOM中包含了<executions>配置信息,绑定了repackage goal。 如果不使用这个父POM, 您需要声明您自己的配置信息。 参见插件文档获取更多细节。

Save your pom.xml and run mvn package from the command line, as follows: 保存您的pom.xml,命令行中运行mvn package, 如下所示:

$ 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.0.3.RELEASE:repackage (default) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

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(命令),如下所示:

$ jar tvf target/myproject-0.0.1-SNAPSHOT.jar

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. 在target目录下,您应该还看到了小很多名叫myproject-0.0.1-SNAPSHOT.jar.original的文件。这是源Jar文件,它由Maven在SpringBoot重新打包之前创建的。

To run that application, use the java -jar command, as follows: 为了运行那个应用程序, 请使用java -jar命令, 如下所示:

$ java -jar target/myproject-0.0.1-SNAPSHOT.jar


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.0.3.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.536 seconds (JVM running for 2.864)

As before, to exit the application, press ctrl-c. 就像之前所述, 要退出应用程序,请按下ctrl-c

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号