1 package org.apache.maven.plugins.jdeprscan;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
42
43
44
45
46 public abstract class BaseJDeprScanMojo extends AbstractJDeprScanMojo
47 {
48 @Parameter( defaultValue = "${project}", readonly = true, required = true )
49 private MavenProject project;
50
51
52
53
54 @Parameter( defaultValue = "true" )
55 private boolean failOnWarning;
56
57
58
59
60
61 @Parameter( property = "maven.jdeprscan.forremoval" )
62 private boolean forRemoval;
63
64
65
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 }