更改Spring Boot项目以继承自定义依赖项管理

Cho*_*hop 14 java spring maven spring-batch spring-boot

我有一个小的Spring Boot应用程序,我必须适应使用自定义父级.谷歌找到了很多关于如何迁移 Spring Boot 的例子,但我不知道 Spring Boot 迁移(我几乎不知道开始).

原因

简而言之,这个应用程序是作为一种概念验证而开发的,开发人员选择了最先进的技术.

现在,这个应用程序将继续生存,我们需要将它集成到我们的框架中.因此,作为一个Maven项目,它应该继承我们的一个普通父项,尽管我的理解是Spring Boot项目应该继承自Spring Boot的父级POM.

[编辑]我错了:可以在没有父POM的情况下使用Spring Boot.因此,删除Spring Boot不是义务,但我们的父POM包含自己的版本管理,这使我必须覆盖启动器中包含的几个版本.

为什么我们需要继承父母

我们在所有项目中都使用了一个企业框架.为了简化操作,我们在父POM中管理许多框架的版本.这些版本与我们的小批量项目中spring-boot-dependencies的版本冲突的一些示例:

  • Spring框架(内部:3.2.4; Spring Boot 1.2.2:4.1.5)
  • Spring Batch(ih:2.2.0; sb:3.0.3)
  • 速度(ih:1.5; sb:1.7)
  • Junit(ih:4.11; sb:4.12)
  • ...(我们管理大约90个版本)

我显然无权升级这些版本.请记住,这个框架用于许多项目,任何改变都可能产生严重和不受控制的后果.

附加背景

目标是获得一点Spring批处理应用程序.它必须从命令行获取一些输入,并12在执行失败时返回退出代码.下面是发射器的核心,但我愿意接受任何更好的方法.

我们的批处理启动器类

如果完全放弃Spring Boot,我需要调整这个启动器类,但是,主要用于XML Spring上下文,我发现这有点困难.

// ...
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("my.config.package")
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        // ...
        SpringApplication app = new SpringApplication(Application.class);
        app.setWebEnvironment(false);
        final Map<String, Object> maps = new HashMap<>();
        maps.put("input", "file:" + inputPath);
        app.setDefaultProperties(maps);
        ConfigurableApplicationContext ctx = app.run(args);

        JobExecutionSavingListener listener = ctx.getBean(JobExecutionSavingListener.class);

        final JobExecution jobExecution = listener.getJobExecution();
        if (jobExecution.getExitStatus() != ExitStatus.COMPLETED) {
            System.exit(12);
            return;
        }
        System.exit(0);
    }
}
Run Code Online (Sandbox Code Playgroud)

这个问题

更改我的代码以调整我的Spring Batch应用程序以便我可以使用自己的Maven父级的最佳方法是什么?

M. *_*num 6

您可以使用或不使用Spring Boot spring-boot-starter-parent作为项目的父级."Spring Boot参考指南"的本节对此进行了解释.

将此添加到您的项目poms仍然允许您使用Spring Boot并拥有自己的父级.

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.2.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)

现在您可以定义自己的父级.添加以下依赖项应该为您提供运行Spring Batch所需的所有jar.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

不能(能够)使用Spring Boot启动器作为父级的缺点之一是,如果您需要特定版本的库,则需要包含所有依赖项.将它用作父项时,就像spring-batch.version使用版本指定属性(即)一样简单.