View Javadoc
1   package org.apache.maven.plugins.jdeprscan;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.util.Collection;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.maven.artifact.DependencyResolutionRequiredException;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.MojoFailureException;
32  
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.plugins.jdeprscan.consumers.JDeprScanConsumer;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.plexus.util.StringUtils;
37  import org.codehaus.plexus.util.cli.Commandline;
38  import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer;
39  
40  /**
41   * Base class for all explicit jdeprscan mojos
42   * 
43   * @author Robert Scholte
44   * @since 3.0.0
45   */
46  public abstract class BaseJDeprScanMojo extends AbstractJDeprScanMojo
47  {
48      @Parameter( defaultValue = "${project}", readonly = true, required = true )
49      private MavenProject project;
50  
51      /**
52       * Indicates whether the build will continue even if there are jdeprscan warnings.
53       */
54      @Parameter( defaultValue = "true" )
55      private boolean failOnWarning;
56      
57      /**
58       * Limits scanning or listing to APIs that are deprecated for removal. 
59       * Can’t be used with a release value of 6, 7, or 8.
60       */
61      @Parameter( property = "maven.jdeprscan.forremoval" )
62      private boolean forRemoval;
63  
64      /**
65       * Specifies the Java SE release that provides the set of deprecated APIs for scanning.
66       */
67      @Parameter
68      private String release;
69      
70      private JDeprScanConsumer consumer = new JDeprScanConsumer();
71      
72      @Override
73      public void execute()
74          throws MojoExecutionException, MojoFailureException
75      {
76          if ( !Files.exists( getClassesDirectory() ) )
77          {
78              getLog().debug( "No classes to scan" );
79              return;
80          }
81          super.execute();
82      }
83  
84      protected MavenProject getProject()
85      {
86          return project;
87      }
88      
89      @Override
90      protected boolean isForRemoval()
91      {
92          return forRemoval;
93      }
94      
95      @Override
96      protected StringStreamConsumer getConsumer()
97      {
98          return consumer;
99      }
100     
101     @Override
102     protected final void addJDeprScanOptions( Commandline cmd ) throws MojoFailureException
103     {
104         super.addJDeprScanOptions( cmd );
105 
106         if ( release != null )
107         {
108             cmd.createArg().setValue( "--release" );
109 
110             cmd.createArg().setValue( release );
111         }
112 
113         try
114         {
115             Collection<Path> cp = getClassPath();
116             
117             if ( !cp.isEmpty() )
118             {
119                 cmd.createArg().setValue( "--class-path" );
120 
121                 cmd.createArg().setValue( StringUtils.join( cp.iterator(), File.pathSeparator ) );
122             }
123             
124         }
125         catch ( DependencyResolutionRequiredException e )
126         {
127             throw new MojoFailureException( e.getMessage(), e );
128         }
129         
130         cmd.createArg().setFile( getClassesDirectory().toFile() );
131     }
132 
133     @Override
134     protected void verify() throws MojoExecutionException
135     {
136         if ( !( consumer.getDeprecatedClasses().isEmpty() && consumer.getDeprecatedMethods().isEmpty() ) )
137         {
138             if ( !consumer.getDeprecatedClasses().isEmpty() )
139             {
140                 getLog().warn( "Found usage of deprecated classes:" );
141                 
142                 for ( Map.Entry<String, Set<String>> classes : consumer.getDeprecatedClasses().entrySet() )
143                 {
144                     getLog().warn( "class " + classes.getKey() + " uses deprecated class(es)" );
145                     for ( String deprClass : classes.getValue() )
146                     {
147                         getLog().warn( "  * " + deprClass );
148                     }
149                 }
150             }
151             
152             if ( !consumer.getDeprecatedMethods().isEmpty() )
153             {
154                 getLog().warn( "Found usage of deprecated methods:" );
155                 
156                 for ( Map.Entry<String, Set<String>> classes : consumer.getDeprecatedMethods().entrySet() )
157                 {
158                     getLog().warn( "class " + classes.getKey() + " uses deprecated method(s)" );
159                     for ( String deprMethod : classes.getValue() )
160                     {
161                         getLog().warn( "  * " + deprMethod );
162                     }
163                 }
164             }
165             
166             if ( failOnWarning )
167             {
168                 throw new MojoExecutionException( "JDeprScan detected usage of deprecated classes/methods" );
169             }
170         }
171     }
172 
173     protected abstract Path getClassesDirectory();
174     
175     protected abstract Collection<Path> getClassPath() throws DependencyResolutionRequiredException;
176 }