maven - 如何替换 MANIFEST.MF 中的 Spring Boot 1.4/1.5 主类来打包和使用我的自定义 JarLauncher 类
问题描述
我想扩展org.springframework.boot.loader.JarLauncher
以添加我的专业。
如何从以下位置替换Main-Class
属性MANIFEST.MF
:
Manifest-Version: 1.0
Implementation-Title: my-project
Implementation-Version: 1.0.0.0
Archiver-Version: Plexus Archiver
Built-By: Roberto
Implementation-Vendor-Id: my.project
Spring-Boot-Version: 1.4.7.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: my.project.MyApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.5.2
Build-Jdk: 1.8.0_181
Implementation-URL: http://projects.spring.io/spring-boot/my-project/
并更改为这样的内容(请参阅Main-Class
属性已更改):
Manifest-Version: 1.0
Implementation-Title: my-project
Implementation-Version: 1.0.0.0
Archiver-Version: Plexus Archiver
Built-By: Roberto
Implementation-Vendor-Id: my.project
Spring-Boot-Version: 1.4.7.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: my.project.MyCustomJarLauncher
Start-Class: my.project.MyApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.5.2
Build-Jdk: 1.8.0_181
Implementation-URL: http://projects.spring.io/spring-boot/my-project/
我已经更改了pom.xml
构建部分以添加我的自定义启动器类:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<zip
destfile="${project.build.directory}/${project.build.finalName}.jar"
update="true" compress="store">
<fileset dir="${project.build.directory}/classes"
includes="my/project/MyCustomJarLauncher.class" />
</zip>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
解决方案
我使用 Groovy 脚本解决了:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
import java.io.*
import java.nio.file.*
import java.util.jar.Manifest
import java.net.URI
def uri = new File(project.build.directory, project.build.finalName + '.jar').toURI()
def fs = FileSystems.newFileSystem(URI.create("jar:${uri.toString()}"), ['create':'false'])
try {
def path = fs.getPath("/META-INF/MANIFEST.MF")
def data = Files.readAllBytes(path)
def mf = new Manifest(new ByteArrayInputStream(data))
mf.mainAttributes.putValue("Main-Class", "my.project.MyCustomJarLauncher")
def out = new ByteArrayOutputStream()
mf.write(out);
data = out.toByteArray()
Files.delete(path)
Files.write(path, data)
} finally {
fs.close()
}
</source>
</configuration>
</execution>
</executions>
</plugin>
这个 groovy 脚本执行以下操作:
- 加载 de JAR
- 加载 MANIFEST.MF 字节
- 从该字节创建一个清单类
- 将Main-Class更改为我的自定义实现
- 将此 Manifest 实例写入字节数组
- 从 JAR 中删除 MANIFEST.MF
- 使用新字节在 JAR 中创建一个新的 MANIFEST.MF
因此,每次我运行mvn package
这个 groovy 脚本时,都会在 Spring Boot Maven 插件之后执行。
推荐阅读
- ruby-on-rails - Ruby on Rails - 生产模式 - 搜索不是在寻找内容的第一个字母
- c - C,Xlib,无法获得焦点窗口的名称
- opengl-es - 将skia的opengl状态与外部状态隔离
- automation - 在 Robot Framework 中跳过关键字执行
- netlify-cms - 使用 netlify 提供静态图像?
- leveldb - 有没有办法更改 LevelDB 中的写入目标?
- servicenow - Service Now Integration ,如何通过使用 Rest API 传递用户名来获取用户的事件详细信息
- node.js - nodejs如何将文件夹压缩成zip文件发送给vue客户端
- python - Python:通过 contextlib 关闭打开的 URL
- mikrotik - Mikrotik 带宽速度下降