Polish Maven profile support

This commit is contained in:
Stephane Nicoll 2021-01-19 16:30:57 +01:00
parent 50c1c28b1c
commit 6da6572ccc
4 changed files with 62 additions and 8 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.file.Path; 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.AbstractTextAssert;
import io.spring.initializr.generator.test.io.NodeAssert; import io.spring.initializr.generator.test.io.NodeAssert;
@ -310,6 +311,27 @@ public class MavenBuildAssert extends AbstractTextAssert<MavenBuildAssert> {
return this; 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}. * Assert {@code pom.xml} does not define a node with the specified {@code path}.
* @param path the path of the node * @param path the path of the node
@ -421,4 +443,11 @@ public class MavenBuildAssert extends AbstractTextAssert<MavenBuildAssert> {
return repository; 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));
};
}
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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)); "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() { private AssertProvider<MavenBuildAssert> forSampleMavenBuild() {
return forMavenBuild("sample-pom.xml"); return forMavenBuild("sample-pom.xml");
} }

View File

@ -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>

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -146,7 +146,6 @@ public class MavenBuildWriter {
if (properties.isEmpty()) { if (properties.isEmpty()) {
return; return;
} }
writer.println();
writeElement(writer, "properties", () -> { writeElement(writer, "properties", () -> {
properties.values().forEach((entry) -> writeSingleElement(writer, entry.getKey(), entry.getValue())); properties.values().forEach((entry) -> writeSingleElement(writer, entry.getKey(), entry.getValue()));
properties.versions((VersionProperty::toStandardFormat)) properties.versions((VersionProperty::toStandardFormat))
@ -202,7 +201,6 @@ public class MavenBuildWriter {
if (dependencies.isEmpty()) { if (dependencies.isEmpty()) {
return; return;
} }
writer.println();
writeElement(writer, "dependencies", () -> { writeElement(writer, "dependencies", () -> {
Collection<Dependency> compiledDependencies = writeDependencies(writer, dependencies, Collection<Dependency> compiledDependencies = writeDependencies(writer, dependencies,
(scope) -> scope == null || scope == DependencyScope.COMPILE); (scope) -> scope == null || scope == DependencyScope.COMPILE);
@ -288,7 +286,6 @@ public class MavenBuildWriter {
if (boms.isEmpty()) { if (boms.isEmpty()) {
return; return;
} }
writer.println();
writeElement(writer, "dependencyManagement", writeElement(writer, "dependencyManagement",
() -> writeCollectionElement(writer, "dependencies", boms.items() () -> writeCollectionElement(writer, "dependencies", boms.items()
.sorted(Comparator.comparing(BillOfMaterials::getOrder)).collect(Collectors.toList()), .sorted(Comparator.comparing(BillOfMaterials::getOrder)).collect(Collectors.toList()),
@ -423,7 +420,6 @@ public class MavenBuildWriter {
if (repositories.isEmpty() && pluginRepositories.isEmpty()) { if (repositories.isEmpty() && pluginRepositories.isEmpty()) {
return; return;
} }
writer.println();
writeCollectionElement(writer, "repositories", repositories, this::writeRepository); writeCollectionElement(writer, "repositories", repositories, this::writeRepository);
writeCollectionElement(writer, "pluginRepositories", pluginRepositories, this::writePluginRepository); writeCollectionElement(writer, "pluginRepositories", pluginRepositories, this::writePluginRepository);
} }
@ -547,7 +543,6 @@ public class MavenBuildWriter {
&& profile.testResources().isEmpty() && profile.plugins().isEmpty()) { && profile.testResources().isEmpty() && profile.plugins().isEmpty()) {
return; return;
} }
writer.println();
writeElement(writer, "build", () -> { writeElement(writer, "build", () -> {
writeSingleElement(writer, "defaultGoal", settings.getDefaultGoal()); writeSingleElement(writer, "defaultGoal", settings.getDefaultGoal());
writeSingleElement(writer, "finalName", settings.getFinalName()); writeSingleElement(writer, "finalName", settings.getFinalName());