View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.maven;
32  
33  import com.qulice.spi.ValidationException;
34  import java.util.Arrays;
35  import java.util.Collection;
36  import java.util.LinkedList;
37  import java.util.Properties;
38  import java.util.stream.Collectors;
39  import org.apache.commons.collections.CollectionUtils;
40  
41  /**
42   * Validate with maven-duplicate-finder-plugin.
43   * @since 0.5
44   * @todo #1118 ignored dependencies and resources should be placed in different parameters,
45   *  and current implementation use ':' symbol as a flag if it is resource or dependency.
46   *  Resource can be presented as a regular expression with that symbol, can cause some problem.
47   */
48  public final class DuplicateFinderValidator implements MavenValidator {
49  
50      @Override
51      public void validate(final MavenEnvironment env)
52          throws ValidationException {
53          final String prefix = "duplicatefinder";
54          if (!env.exclude(prefix, "")) {
55              final Properties props = new Properties();
56              props.put("failBuildInCaseOfConflict", "true");
57              props.put("checkTestClasspath", "false");
58              props.put("useResultFile", "false");
59              props.put(
60                  "ignoredResourcePatterns",
61                  CollectionUtils.union(
62                      env.excludes(prefix).stream()
63                          .filter(s -> !s.contains(":"))
64                          .collect(Collectors.toList()),
65                      Arrays.asList("META-INF/.*", "module-info.class")
66                  )
67              );
68              final Collection<Properties> deps = new LinkedList<>();
69              for (final String sdep : env.excludes(prefix)) {
70                  final String[] parts = sdep.split(":");
71                  if (parts.length < 2) {
72                      continue;
73                  }
74                  final Properties main = new Properties();
75                  final Properties prop = new Properties();
76                  prop.put("groupId", parts[0]);
77                  prop.put("artifactId", parts[1]);
78                  if (parts.length > 2) {
79                      prop.put("version", parts[2]);
80                  }
81                  main.put("dependency", prop);
82                  deps.add(main);
83              }
84              props.put("ignoredDependencies", deps);
85              env.executor().execute(
86                  "org.basepom.maven:duplicate-finder-maven-plugin:2.0.1",
87                  "check",
88                  props
89              );
90          }
91      }
92  
93  }