Spring Boot uber jar打包类到root而不是BOOT-INF / classes

时间:2016-11-11 22:39:16

标签: spring maven spring-boot apache-storm maven-assembly-plugin

Hi Spring Boot Experts -

我正在尝试创建一个需要部署到apache风暴集群的spring boot uber jar。但是,问题在于,Storm正在期待jar根目录中的所有类文件,而打包的应用程序文件位于" BOOT-INF / classes"当使用" spring-boot-maven-plugin"打包时。

有没有办法让我的应用程序类直接打包在root下而不是" BOOT-INF / classes"?

我尝试使用" maven-assembly-plugin"使用" spring-boot-maven-plugin"如下所示,它创建了Uber jar,包含来自uber jar根目录的依赖jar中的所有类文件,但是app类仍然是BOOT-INF / classes。

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <excludes>
                <exclude>
                    <groupId>org.apache.storm</groupId>
                    <artifactId>storm-core</artifactId>
                </exclude>
            </excludes>
            <requiresUnpack>
                <dependency>
                    <groupId>com.myorg</groupId>
                    <artifactId>my-app-artifact</artifactId> <!-- This does not help! :( -->
                </dependency>
            </requiresUnpack>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <appendAssemblyId>false</appendAssemblyId>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

2 个答案:

答案 0 :(得分:7)

所以,对于我未来的自我或任何试图找到类似问题答案的人。以下是我在研究中发现的不同事物 -

  1. Storm想要一个可执行的java jar文件
  2. Spring Boot提供定制的jar包装。虽然它使用java jar包装确认,但Spring Boot从BOOT-INF / classes
  3. 加载类

    因此,要使Spring Boot jar在Storm群集上工作,同时表现为Spring Boot,我们需要创建BOOT-INF / classes到jar文件根目录的所有类的副本。

    这可能吗?答案是肯定的。

    使用描述here的方法,我能够创建一个Spring Boot jar,其中BOOT-INF /类被复制到Spring Boot jar的根目录。这种方法需要ant build.xml,常春藤设置和ivy.xml,如下所示。 (免责声明:配置仅在风暴群集上打包时才进行测试)

    因为我们能够在根目录下创建一个被类入侵的Spring Boot Jar -

    我们应该这样做吗? NO

    原因如下 -

    1. Spring强烈建议不要采用这种方法,以免在jar文件和不同版本中具有相同名称的类出现不必要的类覆盖和类版本问题。

    2. Spring Boot Jar包装不是用作依赖jar的格式。 Read the first line here.因此,对于依赖用例,您需要坚持使用普通的旧Java模块。 Spring Boot适用于更多独立的可执行文件或部署在像tomcat这样的容器上。

    3. 祝你好运!

      <强>的build.xml

      <project
              xmlns:ivy="antlib:org.apache.ivy.ant"
              xmlns:spring-boot="antlib:org.springframework.boot.ant"
              name="spring-boot-sample-ant"
              default="build">
      
          <description>
              Sample ANT build script for a Spring Boot executable JAR project. Uses ivy for
              dependency management and spring-boot-antlib for additional tasks. Run with
              '$ ant -lib ivy-2.2.jar spring-boot-antlib.jar' (substitute the location of your
              actual jars). Run with '$ java -jar target/*.jar'.
          </description>
      
          <property name="spring-boot.version" value="1.4.2.RELEASE" />
          <property name="lib.dir" location="${basedir}/target/lib" />
          <property name="start-class" value="com.my.main.class" />
      
          <target name="resolve" description="--> retrieve dependencies with ivy">
              <ivy:retrieve pattern="${lib.dir}/[conf]/[artifact]-[type]-[revision].[ext]" />
          </target>
      
          <target name="classpaths" depends="resolve">
              <path id="compile.classpath">
                  <fileset dir="${lib.dir}/compile" includes="*.jar" />
              </path>
          </target>
      
          <target name="init" depends="classpaths">
              <mkdir dir="target/classes" />
          </target>
      
          <target name="compile" depends="init" description="compile">
              <javac srcdir="src/main/java" destdir="target/classes" classpathref="compile.classpath" />
          </target>
      
          <target name="clean" description="cleans all created files/dirs">
              <delete dir="target" />
          </target>
      
          <target name="build" depends="compile">
              <spring-boot:exejar destfile="target/${ant.project.name}-${spring-boot.version}.jar" classes="target/classes">
                  <spring-boot:lib>
                      <fileset dir="${lib.dir}/runtime" />
                  </spring-boot:lib>
              </spring-boot:exejar>
          </target>
      
          <target name="unjar_dependencies" depends="compile">
              <unzip dest="target/classes">
                  <fileset dir="${lib.dir}/compile">
                      <include name="my-app-common-0.1-SNAPSHOT.jar" />
                  </fileset>
              </unzip>
          </target>
      
          <!-- Manual equivalent of the build target -->
          <target name="manual" depends="compile, unjar_dependencies">
              <jar destfile="target/manual/${ant.project.name}-${spring-boot.version}.jar" compress="false">
                  <mappedresources>
                      <fileset dir="target/classes" />
                      <globmapper from="*" to="BOOT-INF/classes/*"/>
                  </mappedresources>
                  <mappedresources>  <!-- **** this mapped resources block does what I was looking for **** -->
                      <fileset dir="target/classes" />
                      <globmapper from="*" to="/*"/> 
                  </mappedresources>
                  <mappedresources>
                      <fileset dir="src/main/resources" erroronmissingdir="false"/>
                      <globmapper from="*" to="BOOT-INF/classes/*"/>
                  </mappedresources>
                  <mappedresources>
                      <fileset dir="${lib.dir}/runtime" />
                      <globmapper from="*" to="BOOT-INF/lib/*"/>
                  </mappedresources>
                  <zipfileset src="${lib.dir}/loader/spring-boot-loader-jar-${spring-boot.version}.jar" />
                  <manifest>
                      <attribute name="Main-Class" value="org.springframework.boot.loader.JarLauncher" />
                      <attribute name="Start-Class" value="${start-class}" />
                  </manifest>
              </jar>
          </target>
      </project>
      

      <强> ivysettings.xml

      <ivysettings>
          <settings defaultResolver="chain" />
          <resolvers>
              <chain name="chain" returnFirst="true">
                  <!-- NOTE: You should declare only repositories that you need here -->
                  <filesystem name="local" local="true" m2compatible="true">
                      <artifact pattern="${user.home}/.m2/repository/[organisation]/[module]/[revision]/[module]-[revision].[ext]" />
                      <ivy pattern="${user.home}/.m2/repository/[organisation]/[module]/[revision]/[module]-[revision].pom" />
                  </filesystem>
                  <ibiblio name="ibiblio" m2compatible="true" />
                  <ibiblio name="spring-milestones" m2compatible="true" root="http://repo.spring.io/release" />
                  <ibiblio name="spring-milestones" m2compatible="true" root="http://repo.spring.io/milestone" />
                  <ibiblio name="spring-snapshots" m2compatible="true" root="http://repo.spring.io/snapshot" />
              </chain>
          </resolvers>
      </ivysettings>
      

      <强>的ivy.xml

      <ivy-module version="2.0">
          <info organisation="org.springframework.boot" module="spring-boot-sample-ant" />
          <configurations>
              <conf name="compile" description="everything needed to compile this module" />
              <conf name="runtime" extends="compile" description="everything needed to run this module" />
              <conf name="loader" description="Spring Boot loader used when manually building an executable archive" />
          </configurations>
          <dependencies>
              <dependency org="org.springframework.boot" name="spring-boot-starter" rev="${spring-boot.version}" conf="compile">
                      <exclude org="ch.qos.logback" name="logback-classic"/>
              </dependency>
      
              <dependency org="org.springframework.boot" name="spring-boot-loader" rev="${spring-boot.version}" conf="loader->default" />
      
              <dependency org="org.apache.storm" name="storm-core" rev="1.0.2">
                  <exclude org="org.apache.logging.log4j" name="log4j-slf4j-impl"/>
                  <exclude org="org.apache.logging.log4j" name="log4j-core"/>
              </dependency>
      
              <dependency org="com.mycompany" name="app-common" rev="0.1-SNAPSHOT"/>
      
              <dependency org="org.apache.storm" name="storm-kafka" rev="1.0.2"/>
      
              <dependency org="org.apache.kafka" name="kafka_2.10" rev="0.10.1.0"/>
      
              <dependency org="org.apache.kafka" name="kafka_2.10" rev="0.10.1.0"/>
      
              <dependency org="org.apache.httpcomponents" name="httpcomponents-client" rev="4.5.2"/>
      
              <dependency org="org.eclipse.paho" name="org.eclipse.paho.client.mqttv3" rev="1.1.0"/>
      
              <dependency org="com.amazonaws" name="aws-java-sdk-s3" rev="1.11.53"/>
      
              <dependency org="com.jcraft" name="jsch" rev="0.1.54"/>
      
              <dependency org="io.netty" name="netty-handler" rev="3.7.0.Final"/>
      
      
          </dependencies>
      </ivy-module>
      

答案 1 :(得分:3)

  

有没有办法让我的app类直接打包在root下而不是“BOOT-INF / classes”?

是的,你只需要使用Spring Boot 1.3。如果你像这样声明你的父母,请回到你的pom.xml中的maven ...:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>

然后您的类(和其他文件)将被放置在根级别。这是春季靴子的“旧方式”。

在1.4版本中,他们将spring boot jar结构更改为使用BOOT-INF目录。因此,如果您使用<version>1.4.1.RELEASE</version>,那么您的类将在BOOT-INF / classes下。不良副作用是您的配置文件(例如,application.properties,application-myprofile.properties等)也将位于BOOT-INF /类下,即使它们不是Java类。