我如何从 spring 批处理中创建和 运行 Jar 文件

How can i create and run Jar file from spring batch

我有一个 spring 批处理 应用程序,其中包含 两个作业 eclipse 中的应用程序工作正确地 但是当我创建 jar 和 运行 它时,它不起作用我需要使用 CommandLineJobRunner.

来执行它

我尝试使用多种方式创建一个 jar:


我使用 maven 命令 mvn clean installmvn clean package,在这种情况下,如果我 运行 jar使用 ->

java -jar org.springframework.batch.core.launch.support.CommandLineJobRunner jar 始终执行所有作业。

在其他情况下,如果我尝试使用 java -cp 我得到:

错误:无法找到或加载主程序 class org.springframework.batch.core.launch.support.CommandLineJobRunner 原因:java.lang.ClassNotFoundException:org.springframework.batch.core.launch.support.CommandLineJobRunner


使用ecilpse导出jar文件

在eclipse export -> jar -> 运行neableJar中,在本例中:

如果我使用 java -cp 我得到 FileNotFoundException application.properties,但是这个文件在资源中。

如果我使用 java -jar 我得到 no main manifest attribute.

在eclipse export -> jar -> Jar文件中,本例中:

如果我使用 java -cp 我得到 -> 错误:无法找到或加载主 class org.springframework.batch.core.launch.support.CommandLineJobRunner 原因:java.lang.ClassNotFoundException:org.springframework.batch.core.launch.support.CommandLineJobRunner

如果我使用 java -jar 我得到 no main manifest attribute.

我不知道该尝试什么。 感谢和抱歉我的英语,不是我的母语 languaje

您至少有三个选择:

  • 在类路径中添加 spring 批处理和所有其他依赖项
  • 使用 maven shade plugin 将所有依赖项隐藏在同一个 jar 中
  • 使用 Spring 引导,它会自动为您完成,请参阅 getting started guide.
  • 中的示例

它现在可以工作了,问题是插件,正如 Mahmoud 所说,必须使用 maven 插件,但另一方面我有提供范围的 db2 依赖项,在构建和测试项目期间使用提供的 Maven 依赖项范围.它们也是 运行 所必需的,但不应导出,因为依赖项将在 运行 时间提供,例如,由 servlet 容器或应用程序服务器提供。