自定义spring-initializr的工作方式? [英] Customize the way the spring-initializr works?

查看:647
本文介绍了自定义spring-initializr的工作方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一家企业,我们想在我们的PCF实例上内部托管Initializr.我还能够将application.yml修改为默认的一些选择.

As an enterprise we would like to host the initializr internally on our PCF instance, which I can do. I've also been able to modify application.yml to default some of the selections.

我通过简单地创建自己的spring-boot应用程序来做到这一点,该应用程序使用io.spring.initializr:initializr-web:0.3.0.RELEASE作为运行时依赖项.

I did this by simply creating my own spring-boot app which uses io.spring.initializr:initializr-web:0.3.0.RELEASE as a runtime dependency.

我有两个主要问题:

  1. UI上是否有一种方法可以对某些特定对象进行默认选择 首次出现用户界面时(例如,预先选择"Web& 例如安全性??
  2. 我可以自定义世代的输出吗?例如我们有 我们自己的Gradle自定义发行版,并希望生成 build.gradle文件看起来与initializr有所不同 产生.我们还想生成gradle.properties文件为 项目的一部分.
  1. Is there a way on the UI to default selections of certain dependencies when the UI first comes up (i.e. pre-selecting Web & Security for instance)?
  2. Can I customize the output of the generation? For instance we have our own custom distro of Gradle and would like the generated build.gradle file to look a bit different than what the initializr generates. We'd also like to generate a gradle.properties file as part of the project.

推荐答案

有一个参考指南,该指南

There is a reference guide that walk you through the creation of your own instance. Regarding your main questions:

是的,这在自定义实例中是可能的.实现ProjectRequestPostProcessor并将这两个依赖项添加到项目中.

Yes this is possible in a custom instance. Implement ProjectRequestPostProcessor and add those two dependencies to the project.

@Component
class PreselectedDependenciesRequestPostProcessor implements ProjectRequestPostProcessor {

    private final InitializrMetadataProvider metadataProvider;

    public PreselectedDependenciesRequestPostProcessor(
            InitializrMetadataProvider metadataProvider) {
        this.metadataProvider = metadataProvider;
    }

    @Override
    public void postProcessAfterResolution(ProjectRequest request,
            InitializrMetadata metadata) {
        DependenciesCapability dependencies = metadataProvider.get().getDependencies();
        if (!hasDependencies(request, "web")) {
            request.getResolvedDependencies().add(dependencies.get("web"));
        }
        if (!hasDependencies(request, "security")) {
            request.getResolvedDependencies().add(dependencies.get("security"));
        }
    }

    private boolean hasDependencies(ProjectRequest request, String... dependenciesId) {
        for (String id : dependenciesId) {
            if (getDependency(request, id) == null) {
                return false;
            }
        }
        return true;
    }

    private Dependency getDependency(ProjectRequest request, String id) {
        return request.getResolvedDependencies().stream()
                .filter(d -> id.equals(d.getId())).findFirst().orElse(null);
    }
}

不会在用户界面中选择它们,但它们将以与用户选择它们相同的方式集成到项目中.我之所以推荐这种方法,是因为IDE集成可以以与主实例(start.spring.io)相同的方式来定位您的自定义实例.入侵Web UI只会使该问题与特定的客户端IMO一起使用.

They won't be selected in the UI but they'll be integrated in the project the same way as if the user had selected them. The reason why I recommend this approach is that your custom instance can be targeted by the IDE integration the same way as the main (start.spring.io) instance. Hacking the web UI will only make this work with that particular client which, IMO, is a problem.

对于第二个问题,大多数派生都是从ProjectGenerator扩展而来的,并做他们想做的任何事情(包括覆盖gradle构建).检查该课程以了解更多详细信息.

For the second question, most forks extend from ProjectGenerator and do whatever they want (including overriding the gradle build). Check that class for more details.

Spring Initializr尚未达到1.0,因此它还没有处于合同足够成熟的状态.

Spring Initializr hasn't reached 1.0 yet so it's not in a state where the contract is mature enough.

这篇关于自定义spring-initializr的工作方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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