[英]How to package custom class along with Spring Boot loader?
我正在使用 spring boot maven 插件为我创建一个带有依赖项的 jar。 问题是,我需要作为 Windows 服务启动,而 WinSW 需要一个启动类。 事情是所有的依赖都隐藏在BOOT-INF/lib
和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
我需要某种方式在类路径级别添加我的类(在 jar 的根目录中,以及org.springframework.loader.*
这是 Spring Boot 应用程序的启动序列 + 类加载器)。 该服务不能直接调用 spring 引导加载程序,但在我处理启动/停止命令的类的帮助下。 我正在使用https://github.com/snicol-scratches/spring-boot-daemon但它将所有依赖项复制到lib/
以便 Windows 服务可以加载包含自定义类的 jars。 问题是当所有依赖项都在 jar 中时。
有什么办法可以在jar中的启动序列级别打包我的自定义类? 我想只用 maven 来做,没有任何手动复制,构建应该在 CI 中自动创建。
spring boot maven插件中的自定义布局参数有帮助吗?
更新:我发现我的 ServiceHelper 类也需要org.springframework.boot.*
! 所以这让它变得更加复杂:(
向 JAR 添加资源由Spring Boot Gradle Plugin直接提供:
bootJar {
with copySpec {
from "$buildDir/classes/java/main/com/mejmo/ServiceHelper.class"
into 'com/mejmo'
}
}
Spring Boot Maven Plugin不直接支持这个特性。 但是可以使用 Ant 的Zip Task来更新 JAR:
这是一个提取项目依赖项并将其添加到 JAR 根目录的示例。 但是,可以使用文档中所述的每个zipfileset
:
<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>
Spring Boot Maven 插件也绑定到 Mavens package
阶段。 因此, maven-antrun-plugin
必须在 pom.xml 中spring-boot-maven-plugin
“下方”。
更新JAR文件不起作用,如果是可执行JAR: spring-boot-maven-plugin
必须配置为<executable>false</executable>
,这样JAR前面就不会添加embeddedLaunchScript
。
是的,必须使用 maven-antrun-plugin 作为将外部 jar 文件提取到输出 jar 文件的根级别的解决方案。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.