View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Base class for all explicit jdeprscan mojos
41   *
42   * @author Robert Scholte
43   * @since 3.0.0
44   */
45  public abstract class BaseJDeprScanMojo extends AbstractJDeprScanMojo {
46      @Parameter(defaultValue = "${project}", readonly = true, required = true)
47      private MavenProject project;
48  
49      /**
50       * Indicates whether the build will continue even if there are jdeprscan warnings.
51       */
52      @Parameter(defaultValue = "true")
53      private boolean failOnWarning;
54  
55      /**
56       * Limits scanning or listing to APIs that are deprecated for removal.
57       * Can’t be used with a release value of 6, 7, or 8.
58       */
59      @Parameter(property = "maven.jdeprscan.forremoval")
60      private boolean forRemoval;
61  
62      /**
63       * Specifies the Java SE release that provides the set of deprecated APIs for scanning.
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 }