简体   繁体   中英

How to replace Spring Boot 1.4/1.5 main-class in MANIFEST.MF to package and use my custom JarLauncher class

I want to extend the org.springframework.boot.loader.JarLauncher to add my specialization.

How can I replace the attribute Main-Class in MANIFEST.MF from:

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/

And change to something like this (see the Main-Class attribute has been changed):

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/

I already has changed the pom.xml build section to add my custom launcher class:

<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>

here is maven plugin tag for spring boot, you can change the < start-class> tag, you can find more details here

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <start-class>org.springframework.boot.loader.JarLauncher</start-class>
            </configuration>
        </execution>
    </executions>
</plugin>

I solved using a Groovy Script:

<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>

This groovy script does:

  • Load de JAR
  • Load the MANIFEST.MF bytes
  • Create a Manifest class from that bytes
  • Change the Main-Class to my custom implementation
  • Write this Manifest instance to an array of bytes
  • Delete the MANIFEST.MF from the JAR
  • Create a new MANIFEST.MF in JAR using the new bytes

So, each time I run the mvn package this groovy script is executed after the Spring Boot Maven Plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM