11.5 創(chuàng)建可執(zhí)行程序(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. 我們通過(guò)創(chuàng)建一個(gè)可以在生產(chǎn)中運(yùn)行的完全獨(dú)立的可執(zhí)行JAR文件來(lái)完成我們的示例。 可執(zhí)行jar(有時(shí)候叫做“fat jar”)是一個(gè)歸檔(壓縮)文件,包含了編譯的類(lèi)以及為運(yùn)行您的代碼所依賴(lài)的所有jar包。

Executable jars and Java
可執(zhí)行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沒(méi)有提供一個(gè)標(biāo)準(zhǔn)的方式去加載一個(gè)內(nèi)嵌jar文件(jar文件本身包含在一個(gè)jar文件中)。 如果您想發(fā)布一個(gè)獨(dú)立的應(yīng)用程序,這可能是有問(wèn)題的。

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.

為解決這個(gè)問(wèn)題, 許多開(kāi)發(fā)者使用“uber” jar。 一個(gè)“uber”jar打包了所有這個(gè)應(yīng)用程序依賴(lài)的類(lèi)文件到一個(gè)單獨(dú)的歸檔(壓縮)文件中。 這種方法帶來(lái)的問(wèn)題是很難看出這個(gè)應(yīng)用程序中有哪些庫(kù)。 如果在多個(gè)jar包中使用了相同文件名(但是擁有不同內(nèi)容),這將是有問(wèn)題的。

Spring Boot takes a different approach and lets you actually nest jars directly.
SpringBoot采取了一個(gè)不同方法, 可以讓您直接內(nèi)嵌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: 為了創(chuàng)建一個(gè)可執(zhí)行Jar, 我們需要添加spring-boot-maven-plugin到我們的 pom.xml。如此, 僅在dependencies節(jié)點(diǎn)后面,插入下面幾行(代碼):

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. </plugin>
  7. </plugins>
  8. </build>

說(shuō)明

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。 如果不使用這個(gè)父POM, 您需要聲明您自己的配置信息。 參見(jiàn)插件文檔獲取更多細(xì)節(jié)。

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

  1. $ mvn package
  2. [INFO] Scanning for projects...
  3. [INFO]
  4. [INFO] ------------------------------------------------------------------------
  5. [INFO] Building myproject 0.0.1-SNAPSHOT
  6. [INFO] ------------------------------------------------------------------------
  7. [INFO] .... ..
  8. [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---
  9. [INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar
  10. [INFO]
  11. [INFO] --- spring-boot-maven-plugin:2.0.3.RELEASE:repackage (default) @ myproject ---
  12. [INFO] ------------------------------------------------------------------------
  13. [INFO] BUILD SUCCESS
  14. [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目錄, 您會(huì)看見(jiàn)myproject-0.0.1-SNAPSHOT.jar。這個(gè)文件大概10M大小。 如果您想查看(包)內(nèi)容, 您可以使用 jar tvf(命令),如下所示:

  1. $ 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目錄下,您應(yīng)該還看到了小很多名叫myproject-0.0.1-SNAPSHOT.jar.original的文件。這是源Jar文件,它由Maven在SpringBoot重新打包之前創(chuàng)建的。

To run that application, use the java -jar command, as follows: 為了運(yùn)行那個(gè)應(yīng)用程序, 請(qǐng)使用java -jar命令, 如下所示:

  1. $ java -jar target/myproject-0.0.1-SNAPSHOT.jar
  2. . ____ _ __ _ _
  3. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  4. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  5. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  6. ' |____| .__|_| |_|_| |_\__, | / / / /
  7. =========|_|==============|___/=/_/_/_/
  8. :: Spring Boot :: (v2.0.3.RELEASE)
  9. ....... . . .
  10. ....... . . . (log output here)
  11. ....... . . .
  12. ........ Started Example in 2.536 seconds (JVM running for 2.864)

As before, to exit the application, press ctrl-c. 就像之前所述, 要退出應(yīng)用程序,請(qǐng)按下ctrl-c。

以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)