title: Maven plugin - modifying configuration of another plugin
body: <p>I'm developing custom maven plugin.
  My plugin requires a specific configuration for the <code>surefire</code> plugin. As result, as part of my MOJO I'm searching for <code>'surefire'</code> and if it is present I'm trying to modify its configuration.</p>
  
  <p>My problem is that the configuration is not used.
  Here's most of my code:</p>
  
  <pre><code>package io.kuku.agents.plugin;
  
  import org.apache.maven.model.Dependency;
  import org.apache.maven.model.Model;
  import org.apache.maven.model.Plugin;
  import org.apache.maven.model.PluginExecution;
  import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
  import org.apache.maven.plugin.MojoExecutionException;
  import org.apache.maven.plugin.MojoFailureException;
  import org.codehaus.plexus.util.xml.Xpp3Dom;
  
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.List;
  
  
  /**
   * Initialize the integration with the Testing Framework.
   *
   * @phase test
   * @goal initialize-test-listener
   * @since 1.0.0
   */
  
  
  public class SetupMojo extends AbstractKukusMojo {
  
      boolean hasJunit = false;
      boolean hasTestNG = false;
  
      public void execute() throws MojoExecutionException, MojoFailureException {
          analyzeDependencies();
  
  
          Plugin surefirePlugin = lookupPlugin("org.apache.maven.plugins:maven-surefire-plugin");
          Object config = updateConfiguration(hasJunit, hasTestNG, surefirePlugin.getConfiguration());
          surefirePlugin.setConfiguration(config);
  
          List&lt;PluginExecution&gt; executions = surefirePlugin.getExecutions();
          for (PluginExecution execution : executions) {
              if (execution.getId().equals("default-test")) {
                  System.out.println("Setting DEFAULT-TEST");
                  config = updateConfiguration(hasJunit, hasTestNG, execution.getConfiguration());
                  execution.setConfiguration(config);
                  break;
              }
          }
      }
  
      private void analyzeDependencies() {
          List dependencies = this.project.getDependencies();
  
          for (int i = 0; i &lt; dependencies.size(); i++) {
              Dependency dependency = (Dependency) dependencies.get(i);
  
              if (dependency.getArtifactId().equalsIgnoreCase("junit")) {
                  hasJunit = true;
                  if (hasJunit &amp;&amp; hasTestNG)
                      break;
                  else
                      continue;
              }
  
              if (dependency.getArtifactId().equalsIgnoreCase("testng")) {
                  hasTestNG = true;
                  if (hasJunit &amp;&amp; hasTestNG)
                      break;
                  else
                      continue;
              }
  
          }
      }
  
      private Object updateConfiguration(boolean hasJunit, boolean hasTestNG, Object configuration) throws MojoExecutionException {
  
          if (configuration == null)
              configuration = new Xpp3Dom("configuration");
  
          if (configuration instanceof Xpp3Dom) {
  
              Xpp3Dom xml = (Xpp3Dom) configuration;
              Xpp3Dom properties = xml.getChild("properties");
              if (properties == null)
              {
                  properties = new Xpp3Dom("properties");
                  xml.addChild(properties);
              }
              Xpp3Dom[] property = properties.getChildren("property");
  
              //My logic goes here
              ...
              ...
          }
          return configuration;
      }
  
  
  }
  </code></pre>
  
  <p>I'll appreciate your help.</p>
  
  <p>N.</p>
  
  <p><strong>EDIT - This is my parent pom:</strong></p>
  
  <pre><code> &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;sllistenertest&lt;/groupId&gt;
      &lt;artifactId&gt;parent-artifact&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;packaging&gt;pom&lt;/packaging&gt;
  
      &lt;name&gt;Sl Listener Test (Parent)&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;/properties&gt;
  
      &lt;build&gt;
          &lt;pluginManagement&gt;
              &lt;plugins&gt;
  
                  &lt;plugin&gt;
                      &lt;groupId&gt;io.kuku.on-premise.agents.plugin&lt;/groupId&gt;
                      &lt;artifactId&gt;kuku-maven-plugin&lt;/artifactId&gt;
                      &lt;version&gt;1.0.0&lt;/version&gt;
                      &lt;configuration&gt;
                          &lt;customerid&gt;nadav2&lt;/customerid&gt;
                          &lt;server&gt;https://fake.kuku.co/api&lt;/server&gt;
                          &lt;appName&gt;fake-app-name&lt;/appName&gt;
                          &lt;moduleName&gt;fake-module-name&lt;/moduleName&gt;
                          &lt;workspacepath&gt;${project.basedir}&lt;/workspacepath&gt;
                          &lt;build&gt;52&lt;/build&gt;
                          &lt;branch&gt;fake-branch&lt;/branch&gt;
                          &lt;packagesincluded&gt;*fklistenertest*&lt;/packagesincluded&gt;
                          &lt;packagesexcluded&gt;com.fake.excluded.*&lt;/packagesexcluded&gt;
                          &lt;filesincluded&gt;*.class&lt;/filesincluded&gt;
                          &lt;logLevel&gt;INFO&lt;/logLevel&gt;
                          &lt;logFolder&gt;c:\fake-log-folder&lt;/logFolder&gt;
                          &lt;logToFile&gt;true&lt;/logToFile&gt;
                          &lt;logEnabled&gt;true&lt;/logEnabled&gt;
                      &lt;/configuration&gt;
                      &lt;executions&gt;
                          &lt;execution&gt;
                              &lt;id&gt;a1&lt;/id&gt;
                              &lt;goals&gt;
                                  &lt;goal&gt;build-scanner&lt;/goal&gt;
                              &lt;/goals&gt;
                          &lt;/execution&gt;
                          &lt;execution&gt;
                              &lt;id&gt;a2&lt;/id&gt;
                              &lt;goals&gt;
                                  &lt;goal&gt;test-listener&lt;/goal&gt;
                              &lt;/goals&gt;
                          &lt;/execution&gt;
                          &lt;execution&gt;
                              &lt;id&gt;a3&lt;/id&gt;
                              &lt;goals&gt;
                                  &lt;goal&gt;initialize-test-listener&lt;/goal&gt;
                              &lt;/goals&gt;
                          &lt;/execution&gt;
                      &lt;/executions&gt;
                  &lt;/plugin&gt;
                  &lt;plugin&gt;
                      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                      &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                      &lt;version&gt;2.19&lt;/version&gt;
                      &lt;!--&lt;configuration&gt;
                          &lt;properties&gt;
                              &lt;property&gt;
                                  &lt;name&gt;listener&lt;/name&gt;
                                  &lt;value&gt;io.kuku.onpremise.agents.java.agent.integrations.testng.TestListener&lt;/value&gt;
                              &lt;/property&gt;
                          &lt;/properties&gt;
                          &lt;additionalClasspathElements&gt;
                              &lt;additionalClasspathElement&gt;C:\Temp\kuku-java-1.3.160\artifacts\kuku-api-1.3.160.jar&lt;/additionalClasspathElement&gt;
                          &lt;/additionalClasspathElements&gt;
                      &lt;/configuration&gt;--&gt;
                      &lt;executions&gt;
                          &lt;execution&gt;
                              &lt;id&gt;default-test&lt;/id&gt;
                              &lt;phase&gt;none&lt;/phase&gt;
                          &lt;/execution&gt;
  
                          &lt;execution&gt;
                              &lt;id&gt;run-after-antrun&lt;/id&gt;
                              &lt;phase&gt;test&lt;/phase&gt;
                              &lt;goals&gt;
                                  &lt;goal&gt;test&lt;/goal&gt;
                              &lt;/goals&gt;
                          &lt;/execution&gt;
                      &lt;/executions&gt;
                  &lt;/plugin&gt;
              &lt;/plugins&gt;
          &lt;/pluginManagement&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.19&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;default-test&lt;/id&gt;
                          &lt;phase&gt;none&lt;/phase&gt;
                      &lt;/execution&gt;
  
                      &lt;execution&gt;
                          &lt;id&gt;run-after-antrun&lt;/id&gt;
                          &lt;phase&gt;test&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;test&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
  
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  
      &lt;modules&gt;
          &lt;module&gt;Only Junit&lt;/module&gt;
          &lt;module&gt;Only TestNG&lt;/module&gt;
          &lt;module&gt;Both&lt;/module&gt;
      &lt;/modules&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>This is the pom for one of the children:</strong></p>
  
  <pre><code> &lt;?xml version="1.0"?&gt;
  &lt;project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
           xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;sllistenertest&lt;/groupId&gt;
          &lt;artifactId&gt;parent-artifact&lt;/artifactId&gt;
          &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;sllistenertest&lt;/groupId&gt;
      &lt;artifactId&gt;onlyjunit&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;Only JUnit&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.12&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;io.kuku.on-premise.agents.plugin&lt;/groupId&gt;
                  &lt;artifactId&gt;kuku-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.0.0&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.19&lt;/version&gt;
              &lt;/plugin&gt;
  
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>