XComp commented on code in PR #21349: URL: https://github.com/apache/flink/pull/21349#discussion_r1191307099
########## tools/ci/flink-ci-tools/src/main/java/org/apache/flink/tools/ci/optional/ShadeOptionalChecker.java: ########## @@ -0,0 +1,266 @@ +/* + * 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.flink.tools.ci.optional; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.tools.ci.utils.dependency.DependencyParser; +import org.apache.flink.tools.ci.utils.shade.ShadeParser; +import org.apache.flink.tools.ci.utils.shared.Dependency; +import org.apache.flink.tools.ci.utils.shared.DependencyTree; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Verifies that all dependencies bundled with the shade-plugin are marked as optional in the pom. + * This ensures compatibility with later maven versions and in general simplifies dependency + * management as transitivity is no longer dependent on the shade-plugin. + * + * <p>In Maven 3.3 the dependency tree was made immutable at runtime, and thus can no longer be + * changed by the shade plugin. The plugin would usually remove a dependency from the tree when it + * is being bundled (known as dependency reduction). While dependency reduction still works for the + * published poms (== what users consume) since it can still change the content of the final pom, + * while developing Flink it no longer works. This breaks plenty of things, since suddenly a bunch + * of dependencies are still visible to downstream modules that weren't before. + * + * <p>To workaround this we mark all dependencies that we bundle as optional; this makes them + * non-transitive. To a downstream module, behavior-wise a non-transitive dependency is identical to + * a removed dependency. + * + * <p>This checker analyzes the bundled dependencies (based on the shade-plugin output) and the set + * of dependencies (based on the dependency plugin) to detect cases where a dependency is not marked + * as optional as it should. + * + * <p>The enforced rule is rather simple: Any dependency that is bundled, or any of its parents, + * must show up as optional in the dependency tree. The parent clause is required to cover cases + * where a module has 2 paths to a bundled dependency. If a module depends on A1/A2, each depending + * on B, with A1 and B being bundled, then even if A1 is marked as optional B is still shown as a + * non-optional dependency (because the non-optional A2 still needs it!). + */ +public class ShadeOptionalChecker { + private static final Logger LOG = LoggerFactory.getLogger(ShadeOptionalChecker.class); + + public static void main(String[] args) throws IOException { + if (args.length < 2) { + System.out.println( + "Usage: ShadeOptionalChecker <pathShadeBuildOutput> <pathMavenDependencyOutput>"); + System.exit(1); + } + + final Path shadeOutputPath = Paths.get(args[0]); + final Path dependencyOutputPath = Paths.get(args[1]); + + final Map<String, Set<Dependency>> bundledDependenciesByModule = + ShadeParser.parseShadeOutput(shadeOutputPath); + final Map<String, DependencyTree> dependenciesByModule = + DependencyParser.parseDependencyTreeOutput(dependencyOutputPath); + + final Map<String, Set<Dependency>> violations = + checkOptionalFlags(bundledDependenciesByModule, dependenciesByModule); + + if (!violations.isEmpty()) { + LOG.error( + "{} modules bundle in total {} dependencies without them being marked as optional in the pom.", + violations.keySet().size(), + violations.size()); Review Comment: ```suggestion violations.values().stream().mapToInt(Set::size).sum()); ``` Not sure why I missed it. But other than that, the description looks alright. -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org