Maven 如何替换MANIFEST.MF中的Spring Boot 1.4/1.5主类来打包和使用我的自定义JarLauncher类
我想扩展org.springframework.boot.loader.JarLauncher来添加我的专门化 如何替换Maven 如何替换MANIFEST.MF中的Spring Boot 1.4/1.5主类来打包和使用我的自定义JarLauncher类,maven,spring-boot,spring-boot-maven-plugin,Maven,Spring Boot,Spring Boot Maven Plugin,我想扩展org.springframework.boot.loader.JarLauncher来添加我的专门化 如何替换MANIFEST.MF中的属性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
MANIFEST.MF
中的属性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: 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
属性已更改):
我已经更改了pom.xml
build部分以添加我的自定义启动器类:
<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>
org.springframework.boot
springbootmaven插件
org.apache.maven.plugins
maven antrun插件
包裹
跑
我使用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>
org.codehaus.gmaven
groovy maven插件
包裹
执行
导入java.io*
导入java.nio.file*
导入java.util.jar.Manifest
导入java.net.URI
def uri=新文件(project.build.directory,project.build.finalName+'.jar').toURI()
def fs=FileSystems.newFileSystem(URI.create(“jar:${URI.toString()}”),['create':'false']
试一试{
def path=fs.getPath(“/META-INF/MANIFEST.MF”)
def data=Files.readAllBytes(路径)
def mf=新清单(新的ByteArrayInputStream(数据))
mf.maintattributes.putValue(“主类”、“my.project.MyCustomJarLauncher”)
def out=new ByteArrayOutputStream()
mf.写(出);
data=out.toByteArray()
文件。删除(路径)
文件。写入(路径、数据)
}最后{
财政司司长(关闭)
}
此groovy脚本执行以下操作:
- 装罐
- 加载MANIFEST.MF字节
- 从该字节创建清单类
- 将主类更改为我的自定义实现
- 将此清单实例写入字节数组
- 从JAR中删除MANIFEST.MF
- 使用新字节在JAR中创建一个新的MANIFEST.MF
mvn包时,这个groovy脚本都是在springbootmaven插件之后执行的
<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>