1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.jdeprscan;
20
21 import java.io.File;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.util.Collection;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.maven.artifact.DependencyResolutionRequiredException;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.maven.plugins.jdeprscan.consumers.JDeprScanConsumer;
33 import org.apache.maven.project.MavenProject;
34 import org.apache.maven.toolchain.ToolchainManager;
35 import org.codehaus.plexus.util.StringUtils;
36 import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer;
37 import org.codehaus.plexus.util.cli.Commandline;
38
39
40
41
42
43
44
45 public abstract class BaseJDeprScanMojo extends AbstractJDeprScanMojo {
46 @Parameter(defaultValue = "${project}", readonly = true, required = true)
47 private MavenProject project;
48
49
50
51
52 @Parameter(defaultValue = "true")
53 private boolean failOnWarning;
54
55
56
57
58
59 @Parameter(property = "maven.jdeprscan.forremoval")
60 private boolean forRemoval;
61
62
63
64
65 @Parameter
66 private String release;
67
68 private final JDeprScanConsumer consumer = new JDeprScanConsumer();
69
70 protected BaseJDeprScanMojo(ToolchainManager toolchainManager) {
71 super(toolchainManager);
72 }
73
74 @Override
75 public void execute() throws MojoExecutionException, MojoFailureException {
76 if (!Files.exists(getClassesDirectory())) {
77 getLog().debug("No classes to scan");
78 return;
79 }
80 super.execute();
81 }
82
83 protected MavenProject getProject() {
84 return project;
85 }
86
87 @Override
88 protected boolean isForRemoval() {
89 return forRemoval;
90 }
91
92 @Override
93 protected StringStreamConsumer getConsumer() {
94 return consumer;
95 }
96
97 @Override
98 protected final void addJDeprScanOptions(Commandline cmd) throws MojoFailureException {
99 super.addJDeprScanOptions(cmd);
100
101 if (release != null) {
102 cmd.createArg().setValue("--release");
103
104 cmd.createArg().setValue(release);
105 }
106
107 try {
108 Collection<Path> cp = getClassPath();
109
110 if (!cp.isEmpty()) {
111 cmd.createArg().setValue("--class-path");
112
113 cmd.createArg().setValue(StringUtils.join(cp.iterator(), File.pathSeparator));
114 }
115
116 } catch (DependencyResolutionRequiredException e) {
117 throw new MojoFailureException(e.getMessage(), e);
118 }
119
120 cmd.createArg().setFile(getClassesDirectory().toFile());
121 }
122
123 @Override
124 protected void verify() throws MojoExecutionException {
125 if (!(consumer.getDeprecatedClasses().isEmpty()
126 && consumer.getDeprecatedMethods().isEmpty())) {
127 if (!consumer.getDeprecatedClasses().isEmpty()) {
128 getLog().warn("Found usage of deprecated classes:");
129
130 for (Map.Entry<String, Set<String>> classes :
131 consumer.getDeprecatedClasses().entrySet()) {
132 getLog().warn("class " + classes.getKey() + " uses deprecated class(es)");
133 for (String deprClass : classes.getValue()) {
134 getLog().warn(" * " + deprClass);
135 }
136 }
137 }
138
139 if (!consumer.getDeprecatedMethods().isEmpty()) {
140 getLog().warn("Found usage of deprecated methods:");
141
142 for (Map.Entry<String, Set<String>> classes :
143 consumer.getDeprecatedMethods().entrySet()) {
144 getLog().warn("class " + classes.getKey() + " uses deprecated method(s)");
145 for (String deprMethod : classes.getValue()) {
146 getLog().warn(" * " + deprMethod);
147 }
148 }
149 }
150
151 if (failOnWarning) {
152 throw new MojoExecutionException("JDeprScan detected usage of deprecated classes/methods");
153 }
154 }
155 }
156
157 protected abstract Path getClassesDirectory();
158
159 protected abstract Collection<Path> getClassPath() throws DependencyResolutionRequiredException;
160 }