From 24c6bc552c0ad6701300d50feacfb21d84deb765 Mon Sep 17 00:00:00 2001
From: Andy Gumbrecht <andy.is@gmx.de>
Date: Mon, 5 Aug 2019 10:30:50 +0200
Subject: [PATCH] Fix #139 - "Can't find license-maven-plugin"

Use the PluginDescriptor.
Most painful checkstyle.

Update IT test to use no build dep and full coordinates

Use project coordinates
---
 .../invoker.properties                        |  4 ++--
 src/it/add-third-party-no-encoding/pom.xml    | 13 -----------
 .../license/AggregatorAddThirdPartyMojo.java  | 22 ++++++++++++++++++-
 3 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/src/it/add-third-party-no-encoding/invoker.properties b/src/it/add-third-party-no-encoding/invoker.properties
index 09bf32591..a017991af 100644
--- a/src/it/add-third-party-no-encoding/invoker.properties
+++ b/src/it/add-third-party-no-encoding/invoker.properties
@@ -19,5 +19,5 @@
 # <http://www.gnu.org/licenses/lgpl-3.0.html>.
 # #L%
 ###
-invoker.goals=clean license:add-third-party
-invoker.failureBehavior=fail-fast
\ No newline at end of file
+invoker.goals=clean ${project.groupId}:${project.artifactId}:${project.version}:add-third-party
+invoker.failureBehavior=fail-fast
diff --git a/src/it/add-third-party-no-encoding/pom.xml b/src/it/add-third-party-no-encoding/pom.xml
index b6d0849a1..ca27c0635 100644
--- a/src/it/add-third-party-no-encoding/pom.xml
+++ b/src/it/add-third-party-no-encoding/pom.xml
@@ -45,19 +45,6 @@
       <version>1.1.1</version>
     </dependency>
   </dependencies>
-  <build>
-
-    <pluginManagement>
-      <plugins>
-
-        <plugin>
-          <groupId>org.codehaus.mojo</groupId>
-          <artifactId>license-maven-plugin</artifactId>
-          <version>@pom.version@</version>
-        </plugin>
-      </plugins>
-    </pluginManagement>
-  </build>
 
 </project>
 
diff --git a/src/main/java/org/codehaus/mojo/license/AggregatorAddThirdPartyMojo.java b/src/main/java/org/codehaus/mojo/license/AggregatorAddThirdPartyMojo.java
index e5a9acf53..2c5745e0c 100644
--- a/src/main/java/org/codehaus/mojo/license/AggregatorAddThirdPartyMojo.java
+++ b/src/main/java/org/codehaus/mojo/license/AggregatorAddThirdPartyMojo.java
@@ -33,6 +33,7 @@
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
@@ -203,11 +204,30 @@ protected void doAction()
         }
         if ( groupId == null )
         {
-            throw new IllegalStateException( "Can't find license-maven-plugin" );
+            try
+            {
+                final PluginDescriptor pd = ( PluginDescriptor ) getPluginContext().get( "pluginDescriptor" );
+                groupId = pd.getGroupId();
+                artifactId = pd.getArtifactId();
+                version = pd.getVersion();
+            }
+            catch ( ClassCastException e )
+            {
+                LOG.warn( "Failed to access PluginDescriptor", e );
+            }
+
+            if ( groupId == null )
+            {
+                throw new IllegalStateException( "Failed to determine the license-maven-plugin artifact."
+                    +
+                    "Please add it to your parent POM." );
+            }
         }
 
         String addThirdPartyRoleHint = groupId + ":" + artifactId + ":" + version + ":" + "add-third-party";
 
+        LOG.info( "The default plugin hint is: " + addThirdPartyRoleHint );
+
         for ( MavenProject reactorProject : reactorProjects )
         {
             if ( getProject().equals( reactorProject ) && !acceptPomPackaging )