【问题标题】:How to package custom class along with Spring Boot loader?如何将自定义类与 Spring Boot 加载器一起打包?
【发布时间】:2018-02-13 14:32:37
【问题描述】:

我正在使用 spring boot maven 插件,它为我创建了一个包含依赖项的 jar。问题是,我需要作为 Windows 服务启动,而 WinSW 需要一个启动类。事情是所有的依赖都隐藏在BOOT-INF/libBOOT-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 boot loader,而是在我处理启动/停止命令的类的帮助下。 我正在使用https://github.com/snicoll-scratches/spring-boot-daemon,但它将所有依赖项复制到lib/,以便Windows 服务可以加载包含自定义类的jar。问题是当所有依赖项都在 jar 中时。

有什么方法可以在 jar 中以引导顺序级别打包我的自定义类?我想只用 maven 来做,不需要任何手动复制,构建应该在 CI 中自动创建。

spring boot maven插件中的自定义布局参数有用吗?

更新:我发现我的 ServiceHelper 课程也需要 org.springframework.boot.*!所以这使它变得更加复杂:(

【问题讨论】:

    标签: java spring-boot spring-boot-maven-plugin


    【解决方案1】:

    分级

    向JAR添加资源是由Spring Boot Gradle Plugin直接提供的

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

    Maven

    Spring Boot Maven 插件 不直接支持此功能。但是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必须配置为&lt;executable&gt;false&lt;/executable&gt;,这样JAR前面就没有embeddedLaunchScript了。

    【讨论】:

      【解决方案2】:

      是的,必须使用 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>
      

      【讨论】:

        猜你喜欢
        • 2020-08-15
        • 2019-06-08
        • 2014-03-19
        • 2021-07-30
        • 2018-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-02
        相关资源
        最近更新 更多