bamaer commented on code in PR #9578: URL: https://github.com/apache/hop/pull/9578#discussion_r4619624300
########## bom/src/test/java/org/apache/hop/bom/HopBomTest.java: ########## @@ -0,0 +1,317 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.hop.bom; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.function.Predicate; +import java.util.regex.PatternSyntaxException; +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathFactory; +import javax.xml.xpath.XPathFactoryConfigurationException; +import org.junit.jupiter.api.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** Checks the published BOM independently of the modules enabled in the current reactor. */ +class HopBomTest { + private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = documentBuilderFactory(); + private static final XPathFactory XPATH_FACTORY = xpathFactory(); + + private static final Map<String, String> EXCLUSIONS = + Map.ofEntries( + Map.entry("bom/pom.xml", "The BOM cannot manage itself"), + Map.entry( + "engine-bench/pom.xml", "Standalone JMH benchmarks, outside the release reactor"), + Map.entry( + "web-tests/pom.xml", "Selenium test harness; installation and deployment skipped"), + Map.entry( + "tools/spark-client-pack/pom.xml", + "Standalone Spark pack materializer, not a Hop release artifact"), + Map.entry("lib-p2/swtbot/pom.xml", "Unpublished local Eclipse wrapper"), + Map.entry("lib-p2/tm4e/pom.xml", "Unpublished local Eclipse wrapper"), + Map.entry("assemblies/pom.xml", "Unpublished assembly parent"), + Map.entry("assemblies/debug/pom.xml", "Unpublished debug assembly"), + Map.entry("assemblies/plugins/pom.xml", "Unpublished intermediate plugin assembly"), + Map.entry("assemblies/static/pom.xml", "Unpublished intermediate static assembly"), + Map.entry("assemblies/web/pom.xml", "Unpublished web assembly")); + + @Test + void managesEveryProjectArtifact() throws Exception { + final var bomDirectory = + Path.of(System.getProperty("basedir", ".")).toAbsolutePath().normalize(); + final var root = bomDirectory.getParent(); + final var version = text(parse(root.resolve("pom.xml")), "/project/version"); + final var sourcePoms = sourcePoms(root); + final var expected = new TreeMap<String, String>(); + final var unusedExclusions = new TreeSet<>(EXCLUSIONS.keySet()); + + for (final var pom : sourcePoms) { + final var relative = root.relativize(pom).toString().replace('\\', '/'); + if (EXCLUSIONS.containsKey(relative)) { + unusedExclusions.remove(relative); + continue; + } + final var model = parse(pom); + final var group = inherited(model, "groupId"); + final var artifact = text(model, "/project/artifactId"); + final var packaging = defaultTo(text(model, "/project/packaging"), "jar"); + assertEquals("org.apache.hop", group, relative + ": unexpected group; review BOM coverage"); + assertEquals( + version, inherited(model, "version"), relative + ": not aligned to the Hop release"); + assertTrue( + Set.of("jar", "pom").contains(packaging), + relative + ": review new packaging " + packaging); + addExpected(expected, group + ":" + artifact + ":" + packaging, relative); + + // The inherited assembly profile publishes unclassified ZIPs when this descriptor exists. + final var assembly = pom.getParent().resolve("src/assembly/assembly.xml"); + if (Files.isRegularFile(assembly)) { + assertEquals( + "", + text(model, "/project/properties/assembly_appendId"), + relative + ": review assembly classifier override"); + final var formats = nodes(parse(assembly), "/assembly/formats/format"); + assertTrue(formats.getLength() > 0, relative + ": assembly has no formats"); + for (int i = 0; i < formats.getLength(); i++) { + addExpected( + expected, + group + ":" + artifact + ":" + formats.item(i).getTextContent().trim(), + relative); + } + } + } + assertTrue(unusedExclusions.isEmpty(), "Remove stale BOM exclusions: " + unusedExclusions); + assertFalse(expected.isEmpty(), "No source POMs found under " + root); + assertEquals( + "false", + text(parse(root.resolve("pom.xml")), "/project/properties/assembly_appendId"), + "Review BOM ZIP classifiers when the assembly convention changes"); + + final var managed = + nodes( + parse(bomDirectory.resolve("pom.xml")), + "/project/dependencyManagement/dependencies/dependency"); + final var actual = new TreeSet<String>(); + for (int i = 0; i < managed.getLength(); i++) { + final var dependency = managed.item(i); + final var coordinate = + text(dependency, "groupId") + + ":" + + text(dependency, "artifactId") + + ":" + + defaultTo(text(dependency, "type"), "jar"); + assertTrue(actual.add(coordinate), "Duplicate BOM entry: " + coordinate); + assertEquals( + "${project.version}", + text(dependency, "version"), + coordinate + ": use the BOM release version"); + assertEquals( + "", text(dependency, "scope"), coordinate + ": do not impose a dependency scope"); + assertEquals("", text(dependency, "classifier"), coordinate + ": unexpected classifier"); + } + + final var missing = new TreeMap<>(expected); + actual.forEach(missing::remove); + final var stale = new TreeSet<>(actual); + stale.removeAll(expected.keySet()); + assertAll( + () -> + assertTrue( + missing.isEmpty(), + "Add entries to bom/pom.xml (coordinate=source POM): " + missing), + () -> + assertTrue( + stale.isEmpty(), + "Remove stale or incorrectly typed entries from bom/pom.xml: " + stale)); + } + + private static void addExpected( + final Map<String, String> expected, final String coordinate, final String pom) { + final var previous = expected.putIfAbsent(coordinate, pom); + assertNull( + previous, "Duplicate project coordinate " + coordinate + " in " + previous + " and " + pom); + } + + private static Set<Path> sourcePoms(final Path root) throws IOException { + final var result = new TreeSet<Path>(); + final var ignored = ignoredPaths(root); + // This build-only module deliberately uses JDK I/O: depending on HopVfs would pull Hop + // runtime artifacts into the BOM's tests and prevent testing before those artifacts exist. + Files.walkFileTree( Review Comment: **Should fix.** Walking the filesystem picks up every `pom.xml` that the root `.gitignore` or the global excludesfile doesn't hide. `.git/info/exclude` isn't read, so a worktree inside the checkout (for example `.claude/worktrees/`, which is commonly excluded there) fails the test, and with it a full `./mvnw install`: ``` Duplicate project coordinate org.apache.hop:hop-core:jar in .claude/worktrees/demo/core/pom.xml and core/pom.xml ``` Suggested fix: follow the `<module>` entries from the root POM, including all profiles, instead of walking the tree. On this PR that finds the same POMs as the filesystem walk, minus `engine-bench` and `tools/spark-client-pack`. Those two exclusions and the `.gitignore` / `~/.gitconfig` parsing can then go. ########## bom/pom.xml: ########## @@ -0,0 +1,3578 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with + ~ this work for additional information regarding copyright ownership. + ~ The ASF licenses this file to You 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. + --> +<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.hop</groupId> + <artifactId>hop</artifactId> + <version>2.21.0-SNAPSHOT</version> + </parent> Review Comment: **Blocker.** `hop-bom` inherits from `org.apache.hop:hop`, so importing it also imports the root POM's `dependencyManagement`. A project that imports only `hop-bom` ends up with these managed versions: | Artifact | Version | |---|---| | google-auth-library-credentials, google-auth-library-oauth2-http | 1.46.0 | | api-common | 2.62.0 | | proto-google-common-protos | 2.70.0 | | error_prone_annotations | 2.50.0 | | icu4j | 78.3 | | libphonenumber | 9.0.41 | | animal-sniffer-annotations | 1.27 | | stax2-api | 4.3.2 | | `org.eclipse.swt.gtk.linux.x86_64`, from the OS-activated `swt-unix` profile | 3.135.0 | A `google-auth-library-oauth2-http` dependency declared without a version resolves to 1.46.0, and these pins override the project's own transitive versions. That contradicts the docs ("`hop-libs` … is not imported by `hop-bom`"), and it is hard to take back once the BOM is published. Suggested fix: flatten the published POM in `bom` mode, as `assemblies/client` already does with flatten: ```xml <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>flatten-maven-plugin</artifactId> <version>${flatten-maven-plugin.version}</version> <configuration> <flattenMode>bom</flattenMode> <outputDirectory>${project.build.directory}</outputDirectory> <pomElements> <dependencies>remove</dependencies> <profiles>remove</profiles> <build>remove</build> <properties>remove</properties> </pomElements> </configuration> <executions> <execution> <id>flatten</id> <goals><goal>flatten</goal></goals> <phase>process-resources</phase> </execution> </executions> </plugin> ``` I tried this locally: the only managed entries left are the 641 `org.apache.hop` ones, `hop-core` still resolves without a version, `HopBomTest` still passes, and the published POM keeps its name, URL, licence, developers and SCM metadata. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
