title: Maven Plugin Mojo Configure Default Parameter Values
body: <p>I have a maven plugin and a simple Mojo that looks somewhat close to </p>
  
  <pre><code>public abstract class AbstractSetupMojo extends AbstractMojo {
      @Parameter(property="targetHost", defaultValue="localhost") 
      private String targetHost;
      @Parameter(property="targetPort", defaultValue="27017")
      private Integer targetPort;
      @Parameter(property="targetDbName", required=true) 
      private String targetDbName;
      @Parameter(property="sourceHost", defaultValue="${mojo.configuration.targetHost}") 
      private String sourceHost;
      @Parameter(property="sourcePort", defaultValue="${mojo.configuration.targetPort}")
      private Integer sourcePort;
      @Parameter (property="sourceDbName", defaultValue="${mojo.configuration.targetDbName}")
      private String sourceDbName;
      @Parameter(property="scriptsPath")
      private File scriptsPath;
  }     
  </code></pre>
  
  <p>Other Mojos are extending this one. So, the idea is to set the <code>source*</code> parameters to the values of the corresponding <code>target*</code> parameters. Also some default values are set. </p>
  
  <p>In the test I have something like </p>
  
  <pre><code>public class GenerateMojoTest extends AbstractMojoTestCase {
  
      protected void setUp() throws Exception {
          super.setUp();
      }
  
      public void testConfiguration() throws Exception {
          File pom = getTestFile("src/test/resources/test-project-1/pom.xml");
          assertNotNull(pom);
          assertTrue(pom.exists());
          GenerateMojo generateMojo = (GenerateMojo)lookupMojo("generate", pom);
          assertThat(generateMojo.getSourceDbName()).isEqualTo(generateMojo.getTargetDbName());
          assertThat(generateMojo.getSourcePort()).isEqualTo(generateMojo.getTargetPort()).isEqualTo(27017);
          assertThat(generateMojo.getSourceHost()).isEqualTo(generateMojo.getTargetHost()).isEqualTo("localhost");
      }
  
  }
  </code></pre>
  
  <p>The  part of interest in the  POM file in the test looks  like </p>
  
  <pre><code>        &lt;plugin&gt;
              &lt;groupId&gt;com.ffy&lt;/groupId&gt;
              &lt;artifactId&gt;setup-maven-plugin&lt;/artifactId&gt;
      &lt;!--                
              &lt;configuration&gt;
                  &lt;scriptsPath&gt;src/test/resources/temp&lt;/scriptsPath&gt;
              &lt;/configuration&gt;
      --&gt;                
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;id&gt;generate&lt;/id&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;generate&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
  </code></pre>
  
  <p>The test fails because all the Mojo parameters are null if I keep <code>&lt;configuration&gt;</code> commented out, if I uncomment it then only <code>scriptsPath</code> is set. Other parameters are <code>null</code>.</p>
  
  <p>Do I have to do something else in my tests in order to have a fully configured Mojo ? </p>
  
  <p>I tried longer approach with </p>
  
  <pre><code>protected GenerateMojo setupMojo(final File pom) throws ComponentConfigurationException, Exception {
      final MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
      final ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest();
      final ProjectBuilder projectBuilder = this.lookup(ProjectBuilder.class);
      final MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();
      final MavenSession session = newMavenSession(project);
      final MojoExecution execution = newMojoExecution("generate");
      final GenerateMojo mojo = (GenerateMojo) this.lookupConfiguredMojo(session, execution);
      return mojo;
  }
  </code></pre>
  
  <p>instead of <code>lookupMojo</code> but that didn't change a bit. </p>