首页 > 解决方案 > 如何替换 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>

标签: mavenspring-bootspring-boot-maven-plugin

解决方案


我使用 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 插件之后执行。


推荐阅读