View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.maven;
6   
7   import com.qulice.spi.ValidationException;
8   import java.util.Arrays;
9   import java.util.Collection;
10  import java.util.LinkedList;
11  import java.util.Properties;
12  import java.util.stream.Collectors;
13  import org.apache.commons.collections.CollectionUtils;
14  
15  /**
16   * Validate with maven-duplicate-finder-plugin.
17   * @since 0.5
18   * @todo #1118 ignored dependencies and resources should be placed in different parameters,
19   *  and current implementation use ':' symbol as a flag if it is resource or dependency.
20   *  Resource can be presented as a regular expression with that symbol, can cause some problem.
21   */
22  public final class DuplicateFinderValidator implements MavenValidator {
23  
24      @Override
25      public void validate(final MavenEnvironment env)
26          throws ValidationException {
27          final String prefix = "duplicatefinder";
28          if (!env.exclude(prefix, "")) {
29              final Properties props = new Properties();
30              props.put("failBuildInCaseOfConflict", "true");
31              props.put("checkTestClasspath", "false");
32              props.put("useResultFile", "false");
33              props.put(
34                  "ignoredResourcePatterns",
35                  CollectionUtils.union(
36                      env.excludes(prefix).stream()
37                          .filter(s -> !s.contains(":"))
38                          .collect(Collectors.toList()),
39                      Arrays.asList("META-INF/.*", "module-info.class")
40                  )
41              );
42              final Collection<Properties> deps = new LinkedList<>();
43              for (final String sdep : env.excludes(prefix)) {
44                  final String[] parts = sdep.split(":");
45                  if (parts.length < 2) {
46                      continue;
47                  }
48                  final Properties main = new Properties();
49                  final Properties prop = new Properties();
50                  prop.put("groupId", parts[0]);
51                  prop.put("artifactId", parts[1]);
52                  if (parts.length > 2) {
53                      prop.put("version", parts[2]);
54                  }
55                  main.put("dependency", prop);
56                  deps.add(main);
57              }
58              props.put("ignoredDependencies", deps);
59              env.executor().execute(
60                  "org.basepom.maven:duplicate-finder-maven-plugin:2.0.1",
61                  "check",
62                  props
63              );
64          }
65      }
66  
67  }