eclipse 从基于 Spring 的 Java 应用程序创建单个可执行 JAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28193970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:22:34  来源:igfitidea点击:

Create a single executable JAR from a Spring Based Java Application

javaeclipsespringmavenjar

提问by P456678

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.

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

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 -

我已经尝试了各种方法来让它工作,但唯一有效的是我是否将依赖项包含在 JAR 之外的文件夹中。这是我迄今为止尝试过的-

  1. Maven Compile to a single jarfrom 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!

  1. Maven这里编译为单个 jar,使用这种方法创建单个 JAR,其中包含作为类的依赖项。使用 - 运行 JAR 时

    “java –jar jarName.jar”

    我收到一条错误消息 - “错误:配置问题:无法为 XML 架构命名空间http://www.springframework.org/schema/context]找到 Spring NamespaceHandler ”

  2. 使用从 Eclipse导出“As Runnable JAR 文件”,并在“库处理”部分中选择“将所需的库提取到生成的 JAR 中”

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

  3. 使用从 Eclipse导出“As Runnable JAR 文件”,并在“库处理”部分中选择“将所需的库打包到生成的 JAR 中”

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

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

    因此 JAR 运行但无法扫描我要求它查找的组件。

  4. 使用从 Eclipse导出“As Runnable JAR 文件”,并在“库处理”部分中选择“将所需的库复制到生成的 JAR 旁边的子文件夹中”

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

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?

因此,我认为这不是我的代码的问题,包装 spring 在单个 JAR 中进行扫描似乎存在问题。那会是正确的吗?

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

有没有人对如何构建一个基于 spring 的应用程序进行包/组件扫描到单个可运行的 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.

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

      <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>

采纳答案by Cuzz

use Maven Shade Pluginfor create uberjar. Thiscould help you too.

使用 Maven Shade Plugin创建 uberjar。 也可以帮助你。

回答by Zivilyn Bane

I just had the very same problem than you today, with doing exactly the same steps :)

我今天遇到的问题和你完全一样,步骤完全一样:)

So thanks for your message, it makes me save some time and besides, i just want to quickly answer to your "Why it worked is still a mystery." sentence :

所以感谢您的留言,这让我节省了一些时间,此外,我只想快速回答您的“为什么它有效仍然是个谜”。句子 :

"This is because I have several Spring Jar in my dependencies. Some of the spring jars contain meta info files with the same name. To avoid that some meta files are overridden you have to merge it"

“这是因为我的依赖项中有几个 Spring Jar。一些 spring jar 包含同名的元信息文件。为了避免某些元文件被覆盖,你必须合并它”

So this is why you need to add those two transformers to the maven snippet given into the Maven Shade Plugin documentation :

所以这就是为什么您需要将这两个转换器添加到 Maven Shade Plugin 文档中给出的 Maven 代码段中的原因:

<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

source : http://robert-reiz.com/2011/11/14/832/

来源:http: //robert-reiz.com/2011/11/14/832/

Hope it helps for understanding ;)

希望它有助于理解;)

回答by Farzan Skt

try this one in eclipse: in the menu choose File-> Export-> Runnable JAR file

在 Eclipse 中试试这个:在菜单中选择File-> Export-> Runnable JAR file

in the specification window:(after filling other blanks properly) Library handling: Select the second one

在规范窗口中:(正确填写其他空白后)库处理选择第二个

and Finish

完成

now try the following command in cmd or terminal: java -jar [your project's name]

现在在 cmd 或终端中尝试以下命令: java -jar [您的项目名称]

Hope it helps

希望能帮助到你

回答by M Kaweepatt Churcharoen

You can use your option 1 Maven Compile to a single jarwith additional goal parameters.

您可以使用选项 1 Maven Compile to a single jarwith additional goal parameters。

mvn clean package spring-boot:repackage

It will create the standalone jar file with Spring autoload functionality.

它将创建具有 Spring 自动加载功能的独立 jar 文件。

enter image description here

在此处输入图片说明