从基于Spring的Java应用程序创建单个可执行JAR [英] Create a single executable JAR from a Spring Based Java Application

查看:1471
本文介绍了从基于Spring的Java应用程序创建单个可执行JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于弹簧的应用程序,执行组件/程序包扫描,查找某个命名空间中的程序包。该应用程序在Eclipse中运行良好,我想创建一个可执行JAR来部署到我们的各种环境。



我已经尝试过各种各样的方法来使其工作,但唯一的工作是如果我将依赖项包含在JAR之外的文件夹中。这是我迄今为止所尝试的 -


  1. Maven编译为单个jar 这里,使用这种方法单个JAR是以包含的依赖关系作为类创建的。当运行JAR时,使用



    java -jar jarName.jar



    我收到一个错误说明 - 错误:配置问题:无法找到Spring NamespaceHandler的XML模式命名空间 http ://www.springframework.org/schema/context]


  2. 使用导出'as Runnable JAR文件'从Eclipse中选择,然后在库处理部分中选择将所需的库解压缩到生成的JAR中。



    这也构建了一个具有依赖关系的jar作为其中的类。运行jar时,我得到相同的错误:配置问题:找不到用于XML架构命名空间的Spring NamespaceHandler [ http://www.springframework.org/schema/context]


  3. 使用导出'As Runnable JAR文件' ,并在库处理部分中选择包需要的库到生成的JAR



    这将构建一个jar作为JAR(而不是类)中的依赖关系。当我运行这个JAR时,我收到以下错误 -



    无法搜索URL [rsrc:com / company /]下的匹配文件,因为它与目录不对应在文件系统中java.io.FileNotFoundException:URL [rsrc:com / company /]无法解析为绝对文件路径,因为它不驻留在文件系统中:rsrc:com / company / at org.springframework.util.ResourceUtils .getFile(ResourceUtils.java:210



    所以JAR运行,但无法对我要求查找的组件进行扫描。

    / li>
  4. 使用从Eclipse导出As Runnable JAR文件,然后选择将所需库复制到生成的JAR旁边的子文件夹中'



    这将创建一个小的JAR,其旁边有一个文件夹,其中包含所有依赖项作为JAR,当我运行这个一切正常!


所以我不认为这是一个ue与我的代码,似乎有一个包装弹簧的问题,在单个JAR内进行扫描。这是否正确?



有没有人有任何建议,如何构建一个基于弹簧的应用程序进行包/组件扫描到一个可运行的JAR?



ANSWER



我将以下XML添加到我的POM文件中,只使用mvn package ,它创建了一个单一的可执行jar工作。为什么它的工作仍然是一个谜。

 < plugin> 
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-shade-plugin< / artifactId>
< version> 2.3< / version>
<执行>
< execution>
< phase> package< / phase>
< goals>
< goal> shade< / goal>
< / goals>
< configuration>
<变压器>
< transformer implementation =org.apache.maven.plugins.shade.resource.ManifestResourceTransformer>
< mainClass> com.company.project.MainApp< / mainClass>
< / transformer>
< transformer implementation =org.apache.maven.plugins.shade.resource.AppendingTransformer>
< resource> META-INF / spring.handlers< / resource>
< / transformer>
< transformer implementation =org.apache.maven.plugins.shade.resource.AppendingTransformer>
< resource> META-INF / spring.schemas< / resource>
< / transformer>
< / transformers>
< / configuration>
< / execution>
< / executions>
< / plugin>


解决方案

使用 Maven Shade Plugin
这个也可以帮助你。


I have a spring based application that does a component/package scan looking for packages within a certain namespace. The application runs perfectly in Eclipse and I'd like to create a single executable JAR to deploy to our various environments.

I've tried various ways to get this to work but the only thing that's working is if I include the dependencies in a folder outside of the JAR. Here is what I have tried so far -

  1. Maven Compile to a single jar from here , using this approach the single JAR is created with the dependencies included as classes. When running the JAR using -

    "java –jar jarName.jar"

    I get an error stating - "Error: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace http://www.springframework.org/schema/context]"

  2. Using Export ‘As Runnable JAR file’ from Eclipse, and choose ‘Extract required libraries into generated JAR’ in the Library Handling section.

    This also builds one jar with the dependencies as classes within it. When running the jar, I get the same "Error: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]"

  3. Using Export ‘As Runnable JAR file’ from Eclipse, and choose ‘Package required libraries into generated JAR’ in the Library Handling section.

    This builds one jar with the dependencies inside it as JARs (not classes). When I run this JAR I get the following error –

    "Cannot search for matching files underneath URL [rsrc:com/company/] because it does not correspond to a directory in the file system java.io.FileNotFoundException: URL [rsrc:com/company/] cannot be resolved to absolute file path because it does not reside in the file system: rsrc:com/company/ at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:210"

    So the JAR ran but was unable to do a scan for the components I was asking it to find.

  4. Using Export ‘As Runnable JAR file’ from Eclipse, and choose ‘Copy required libraries into a sub-folder next to the generated JAR’ in the Library Handling section.

    This creates a small JAR with a folder next to it which contains all of the dependencies as JARs. When I run this, everything works!

Therefore I don’t think it’s an issue with my code, there seems to be an issue with packaging spring that does a scan inside a single JAR. Would that be correct?

Does anyone have any advice on how to build a spring based application doing a package/component scan into a single runnable JAR?

ANSWER:

I added the following XML to my POM file and just used "mvn package", it created a single executable jar that worked. Why it worked is still a mystery.

      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.company.project.MainApp</mainClass>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

解决方案

use Maven Shade Plugin for create uberjar. This could help you too.

这篇关于从基于Spring的Java应用程序创建单个可执行JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆