Maven Plugin Mojo Configuring Default Parameter Values

I have a maven plugin and a simple Mojo that is somewhat close to

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; } 

Other Mojos extend this one. So, the idea is to set the source* parameters to the values ​​of the corresponding target* parameters. Some default values ​​are also set.

In the test, I have something like

 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"); } } 

Part of the interest in the POM file in the test looks like

  <plugin> <groupId>com.ffy</groupId> <artifactId>setup-maven-plugin</artifactId> <!-- <configuration> <scriptsPath>src/test/resources/temp</scriptsPath> </configuration> --> <executions> <execution> <id>generate</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> 

The test fails because all Mojo parameters are null, if I keep <configuration> commented out, if I uncomment it, then only scriptsPath . Other parameters are null .

Do I need to do something in my tests in order to have a fully configured Mojo?

I tried a longer approach with

 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; } 

instead of lookupMojo , but that has changed a bit.

+5
source share
1 answer

You will need to have the <configuration/> and determine the values ​​that interest you. You expect this to be a little smarter, but actually what the test harness does is read the values ​​from <configuration/> and ignore those from your annotations. The rigidity of testing leaves much to be desired, it actually does not load values ​​for you, like Maven's proper execution / interpolation ... Therefore, I would advise you to use maven-invoker-plugin if it suits your needs better.

+5
source

Source: https://habr.com/ru/post/1239838/


All Articles