mirror of
https://gitee.com/dcren/initializr.git
synced 2025-07-15 13:46:50 +08:00
Polish Maven profile support
This commit is contained in:
parent
50c1c28b1c
commit
6da6572ccc
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
@ -19,6 +19,7 @@ package io.spring.initializr.generator.test.buildsystem.maven;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import io.spring.initializr.generator.test.io.AbstractTextAssert;
|
||||
import io.spring.initializr.generator.test.io.NodeAssert;
|
||||
@ -310,6 +311,27 @@ public class MavenBuildAssert extends AbstractTextAssert<MavenBuildAssert> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert {@code pom.xml} defines a profile with the specified {@code id}.
|
||||
* @param id the id of the profile
|
||||
* @return {@code this} assertion object
|
||||
*/
|
||||
public MavenBuildAssert hasProfile(String id) {
|
||||
this.pom.nodesAtPath("/project/profiles/profile").areExactly(1,
|
||||
new Condition<>(profile(id), "matching profile"));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert {@code pom.xml} does not define a profile with the specified {@code id}.
|
||||
* @param id the id of the profile
|
||||
* @return {@code this} assertion object
|
||||
*/
|
||||
public MavenBuildAssert doesNotHaveProfile(String id) {
|
||||
this.pom.nodesAtPath("/project/profiles/profile").noneMatch(profile(id));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert {@code pom.xml} does not define a node with the specified {@code path}.
|
||||
* @param path the path of the node
|
||||
@ -421,4 +443,11 @@ public class MavenBuildAssert extends AbstractTextAssert<MavenBuildAssert> {
|
||||
return repository;
|
||||
}
|
||||
|
||||
private static Predicate<? super Node> profile(String id) {
|
||||
return (candidate) -> {
|
||||
String actualId = ((Element) candidate).getElementsByTagName("id").item(0).getTextContent();
|
||||
return (actualId.equals(id));
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
@ -324,6 +324,17 @@ class MavenBuildAssertTests {
|
||||
"Acme Milestones", "https://repo.example.com/milestone", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasProfile() {
|
||||
assertThat(forMavenBuild("sample-profiles-pom.xml")).hasProfile("one");
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasProfileWithUnknownId() {
|
||||
assertThatExceptionOfType(AssertionError.class)
|
||||
.isThrownBy(() -> assertThat(forMavenBuild("sample-profiles-pom.xml")).hasProfile("unknown"));
|
||||
}
|
||||
|
||||
private AssertProvider<MavenBuildAssert> forSampleMavenBuild() {
|
||||
return forMavenBuild("sample-pom.xml");
|
||||
}
|
||||
|
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>one</id>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>two</id>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
</project>
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
@ -146,7 +146,6 @@ public class MavenBuildWriter {
|
||||
if (properties.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
writer.println();
|
||||
writeElement(writer, "properties", () -> {
|
||||
properties.values().forEach((entry) -> writeSingleElement(writer, entry.getKey(), entry.getValue()));
|
||||
properties.versions((VersionProperty::toStandardFormat))
|
||||
@ -202,7 +201,6 @@ public class MavenBuildWriter {
|
||||
if (dependencies.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
writer.println();
|
||||
writeElement(writer, "dependencies", () -> {
|
||||
Collection<Dependency> compiledDependencies = writeDependencies(writer, dependencies,
|
||||
(scope) -> scope == null || scope == DependencyScope.COMPILE);
|
||||
@ -288,7 +286,6 @@ public class MavenBuildWriter {
|
||||
if (boms.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
writer.println();
|
||||
writeElement(writer, "dependencyManagement",
|
||||
() -> writeCollectionElement(writer, "dependencies", boms.items()
|
||||
.sorted(Comparator.comparing(BillOfMaterials::getOrder)).collect(Collectors.toList()),
|
||||
@ -423,7 +420,6 @@ public class MavenBuildWriter {
|
||||
if (repositories.isEmpty() && pluginRepositories.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
writer.println();
|
||||
writeCollectionElement(writer, "repositories", repositories, this::writeRepository);
|
||||
writeCollectionElement(writer, "pluginRepositories", pluginRepositories, this::writePluginRepository);
|
||||
}
|
||||
@ -547,7 +543,6 @@ public class MavenBuildWriter {
|
||||
&& profile.testResources().isEmpty() && profile.plugins().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
writer.println();
|
||||
writeElement(writer, "build", () -> {
|
||||
writeSingleElement(writer, "defaultGoal", settings.getDefaultGoal());
|
||||
writeSingleElement(writer, "finalName", settings.getFinalName());
|
||||
|
Loading…
Reference in New Issue
Block a user