mirror of
https://gitee.com/dcren/initializr.git
synced 2026-09-27 23:07:39 +08:00
Move metadata build support classes to the metadata module
See gh-871
This commit is contained in:
+103
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.metadata.support;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.Build;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
import io.spring.initializr.metadata.BillOfMaterials;
|
||||
import io.spring.initializr.metadata.Dependency;
|
||||
import io.spring.initializr.metadata.Repository;
|
||||
|
||||
/**
|
||||
* An internal class used to easily translate metadata information to {@link Build} items.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public final class MetadataBuildItemMapper {
|
||||
|
||||
private MetadataBuildItemMapper() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an {@link Build} dependency from a {@link Dependency dependency metadata}.
|
||||
* @param dependency a dependency metadata
|
||||
* @return an equivalent build dependency
|
||||
*/
|
||||
public static io.spring.initializr.generator.buildsystem.Dependency toDependency(
|
||||
Dependency dependency) {
|
||||
if (dependency == null) {
|
||||
return null;
|
||||
}
|
||||
return new io.spring.initializr.generator.buildsystem.Dependency(
|
||||
dependency.getGroupId(), dependency.getArtifactId(),
|
||||
VersionReference.ofValue(dependency.getVersion()),
|
||||
toDependencyScope(dependency.getScope()), dependency.getType());
|
||||
}
|
||||
|
||||
private static DependencyScope toDependencyScope(String scope) {
|
||||
switch (scope) {
|
||||
case Dependency.SCOPE_ANNOTATION_PROCESSOR:
|
||||
return DependencyScope.ANNOTATION_PROCESSOR;
|
||||
case Dependency.SCOPE_COMPILE:
|
||||
return DependencyScope.COMPILE;
|
||||
case Dependency.SCOPE_RUNTIME:
|
||||
return DependencyScope.RUNTIME;
|
||||
case Dependency.SCOPE_COMPILE_ONLY:
|
||||
return DependencyScope.COMPILE_ONLY;
|
||||
case Dependency.SCOPE_PROVIDED:
|
||||
return DependencyScope.PROVIDED_RUNTIME;
|
||||
case Dependency.SCOPE_TEST:
|
||||
return DependencyScope.TEST_COMPILE;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a {@link Build} bom from a {@link BillOfMaterials bom metadata}.
|
||||
* @param bom a metadata bom
|
||||
* @return an equivalent build bom
|
||||
*/
|
||||
public static io.spring.initializr.generator.buildsystem.BillOfMaterials toBom(
|
||||
BillOfMaterials bom) {
|
||||
if (bom == null) {
|
||||
return null;
|
||||
}
|
||||
VersionReference version = (bom.getVersionProperty() != null)
|
||||
? VersionReference.ofProperty(bom.getVersionProperty())
|
||||
: VersionReference.ofValue(bom.getVersion());
|
||||
return new io.spring.initializr.generator.buildsystem.BillOfMaterials(
|
||||
bom.getGroupId(), bom.getArtifactId(), version, bom.getOrder());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a {@link Build} repository from a {@link Repository repository metadata}.
|
||||
* @param id the repository id
|
||||
* @param repository a repository metadata
|
||||
* @return an equivalent build repository
|
||||
*/
|
||||
public static io.spring.initializr.generator.buildsystem.MavenRepository toRepository(
|
||||
String id, Repository repository) {
|
||||
if (repository == null) {
|
||||
return null;
|
||||
}
|
||||
return new io.spring.initializr.generator.buildsystem.MavenRepository(id,
|
||||
repository.getName(), repository.getUrl().toExternalForm(),
|
||||
repository.isSnapshotsEnabled());
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.metadata.support;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.BillOfMaterials;
|
||||
import io.spring.initializr.generator.buildsystem.BuildItemResolver;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.metadata.InitializrMetadata;
|
||||
|
||||
/**
|
||||
* A {@link BuildItemResolver} that uses the {@link InitializrMetadata} to resolve build
|
||||
* items.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public final class MetadataBuildItemResolver implements BuildItemResolver {
|
||||
|
||||
private final InitializrMetadata metadata;
|
||||
|
||||
public MetadataBuildItemResolver(InitializrMetadata metadata) {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dependency resolveDependency(String id) {
|
||||
return MetadataBuildItemMapper
|
||||
.toDependency(this.metadata.getDependencies().get(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BillOfMaterials resolveBom(String id) {
|
||||
return MetadataBuildItemMapper
|
||||
.toBom(this.metadata.getConfiguration().getEnv().getBoms().get(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public MavenRepository resolveRepository(String id) {
|
||||
if (id.equals(MavenRepository.MAVEN_CENTRAL.getId())) {
|
||||
return MavenRepository.MAVEN_CENTRAL;
|
||||
}
|
||||
return MetadataBuildItemMapper.toRepository(id,
|
||||
this.metadata.getConfiguration().getEnv().getRepositories().get(id));
|
||||
}
|
||||
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.metadata.support;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.metadata.BillOfMaterials;
|
||||
import io.spring.initializr.metadata.Dependency;
|
||||
import io.spring.initializr.metadata.DependencyGroup;
|
||||
import io.spring.initializr.metadata.InitializrMetadata;
|
||||
import io.spring.initializr.metadata.Repository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link MetadataBuildItemResolver}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class MetadataBuildItemResolverTests {
|
||||
|
||||
@Test
|
||||
void resoleDependencyWithMatchingEntry() {
|
||||
InitializrMetadata metadata = new InitializrMetadata();
|
||||
DependencyGroup group = DependencyGroup.create("test");
|
||||
group.getContent().add(
|
||||
Dependency.withId("test-dep", "com.example", "test", "1.0.0", "runtime"));
|
||||
metadata.getDependencies().getContent().add(group);
|
||||
metadata.validate();
|
||||
MetadataBuildItemResolver resolver = new MetadataBuildItemResolver(metadata);
|
||||
io.spring.initializr.generator.buildsystem.Dependency dependency = resolver
|
||||
.resolveDependency("test-dep");
|
||||
assertThat(dependency.getGroupId()).isEqualTo("com.example");
|
||||
assertThat(dependency.getArtifactId()).isEqualTo("test");
|
||||
assertThat(dependency.getVersion()).hasToString("1.0.0");
|
||||
assertThat(dependency.getScope()).isEqualTo(DependencyScope.RUNTIME);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resoleDependencyWithNotMatchingEntry() {
|
||||
InitializrMetadata metadata = new InitializrMetadata();
|
||||
DependencyGroup group = DependencyGroup.create("test");
|
||||
group.getContent().add(
|
||||
Dependency.withId("test-dep", "com.example", "test", "1.0.0", "runtime"));
|
||||
metadata.getDependencies().getContent().add(group);
|
||||
metadata.validate();
|
||||
MetadataBuildItemResolver resolver = new MetadataBuildItemResolver(metadata);
|
||||
assertThat(resolver.resolveDependency("does-not-exist")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resoleBomWithMatchingEntry() {
|
||||
InitializrMetadata metadata = new InitializrMetadata();
|
||||
BillOfMaterials bom = BillOfMaterials.create("com.example", "bom", "2.0.0");
|
||||
metadata.getConfiguration().getEnv().getBoms().put("test-bom", bom);
|
||||
metadata.validate();
|
||||
MetadataBuildItemResolver resolver = new MetadataBuildItemResolver(metadata);
|
||||
io.spring.initializr.generator.buildsystem.BillOfMaterials resolvedBom = resolver
|
||||
.resolveBom("test-bom");
|
||||
assertThat(resolvedBom.getGroupId()).isEqualTo("com.example");
|
||||
assertThat(resolvedBom.getArtifactId()).isEqualTo("bom");
|
||||
assertThat(resolvedBom.getVersion()).hasToString("2.0.0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resoleBomWithNotMatchingEntry() {
|
||||
InitializrMetadata metadata = new InitializrMetadata();
|
||||
BillOfMaterials bom = BillOfMaterials.create("com.example", "bom", "2.0.0");
|
||||
metadata.getConfiguration().getEnv().getBoms().put("test-bom", bom);
|
||||
metadata.validate();
|
||||
MetadataBuildItemResolver resolver = new MetadataBuildItemResolver(metadata);
|
||||
assertThat(resolver.resolveBom("does-not-exost")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resoleRepositoryWithMatchingEntry() throws MalformedURLException {
|
||||
InitializrMetadata metadata = new InitializrMetadata();
|
||||
metadata.getConfiguration().getEnv().getRepositories().put("test-repo",
|
||||
new Repository("test", new URL("https://example.com/repo"), false));
|
||||
metadata.validate();
|
||||
MetadataBuildItemResolver resolver = new MetadataBuildItemResolver(metadata);
|
||||
MavenRepository repository = resolver.resolveRepository("test-repo");
|
||||
assertThat(repository.getId()).isEqualTo("test-repo");
|
||||
assertThat(repository.getName()).isEqualTo("test");
|
||||
assertThat(repository.getUrl()).isEqualTo("https://example.com/repo");
|
||||
assertThat(repository.isSnapshotsEnabled()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resoleRepositoryWithNonMatchingEntry() throws MalformedURLException {
|
||||
InitializrMetadata metadata = new InitializrMetadata();
|
||||
metadata.getConfiguration().getEnv().getRepositories().put("test-repo",
|
||||
new Repository("test", new URL("https://example.com/repo"), false));
|
||||
metadata.validate();
|
||||
MetadataBuildItemResolver resolver = new MetadataBuildItemResolver(metadata);
|
||||
assertThat(resolver.resolveRepository("does-not-exist")).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user