简体   繁体   中英

How to package custom class along with Spring Boot loader?

I am using spring boot maven plugin that creates a jar for me with dependencies in it. The problem is, I need to start as a Windows service and WinSW needs a start class. Thing is all dependecies are hidden in BOOT-INF/lib and classes in BOOT-INF/classes .

my-boot.jar
 \
 \BOOT-INF
 \BOOT-INF\lib (jar dependencies)
 \BOOT-INF\classes (compiled output)
 \org\springframework... (Spring boot loader)
 \com\mejmo\ServiceHelper.class <- here should be my class

I need some way to add my class at the classpath level (in root of jar, along with org.springframework.loader.* which is the boot sequence + class loader for Spring Boot application). The service cannot call spring boot loader directly, but with help of my class which is processing start/stop commands. I am using https://github.com/snicoll-scratches/spring-boot-daemon but it copies all dependencies to lib/ so that Windows service can load the jars including the custom class. The problem is when all dependecies are in jar.

Is there any way how to package my custom class at the level of the boot sequence in jar? I would like to do it solely with maven, without any manual copying, build should be automatically created in CI.

Can custom layout parameter in spring boot maven plugin be helpful?

UPDATE: I found out that I need org.springframework.boot.* for my ServiceHelper class too! So this makes it even more complicated :(

Gradle

Adding resources to the JAR is directly provided by Spring Boot Gradle Plugin :

bootJar {
with copySpec {
    from "$buildDir/classes/java/main/com/mejmo/ServiceHelper.class"
    into 'com/mejmo'
    }
}

Maven

The Spring Boot Maven Plugin does not support this feature directly. But Ant's Zip Task can be used to update the JAR:

Here is an example to extract a projects dependency and add it to the root of the JAR. However, every zipfileset as stated in the documentation can be used:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>addExtractedJarOnRootLevel</id>
            <phase>package</phase>
            <configuration>
                <target>
                    <zip destfile="${project.build.directory}/${project.artifactId}-${project.version}.jar"
                         update="yes" compress="false">
                        <zipfileset src="${GROUP_ID:ARTIFACT_ID:jar}" />
                    </zip>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Notes

The Spring Boot Maven Plugin is bound to Mavens package phase as well. Therefore, the maven-antrun-plugin must be 'below' the spring-boot-maven-plugin in the pom.xml.

Updating the JAR file does not work, if it is an executable JAR: spring-boot-maven-plugin must be configured as <executable>false</executable> , so that no embeddedLaunchScript is added in front of the JAR.

yes, must be used maven-antrun-plugin as a solution to extract external jar file into root level of output jar file.

<properties>
    <pcanbasicdir>D:/libs/JAVA/pcanbasic</pcanbasicdir>
    <pcanbasicjar>pcanbasic.jar</pcanbasicjar>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!--
                <includeSystemScope>true</includeSystemScope>
                -->
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <id>unpack-jar-features</id>
                    <phase>install</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <echo message="unpack jar file : ${pcanbasicdir}/${pcanbasicjar}" />
                            <mkdir dir="${project.build.directory}/pcanbasic"/>
                            <unzip dest="${project.build.directory}/pcanbasic">
                                <fileset dir="${pcanbasicdir}/">
                                    <include name="${pcanbasicjar}" />
                                </fileset>
                            </unzip>
                        </target>
                    </configuration>
                </execution>
                <execution>
                    <id>addExtractedJarOnRootLevel</id>
                    <phase>install</phase>
                    <configuration>
                        <target>
                            <zip destfile="${project.build.directory}/${project.artifactId}-${project.version}.jar"
                                 update="yes" compress="false">
                                <zipfileset src="${pcanbasicdir}/${pcanbasicjar}" />
                            </zip>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

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