All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.jhipster.lite.module.domain.mavenplugin.JHipsterModuleMavenPlugins Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package tech.jhipster.lite.module.domain.mavenplugin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
import tech.jhipster.lite.module.domain.javabuild.command.*;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId;
import tech.jhipster.lite.module.domain.javadependency.JavaDependenciesVersions;
import tech.jhipster.lite.module.domain.javadependency.JavaDependency;
import tech.jhipster.lite.module.domain.javadependency.JavaDependencyVersion;
import tech.jhipster.lite.module.domain.javadependency.ProjectJavaDependencies;
import tech.jhipster.lite.shared.error.domain.Assert;

public final class JHipsterModuleMavenPlugins {

  private final Collection pluginsManagement;
  private final Collection plugins;

  private JHipsterModuleMavenPlugins(JHipsterModuleMavenPluginsBuilder builder) {
    pluginsManagement = builder.pluginsManagement;
    plugins = builder.plugins;
  }

  public static  JHipsterModuleMavenPluginsBuilder builder(T parentModuleBuilder) {
    return new JHipsterModuleMavenPluginsBuilder<>(parentModuleBuilder);
  }

  public JavaBuildCommands buildChanges(JavaDependenciesVersions versions, ProjectJavaDependencies projectJavaDependencies) {
    return buildChanges(versions, projectJavaDependencies, Optional.empty());
  }

  public JavaBuildCommands buildChanges(
    JavaDependenciesVersions versions,
    ProjectJavaDependencies projectJavaDependencies,
    BuildProfileId buildProfile
  ) {
    Assert.notNull("buildProfile", buildProfile);
    return buildChanges(versions, projectJavaDependencies, Optional.of(buildProfile));
  }

  private JavaBuildCommands buildChanges(
    JavaDependenciesVersions versions,
    ProjectJavaDependencies projectJavaDependencies,
    Optional buildProfile
  ) {
    Assert.notNull("versions", versions);
    Assert.notNull("projectJavaDependencies", projectJavaDependencies);

    Stream managementCommands = pluginsManagement
      .stream()
      .map(toAddMavenPlugin(versions, projectJavaDependencies, buildProfile, AddMavenPluginManagement::builder));
    Stream pluginsCommands = plugins
      .stream()
      .map(toAddMavenPlugin(versions, projectJavaDependencies, buildProfile, AddDirectMavenPlugin::builder));

    return new JavaBuildCommands(Stream.concat(managementCommands, pluginsCommands).toList());
  }

  private static  Function toAddMavenPlugin(
    JavaDependenciesVersions versions,
    ProjectJavaDependencies projectDependencies,
    Optional buildProfile,
    Supplier> builderFactory
  ) {
    return plugin -> {
      AddMavenPluginOptionalBuilder commandBuilder = builderFactory.get().plugin(plugin);
      buildProfile.ifPresent(commandBuilder::buildProfile);
      plugin
        .dependencies()
        .stream()
        .flatMap(toDependencyVersion(versions, projectDependencies))
        .forEach(commandBuilder::addDependencyVersion);
      plugin.versionSlug().map(versions::get).ifPresent(commandBuilder::pluginVersion);
      return commandBuilder.build();
    };
  }

  private static Function> toDependencyVersion(
    JavaDependenciesVersions versions,
    ProjectJavaDependencies projectDependencies
  ) {
    return dependency -> dependency.version().flatMap(JavaDependency.toVersion(versions, projectDependencies)).stream();
  }

  public static final class JHipsterModuleMavenPluginsBuilder {

    private final T parentModuleBuilder;
    private final Collection pluginsManagement = new ArrayList<>();
    private final Collection plugins = new ArrayList<>();

    private JHipsterModuleMavenPluginsBuilder(T parentModuleBuilder) {
      Assert.notNull("parentModuleBuilder", parentModuleBuilder);

      this.parentModuleBuilder = parentModuleBuilder;
    }

    public JHipsterModuleMavenPluginsBuilder pluginManagement(MavenPlugin pluginManagement) {
      Assert.notNull("pluginManagement", pluginManagement);

      pluginsManagement.add(pluginManagement);

      return this;
    }

    public JHipsterModuleMavenPluginsBuilder plugin(MavenPlugin plugin) {
      Assert.notNull("plugin", plugin);

      plugins.add(plugin);

      return this;
    }

    public T and() {
      return parentModuleBuilder;
    }

    public JHipsterModuleMavenPlugins build() {
      return new JHipsterModuleMavenPlugins(this);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy