View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.maven;
6   
7   import com.qulice.spi.ValidationException;
8   import java.util.ArrayList;
9   import java.util.Arrays;
10  import java.util.Collection;
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      /**
25       * Default constructor.
26       */
27      public DuplicateFinderValidator() {
28          // nothing to initialize
29      }
30  
31      @Override
32      public void validate(final MavenEnvironment env)
33          throws ValidationException {
34          final String prefix = "duplicatefinder";
35          if (!env.exclude(prefix, "")) {
36              final Properties props = new Properties();
37              props.put("failBuildInCaseOfConflict", "true");
38              props.put("checkTestClasspath", "false");
39              props.put("useResultFile", "false");
40              props.put(
41                  "ignoredResourcePatterns",
42                  CollectionUtils.union(
43                      env.excludes(prefix).stream()
44                          .filter(s -> !s.contains(":"))
45                          .collect(Collectors.toList()),
46                      Arrays.asList("META-INF/.*", "module-info.class")
47                  )
48              );
49              final Collection<Properties> deps = new ArrayList<>(0);
50              for (final String sdep : env.excludes(prefix)) {
51                  final String[] parts = sdep.split(":", -1);
52                  if (parts.length < 2) {
53                      continue;
54                  }
55                  final Properties main = new Properties();
56                  final Properties prop = new Properties();
57                  prop.put("groupId", parts[0]);
58                  prop.put("artifactId", parts[1]);
59                  if (parts.length > 2) {
60                      prop.put("version", parts[2]);
61                  }
62                  main.put("dependency", prop);
63                  deps.add(main);
64              }
65              props.put("ignoredDependencies", deps);
66              env.executor().execute(
67                  "org.basepom.maven:duplicate-finder-maven-plugin:2.0.1",
68                  "check",
69                  props
70              );
71          }
72      }
73  }