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   *
18   * @since 0.5
19   * @todo #1118 ignored dependencies and resources should be placed in different parameters,
20   *  and current implementation use ':' symbol as a flag if it is resource or dependency.
21   *  Resource can be presented as a regular expression with that symbol, can cause some problem.
22   */
23  public final class DuplicateFinderValidator implements MavenValidator {
24  
25      /**
26       * Default constructor.
27       */
28      public DuplicateFinderValidator() {
29          // nothing to initialize
30      }
31  
32      @Override
33      public void validate(final MavenEnvironment env)
34          throws ValidationException {
35          final String prefix = "duplicatefinder";
36          if (!env.exclude(prefix, "")) {
37              final Properties props = new Properties();
38              props.put("failBuildInCaseOfConflict", "true");
39              props.put("checkTestClasspath", "false");
40              props.put("useResultFile", "false");
41              props.put(
42                  "ignoredResourcePatterns",
43                  CollectionUtils.union(
44                      env.excludes(prefix).stream()
45                          .filter(s -> !s.contains(":"))
46                          .collect(Collectors.toList()),
47                      Arrays.asList("META-INF/.*", "module-info.class")
48                  )
49              );
50              final Collection<Properties> deps = new ArrayList<>(0);
51              for (final String sdep : env.excludes(prefix)) {
52                  final String[] parts = sdep.split(":", -1);
53                  if (parts.length < 2) {
54                      continue;
55                  }
56                  final Properties main = new Properties();
57                  final Properties prop = new Properties();
58                  prop.put("groupId", parts[0]);
59                  prop.put("artifactId", parts[1]);
60                  if (parts.length > 2) {
61                      prop.put("version", parts[2]);
62                  }
63                  main.put("dependency", prop);
64                  deps.add(main);
65              }
66              props.put("ignoredDependencies", deps);
67              env.executor().execute(
68                  "org.basepom.maven:duplicate-finder-maven-plugin:2.0.1",
69                  "check",
70                  props
71              );
72          }
73      }
74  }