Spring Boot的Maven插件(Spring Boot Maven plugin)能夠以Maven的方式為應用提供Spring Boot的支持,即為Spring Boot應用提供了執行Maven操作的可能。
Spring Boot Maven plugin能夠將Spring Boot應用打包為可執行的jar或war文件,然后以通常的方式運行Spring Boot應用。
Spring Boot Maven plugin的最新版本為2017.6.8發布的1.5.4.RELEASE,要求Java 8, Maven 3.2及以后。
Spring Boot Maven plugin的5個Goals
- spring-boot:repackage,默認goal。在mvn package之后,再次打包可執行的jar/war,同時保留mvn package生成的jar/war為.origin
- spring-boot:run,運行Spring Boot應用
- spring-boot:start,在mvn integration-test階段,進行Spring Boot應用生命周期的管理
- spring-boot:stop,在mvn integration-test階段,進行Spring Boot應用生命周期的管理
- spring-boot:build-info,生成Actuator使用的構建信息文件build-info.properties
配置pom.xml文件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.1.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
mvn package spring-boot:repackage說明
Spring Boot Maven plugin的最主要goal就是repackage,其在Maven的package生命周期階段,能夠將mvn package生成的軟件包,再次打包為可執行的軟件包,並將mvn package生成的軟件包重命名為*.original。
基於上述配置,對一個生成Jar軟件包的項目執行如下命令
可以看到生成的兩個jar文件,一個是*.jar,另一個是*.jar.original。在執行上述命令的過程中,Maven首先在package階段打包生成*.jar文件;然后執行spring-boot:repackage重新打包,查找Manifest文件中配置的Main-Class屬性,如下所示:
Manifest-Version: 1.0
Implementation-Title: gs-consuming-rest
Implementation-Version: 0.1.0
Archiver-Version: Plexus Archiver
Built-By: exihaxi
Implementation-Vendor-Id: org.springframework
Spring-Boot-Version: 1.5.3.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.ericsson.ramltest.MyApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.5.0
Build-Jdk: 1.8.0_131
注意,其中的Main-Class屬性值為org.springframework.boot.loader.JarLauncher;
Start-Class屬性值為com.ericsson.ramltest.MyApplication。
其中com.ericsson.ramltest.MyApplication類中定義了main()方法,是程序的入口。
通常,Spring Boot Maven plugin會在打包過程中自動為Manifest文件設置Main-Class屬性,事實上該屬性究竟作用幾何,還可以受Spring Boot Maven plugin的配置屬性layout控制的,示例如下
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.4.RELEASE</version> <configuration> <mainClass>${start-class}</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
layout屬性的值可以如下:
- JAR,即通常的可執行jar
Main-Class: org.springframework.boot.loader.JarLauncher
- WAR,即通常的可執行war,需要的servlet容器依賴位於WEB-INF/lib-provided
Main-Class: org.springframework.boot.loader.warLauncher
- ZIP,即DIR,類似於JAR
Main-Class: org.springframework.boot.loader.PropertiesLauncher
- MODULE,將所有的依賴庫打包(scope為provided的除外),但是不打包Spring Boot的任何Launcher
- NONE,將所有的依賴庫打包,但是不打包Spring Boot的任何Launcher
RepackageMojo的關鍵方法execute:
@Override public void execute() throws MojoExecutionException, MojoFailureException { if (this.project.getPackaging().equals("pom")) { getLog().debug("repackage goal could not be applied to pom project."); return; } if (this.skip) { getLog().debug("skipping repackaging as per configuration."); return; } //得到項目中的原始的jar,就是使用maven-jar-plugin生成的jar File source = this.project.getArtifact().getFile(); //要寫入的目標文件,就是fat jar File target = getTargetFile(); Repackager repackager = new Repackager(source) { //從source中尋找spring boot 應用程序入口的main方法。 @Override protected String findMainMethod(JarFile source) throws IOException { long startTime = System.currentTimeMillis(); try { return super.findMainMethod(source); } finally { long duration = System.currentTimeMillis() - startTime; if (duration > FIND_WARNING_TIMEOUT) { getLog().warn("Searching for the main-class is taking some time, " + "consider using the mainClass configuration " + "parameter"); } } } }; //如果插件中指定了mainClass就直接使用 repackager.setMainClass(this.mainClass); if (this.layout != null) { getLog().info("Layout: " + this.layout); repackager.setLayout(this.layout.layout()); } //尋找項目運行時依賴的jar,過濾后 Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(), getFilters(getAdditionalFilters())); //將Artifact轉化成Libraries Libraries libraries = new ArtifactsLibraries(artifacts, this.requiresUnpack, getLog()); try { LaunchScript launchScript = getLaunchScript(); //進行repackage repackager.repackage(target, libraries, launchScript); } catch (IOException ex) { throw new MojoExecutionException(ex.getMessage(), ex); } if (this.classifier != null) { getLog().info("Attaching archive: " + target + ", with classifier: " + this.classifier); this.projectHelper.attachArtifact(this.project, this.project.getPackaging(), this.classifier, target); } else if (!source.equals(target)) { this.project.getArtifact().setFile(target); getLog().info("Replacing main artifact " + source + " to " + target); }
基本上重要的步驟都有注釋,應該不難理解的。再來看下面,當然也不是重點,看看就行
public void repackage(File destination, Libraries libraries, LaunchScript launchScript) throws IOException { if (destination == null || destination.isDirectory()) { throw new IllegalArgumentException("Invalid destination"); } if (libraries == null) { throw new IllegalArgumentException("Libraries must not be null"); } if (alreadyRepackaged()) { return; } destination = destination.getAbsoluteFile(); File workingSource = this.source; //如果源jar與目標jar的文件路徑及名稱是一致的 if (this.source.equals(destination)) { //將源jar重新命名為原名稱+.original,同時刪除原來的源jar workingSource = new File(this.source.getParentFile(), this.source.getName() + ".original"); workingSource.delete(); renameFile(this.source, workingSource); } destination.delete(); try { //將源jar變成JarFile JarFile jarFileSource = new JarFile(workingSource); try { repackage(jarFileSource, destination, libraries, launchScript); } finally { jarFileSource.close(); } } finally { if (!this.backupSource && !this.source.equals(workingSource)) { deleteFile(workingSource); } } }
這一步所做的是清理工作,如果源jar同目標文件路徑名稱等一致,將源jar重命名,原來的文件刪除。為目標文件騰位置。下面的重點來了。
private void repackage(JarFile sourceJar, File destination, Libraries libraries, LaunchScript launchScript) throws IOException { JarWriter writer = new JarWriter(destination, launchScript); try { final List<Library> unpackLibraries = new ArrayList<Library>(); final List<Library> standardLibraries = new ArrayList<Library>(); libraries.doWithLibraries(new LibraryCallback() { @Override public void library(Library library) throws IOException { File file = library.getFile(); if (isZip(file)) { if (library.isUnpackRequired()) { unpackLibraries.add(library); } else { standardLibraries.add(library); } } } }); //按照規則寫入manifest文件 writer.writeManifest(buildManifest(sourceJar)); Set<String> seen = new HashSet<String>(); writeNestedLibraries(unpackLibraries, seen, writer); //寫入源jar中的內容 writer.writeEntries(sourceJar); //寫入標准的jar,依賴的jar writeNestedLibraries(standardLibraries, seen, writer); if (this.layout.isExecutable()) { //寫入spring boot loader的類 writer.writeLoaderClasses(); } } finally { try { writer.close(); } catch (Exception ex) { // Ignore } }