1515 */
1616package one .util .huntbugs .maven .plugin ;
1717
18+ import com .strobel .assembler .metadata .ClasspathTypeLoader ;
19+ import com .strobel .assembler .metadata .CompositeTypeLoader ;
20+ import com .strobel .assembler .metadata .ITypeLoader ;
21+ import com .strobel .assembler .metadata .JarTypeLoader ;
1822import one .util .huntbugs .analysis .AnalysisOptions ;
1923import one .util .huntbugs .analysis .Context ;
2024import one .util .huntbugs .analysis .HuntBugsResult ;
2529import one .util .huntbugs .repo .DirRepository ;
2630import one .util .huntbugs .repo .Repository ;
2731import one .util .huntbugs .warning .Warning ;
28-
2932import org .apache .maven .artifact .Artifact ;
3033import org .apache .maven .artifact .repository .ArtifactRepository ;
34+ import org .apache .maven .artifact .resolver .filter .ArtifactFilter ;
35+ import org .apache .maven .artifact .resolver .filter .ScopeArtifactFilter ;
3136import org .apache .maven .execution .MavenSession ;
3237import org .apache .maven .plugin .AbstractMojo ;
3338import org .apache .maven .plugin .MojoExecutionException ;
3439import org .apache .maven .plugin .MojoFailureException ;
40+ import org .apache .maven .plugins .annotations .Component ;
3541import org .apache .maven .plugins .annotations .LifecyclePhase ;
3642import org .apache .maven .plugins .annotations .Mojo ;
3743import org .apache .maven .plugins .annotations .Parameter ;
3844import org .apache .maven .project .MavenProject ;
39-
40- import com .strobel .assembler .metadata .ClasspathTypeLoader ;
41- import com .strobel .assembler .metadata .CompositeTypeLoader ;
42- import com .strobel .assembler .metadata .ITypeLoader ;
43- import com .strobel .assembler .metadata .JarTypeLoader ;
45+ import org .apache .maven .shared .dependency .tree .DependencyNode ;
46+ import org .apache .maven .shared .dependency .tree .DependencyTreeBuilder ;
47+ import org .apache .maven .shared .dependency .tree .DependencyTreeBuilderException ;
48+ import org .apache .maven .shared .dependency .tree .traversal .CollectingDependencyNodeVisitor ;
4449
4550import java .io .File ;
4651import java .io .IOException ;
4954import java .util .ArrayList ;
5055import java .util .Arrays ;
5156import java .util .List ;
52- import java .util .Set ;
5357import java .util .jar .JarFile ;
5458
5559/**
@@ -101,7 +105,10 @@ public class HuntBugsMojo extends AbstractMojo {
101105
102106 @ Parameter ( defaultValue = "${session}" , readonly = true , required = true )
103107 private MavenSession session ;
104-
108+
109+ @ Component
110+ private DependencyTreeBuilder treeBuilder ;
111+
105112 @ Override
106113 public void execute () throws MojoExecutionException {
107114 try {
@@ -125,26 +132,27 @@ private Repository constructRepository() throws IOException {
125132 getLog ().info ("HuntBugs: +dir " + classesDirectory );
126133 }
127134
135+ // collecting project dependencies including pom and transitive dependencies
136+ ArtifactFilter artifactFilter = new ScopeArtifactFilter ("compile" );
137+ DependencyNode rootNode ;
138+ try {
139+ rootNode = treeBuilder .buildDependencyTree (project , session .getLocalRepository (), artifactFilter );
140+ } catch (DependencyTreeBuilderException e ) {
141+ throw new RuntimeException (e );
142+ }
143+ CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor ();
144+ rootNode .accept (visitor );
145+
146+ // converting dependencies to type loaders
147+ List <DependencyNode > nodes = visitor .getNodes ();
128148 List <ITypeLoader > deps = new ArrayList <>();
129- ArtifactRepository localRepository = session .getLocalRepository ();
149+ for (DependencyNode dependencyNode : nodes ) {
150+ int state = dependencyNode .getState ();
130151
131- Set <Artifact > dependencyArtifacts = project .getDependencyArtifacts ();
132- if (dependencyArtifacts != null ) {
133- for (Artifact art : dependencyArtifacts ) {
134- if (art .getScope ().equals ("compile" )) {
135- File f = localRepository .find (art ).getFile ();
136- if (f != null ) {
137- Path path = f .toPath ();
138- if (!quiet ) {
139- getLog ().info ("HuntBugs: +dep " + path );
140- }
141- if (Files .isRegularFile (path ) && art .getType ().equals ("jar" )) {
142- deps .add (new JarTypeLoader (new JarFile (path .toFile ())));
143- } else if (Files .isDirectory (path )) {
144- deps .add (new ClasspathTypeLoader (path .toString ()));
145- }
146- }
147- }
152+ // checking that transitive dependency is NOT excluded
153+ if (state == DependencyNode .INCLUDED ) {
154+ Artifact artifact = dependencyNode .getArtifact ();
155+ addDependency (artifact , deps );
148156 }
149157 }
150158
@@ -155,7 +163,25 @@ private Repository constructRepository() throws IOException {
155163 return new CompositeRepository (
156164 Arrays .asList (repo , new AuxRepository (new CompositeTypeLoader (deps .toArray (new ITypeLoader [0 ])))));
157165 }
158-
166+
167+ private void addDependency (Artifact art , List <ITypeLoader > deps ) throws IOException {
168+ if ("compile" .equals (art .getScope ())) {
169+ ArtifactRepository localRepository = session .getLocalRepository ();
170+ File f = localRepository .find (art ).getFile ();
171+ if (f != null ) {
172+ Path path = f .toPath ();
173+ if (!quiet ) {
174+ getLog ().info ("HuntBugs: +dep " + path );
175+ }
176+ if (Files .isRegularFile (path ) && art .getType ().equals ("jar" )) {
177+ deps .add (new JarTypeLoader (new JarFile (path .toFile ())));
178+ } else if (Files .isDirectory (path )) {
179+ deps .add (new ClasspathTypeLoader (path .toString ()));
180+ }
181+ }
182+ }
183+ }
184+
159185 private AnalysisOptions constructOptions () {
160186 AnalysisOptions options = new AnalysisOptions ();
161187 options .minScore = minScore ;
0 commit comments