How to scan maven plugin dependency?

In my project I attached commons-lang3 to plugin and commons-io to project:

<build>
    <plugins>
        <plugin>
            <groupId>@project.groupId@</groupId>
            <artifactId>@project.artifactId@</artifactId>
            <version>@project.version@</version>
               (...)
            <dependencies>
                <dependency>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-lang3</artifactId>
                    <version>3.4</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
</dependencies>

Within my custom plugin (within my mojo), I can find commons-io

@Component
private MavenProject project;
(...)
project.getDependecies(); // [{groupId=commons-io, artifactId=commons-io...

How to find commons-lang3?

Answers


Using the Maven API, from the MavenProject you can get the Build instance and from it then the list of configured plugins, from each Plugin object you can then have its dependencies as following:

@Component
private PluginDescriptor pluginDescriptor;
(...)

List<Plugin> plugins = project.getBuild().getPlugins();
for (Plugin p : plugins) {
    if (p.getId().equals(pluginDescriptor.getId())) {
       List<Dependency> pluginDependencies = p.getDependencies();
       // your logic here
       break;
    }
}

If you really want to cover every case, you can also scan the plugins configured in the currently active profiles as following:

List<Profile> profiles = project.getActiveProfiles();
for (Profile p : profiles) {
   // from personal experience, don't forget this check!
   if (p.getBuild() != null) {
        checkAsShownAbove(p.getBuild().getPlugins());
   }
}

Hope that helps.


As commons-lang3 is declared as a dependency only for a specific plugin, it is not available during compilation. You have to explicitly define commons-lang3 as a dependency (if its not getting included as a transitive dependency) similar to how you have defined commons-io.


Need Your Help

Get a list of available domains (NT4 and Active Directory)

c# active-directory nt4

Does anyone know (in c#) a way of getting the available NT4 domains (a bit like the WinXP login box dropdown)?

Getting Image size of JPEG from its binary

file binary jpeg

I have a lot of jpeg files with varying image size. For instance, here is the first 640 bytes as given by hexdump of an image of size 256*384(pixels):