mirror of
https://gitee.com/dcren/initializr.git
synced 2025-04-24 18:04:58 +08:00
Introduce shared utilities for ProjectRequestPostProcessor
This commit is contained in:
parent
378623fcc0
commit
aad08e0c21
@ -483,15 +483,21 @@ public class Dependency extends MetadataElement implements Describable {
|
||||
}
|
||||
|
||||
public static Dependency withId(String id, String groupId, String artifactId,
|
||||
String version) {
|
||||
String version, String scope) {
|
||||
Dependency dependency = new Dependency();
|
||||
dependency.setId(id);
|
||||
dependency.groupId = groupId;
|
||||
dependency.artifactId = artifactId;
|
||||
dependency.version = version;
|
||||
dependency.scope = (scope != null ? scope : SCOPE_COMPILE);
|
||||
return dependency;
|
||||
}
|
||||
|
||||
public static Dependency withId(String id, String groupId, String artifactId,
|
||||
String version) {
|
||||
return withId(id, groupId, artifactId, version, null);
|
||||
}
|
||||
|
||||
public static Dependency withId(String id, String groupId, String artifactId) {
|
||||
return withId(id, groupId, artifactId, null);
|
||||
}
|
||||
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.spring.initializr.service.extension;
|
||||
|
||||
import io.spring.initializr.generator.ProjectRequest;
|
||||
import io.spring.initializr.generator.ProjectRequestPostProcessor;
|
||||
import io.spring.initializr.metadata.Dependency;
|
||||
import io.spring.initializr.util.Version;
|
||||
|
||||
/**
|
||||
* Base {@link ProjectRequestPostProcessor} with reusable utilities.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class AbstractProjectRequestPostProcessor implements ProjectRequestPostProcessor {
|
||||
|
||||
/**
|
||||
* Determine if the {@link ProjectRequest request} defines the dependency with the
|
||||
* specified {@code dependencyId}.
|
||||
* @param request the request to handle
|
||||
* @param dependencyId the id of a dependency
|
||||
* @return {@code true} if the project defines that dependency
|
||||
*/
|
||||
protected boolean hasDependency(ProjectRequest request, String dependencyId) {
|
||||
return hasDependencies(request, dependencyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the {@link ProjectRequest request} defines the dependencies with the
|
||||
* specified {@code dependenciesId}.
|
||||
* @param request the request to handle
|
||||
* @param dependenciesId the dependency ids
|
||||
* @return {@code true} if the project defines all dependencies
|
||||
*/
|
||||
protected boolean hasDependencies(ProjectRequest request, String... dependenciesId) {
|
||||
for (String id : dependenciesId) {
|
||||
if (getDependency(request, id) == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link Dependency} with the specified {@code id} or {@code null} if the
|
||||
* project does not define it.
|
||||
* @param request the request to handle
|
||||
* @param id the id of a dependency
|
||||
* @return the {@link Dependency} with that id or {@code null} if the project does not
|
||||
* define such dependency
|
||||
*/
|
||||
protected Dependency getDependency(ProjectRequest request, String id) {
|
||||
return request.getResolvedDependencies().stream()
|
||||
.filter(d -> id.equals(d.getId())).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify if the Spring Boot version of the {@link ProjectRequest request} is higher
|
||||
* or equal to the specified {@link Version}.
|
||||
* @param request the request to handle
|
||||
* @param version the minimum version
|
||||
* @return {@code true} if the requested version is equal or higher than the specified
|
||||
* {@code version}
|
||||
*/
|
||||
protected boolean isSpringBootVersionAtLeastAfter(ProjectRequest request,
|
||||
Version version) {
|
||||
Version requestVersion = Version.safeParse(request.getBootVersion());
|
||||
return version.compareTo(requestVersion) <= 0;
|
||||
}
|
||||
|
||||
}
|
@ -32,19 +32,15 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
class JacksonKotlinRequestPostProcessor implements ProjectRequestPostProcessor {
|
||||
|
||||
private final Dependency jacksonModuleKotlin;
|
||||
|
||||
public JacksonKotlinRequestPostProcessor() {
|
||||
this.jacksonModuleKotlin = Dependency.withId("jackson-module-kotlin",
|
||||
"com.fasterxml.jackson.module", "jackson-module-kotlin");
|
||||
}
|
||||
static final Dependency JACKSON_KOTLIN = Dependency.withId("jackson-module-kotlin",
|
||||
"com.fasterxml.jackson.module", "jackson-module-kotlin");
|
||||
|
||||
@Override
|
||||
public void postProcessAfterResolution(ProjectRequest request,
|
||||
InitializrMetadata metadata) {
|
||||
if (request.getFacets().contains("json")
|
||||
&& "kotlin".equals(request.getLanguage())) {
|
||||
request.getResolvedDependencies().add(this.jacksonModuleKotlin);
|
||||
request.getResolvedDependencies().add(JACKSON_KOTLIN);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,33 +31,19 @@ import org.springframework.stereotype.Component;
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@Component
|
||||
class ReactorTestRequestPostProcessor implements ProjectRequestPostProcessor {
|
||||
class ReactorTestRequestPostProcessor extends AbstractProjectRequestPostProcessor {
|
||||
|
||||
private static final Version VERSION_2_0_0_M2 = Version.parse("2.0.0.M2");
|
||||
|
||||
private final Dependency reactorTest;
|
||||
|
||||
public ReactorTestRequestPostProcessor() {
|
||||
this.reactorTest = Dependency.withId(
|
||||
"reactor-test", "io.projectreactor", "reactor-test");
|
||||
this.reactorTest.setScope(Dependency.SCOPE_TEST);
|
||||
}
|
||||
static final Dependency REACTOR_TEST = Dependency.withId("reactor-test",
|
||||
"io.projectreactor", "reactor-test", null, Dependency.SCOPE_TEST);
|
||||
|
||||
@Override
|
||||
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) {
|
||||
if (hasWebFlux(request) && isAtLeastAfter(request, VERSION_2_0_0_M2)) {
|
||||
request.getResolvedDependencies().add(this.reactorTest);
|
||||
if (hasDependency(request, "webflux")
|
||||
&& isSpringBootVersionAtLeastAfter(request, VERSION_2_0_0_M2)) {
|
||||
request.getResolvedDependencies().add(REACTOR_TEST);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasWebFlux(ProjectRequest request) {
|
||||
return request.getResolvedDependencies().stream()
|
||||
.anyMatch(d -> "webflux".equals(d.getId()));
|
||||
}
|
||||
|
||||
private boolean isAtLeastAfter(ProjectRequest request, Version version) {
|
||||
Version requestVersion = Version.safeParse(request.getBootVersion());
|
||||
return version.compareTo(requestVersion) <= 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -31,34 +31,20 @@ import org.springframework.stereotype.Component;
|
||||
* @author Tim Riemer
|
||||
*/
|
||||
@Component
|
||||
class SpringBatchTestRequestPostProcessor implements ProjectRequestPostProcessor {
|
||||
class SpringBatchTestRequestPostProcessor extends AbstractProjectRequestPostProcessor {
|
||||
|
||||
private static final Version VERSION_1_3_0 = Version.parse("1.3.0.RELEASE");
|
||||
|
||||
private final Dependency springBatchTest;
|
||||
|
||||
public SpringBatchTestRequestPostProcessor() {
|
||||
this.springBatchTest = Dependency.withId("spring-batch-test",
|
||||
"org.springframework.batch", "spring-batch-test");
|
||||
this.springBatchTest.setScope(Dependency.SCOPE_TEST);
|
||||
}
|
||||
static final Dependency SPRING_BATCH_TEST = Dependency.withId("spring-batch-test",
|
||||
"org.springframework.batch", "spring-batch-test", null, Dependency.SCOPE_TEST);
|
||||
|
||||
@Override
|
||||
public void postProcessAfterResolution(ProjectRequest request,
|
||||
InitializrMetadata metadata) {
|
||||
if (hasSpringBatch(request) && isAtLeastAfter(request, VERSION_1_3_0)) {
|
||||
request.getResolvedDependencies().add(this.springBatchTest);
|
||||
if (hasDependency(request, "batch")
|
||||
&& isSpringBootVersionAtLeastAfter(request, VERSION_1_3_0)) {
|
||||
request.getResolvedDependencies().add(SPRING_BATCH_TEST);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasSpringBatch(ProjectRequest request) {
|
||||
return request.getResolvedDependencies().stream()
|
||||
.anyMatch(d -> "batch".equals(d.getId()));
|
||||
}
|
||||
|
||||
private boolean isAtLeastAfter(ProjectRequest request, Version version) {
|
||||
Version requestVersion = Version.safeParse(request.getBootVersion());
|
||||
return version.compareTo(requestVersion) <= 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.ProjectRequest;
|
||||
import io.spring.initializr.generator.ProjectRequestPostProcessor;
|
||||
import io.spring.initializr.metadata.InitializrMetadata;
|
||||
import io.spring.initializr.util.Version;
|
||||
|
||||
@ -33,19 +32,18 @@ import org.springframework.stereotype.Component;
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@Component
|
||||
class SpringBoot2RequestPostProcessor implements ProjectRequestPostProcessor {
|
||||
class SpringBoot2RequestPostProcessor extends AbstractProjectRequestPostProcessor {
|
||||
|
||||
private static final Version VERSION_2_0_0_M1 = Version.parse("2.0.0.M1");
|
||||
|
||||
private static final List<String> VALID_VERSIONS = Arrays.asList("1.8", "9");
|
||||
|
||||
@Override
|
||||
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) {
|
||||
if (!VALID_VERSIONS.contains(request.getJavaVersion())) {
|
||||
Version requestVersion = Version.safeParse(request.getBootVersion());
|
||||
if (VERSION_2_0_0_M1.compareTo(requestVersion) <= 0) {
|
||||
request.setJavaVersion("1.8");
|
||||
}
|
||||
public void postProcessAfterResolution(ProjectRequest request,
|
||||
InitializrMetadata metadata) {
|
||||
if (!VALID_VERSIONS.contains(request.getJavaVersion())
|
||||
&& isSpringBootVersionAtLeastAfter(request, VERSION_2_0_0_M1)) {
|
||||
request.setJavaVersion("1.8");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,34 +31,21 @@ import org.springframework.stereotype.Component;
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@Component
|
||||
class SpringSecurityTestRequestPostProcessor implements ProjectRequestPostProcessor {
|
||||
class SpringSecurityTestRequestPostProcessor extends AbstractProjectRequestPostProcessor {
|
||||
|
||||
private static final Version VERSION_1_3_0 = Version.parse("1.3.0.RELEASE");
|
||||
|
||||
private final Dependency springSecurityTest;
|
||||
|
||||
public SpringSecurityTestRequestPostProcessor() {
|
||||
this.springSecurityTest = Dependency.withId("spring-security-test",
|
||||
"org.springframework.security", "spring-security-test");
|
||||
this.springSecurityTest.setScope(Dependency.SCOPE_TEST);
|
||||
}
|
||||
static final Dependency SPRING_SECURITY_TEST = Dependency.withId(
|
||||
"spring-security-test", "org.springframework.security",
|
||||
"spring-security-test", null, Dependency.SCOPE_TEST);
|
||||
|
||||
@Override
|
||||
public void postProcessAfterResolution(ProjectRequest request,
|
||||
InitializrMetadata metadata) {
|
||||
if (hasSpringSecurity(request) && isAtLeastAfter(request, VERSION_1_3_0)) {
|
||||
request.getResolvedDependencies().add(this.springSecurityTest);
|
||||
if (hasDependency(request, "security")
|
||||
&& isSpringBootVersionAtLeastAfter(request, VERSION_1_3_0)) {
|
||||
request.getResolvedDependencies().add(SPRING_SECURITY_TEST);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasSpringSecurity(ProjectRequest request) {
|
||||
return request.getResolvedDependencies().stream()
|
||||
.anyMatch(d -> "security".equals(d.getId()));
|
||||
}
|
||||
|
||||
private boolean isAtLeastAfter(ProjectRequest request, Version version) {
|
||||
Version requestVersion = Version.safeParse(request.getBootVersion());
|
||||
return version.compareTo(requestVersion) <= 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -34,7 +34,8 @@ import org.springframework.stereotype.Component;
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@Component
|
||||
public class SpringSessionRequestPostProcessor implements ProjectRequestPostProcessor {
|
||||
public class SpringSessionRequestPostProcessor
|
||||
extends AbstractProjectRequestPostProcessor {
|
||||
|
||||
private static final Version VERSION_2_0_0_M3 = Version.parse("2.0.0.M3");
|
||||
|
||||
@ -46,9 +47,9 @@ public class SpringSessionRequestPostProcessor implements ProjectRequestPostProc
|
||||
|
||||
|
||||
@Override
|
||||
public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) {
|
||||
Version requestVersion = Version.safeParse(request.getBootVersion());
|
||||
if (VERSION_2_0_0_M3.compareTo(requestVersion) <= 0) {
|
||||
public void postProcessAfterResolution(ProjectRequest request,
|
||||
InitializrMetadata metadata) {
|
||||
if (isSpringBootVersionAtLeastAfter(request, VERSION_2_0_0_M3)) {
|
||||
swapSpringSessionDepenendency(request);
|
||||
}
|
||||
}
|
||||
@ -71,13 +72,4 @@ public class SpringSessionRequestPostProcessor implements ProjectRequestPostProc
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasDependency(ProjectRequest request, String id) {
|
||||
return getDependency(request, id) != null;
|
||||
}
|
||||
|
||||
private Dependency getDependency(ProjectRequest request, String id) {
|
||||
return request.getResolvedDependencies().stream()
|
||||
.filter(d -> id.equals(d.getId())).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,9 +17,11 @@
|
||||
package io.spring.initializr.service.extension;
|
||||
|
||||
import io.spring.initializr.generator.ProjectRequest;
|
||||
import io.spring.initializr.metadata.Dependency;
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.spring.initializr.service.extension.JacksonKotlinRequestPostProcessor.JACKSON_KOTLIN;
|
||||
import static io.spring.initializr.service.extension.ReactorTestRequestPostProcessor.REACTOR_TEST;
|
||||
|
||||
/**
|
||||
* Tests for {@link JacksonKotlinRequestPostProcessor}.
|
||||
*
|
||||
@ -29,12 +31,6 @@ import org.junit.Test;
|
||||
public class JacksonKotlinRequestPostProcessorTests
|
||||
extends AbstractRequestPostProcessorTests {
|
||||
|
||||
static final Dependency JACKSON_KOTLIN = Dependency.withId("jackson-module-kotlin",
|
||||
"com.fasterxml.jackson.module", "jackson-module-kotlin");
|
||||
|
||||
static final Dependency REACTOR_TEST = Dependency.create(
|
||||
"io.projectreactor", "reactor-test", null, Dependency.SCOPE_TEST);
|
||||
|
||||
@Test
|
||||
public void jacksonModuleKotlinIsAdded() {
|
||||
ProjectRequest request = createProjectRequest("webflux");
|
||||
|
Loading…
Reference in New Issue
Block a user