2

I'm developing custom maven plugin. My plugin requires a specific configuration for the surefire plugin. As result, as part of my MOJO I'm searching for 'surefire' and if it is present I'm trying to modify its configuration.

My problem is that the configuration is not used. Here's most of my 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<PluginExecution> 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 < dependencies.size(); i++) {
            Dependency dependency = (Dependency) dependencies.get(i);

            if (dependency.getArtifactId().equalsIgnoreCase("junit")) {
                hasJunit = true;
                if (hasJunit && hasTestNG)
                    break;
                else
                    continue;
            }

            if (dependency.getArtifactId().equalsIgnoreCase("testng")) {
                hasTestNG = true;
                if (hasJunit && 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;
    }


}

I'll appreciate your help.

N.

EDIT - This is my parent pom:

 <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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sllistenertest</groupId>
    <artifactId>parent-artifact</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>

    <name>Sl Listener Test (Parent)</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <pluginManagement>
            <plugins>

                <plugin>
                    <groupId>io.kuku.on-premise.agents.plugin</groupId>
                    <artifactId>kuku-maven-plugin</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <customerid>nadav2</customerid>
                        <server>https://fake.kuku.co/api</server>
                        <appName>fake-app-name</appName>
                        <moduleName>fake-module-name</moduleName>
                        <workspacepath>${project.basedir}</workspacepath>
                        <build>52</build>
                        <branch>fake-branch</branch>
                        <packagesincluded>*fklistenertest*</packagesincluded>
                        <packagesexcluded>com.fake.excluded.*</packagesexcluded>
                        <filesincluded>*.class</filesincluded>
                        <logLevel>INFO</logLevel>
                        <logFolder>c:\fake-log-folder</logFolder>
                        <logToFile>true</logToFile>
                        <logEnabled>true</logEnabled>
                    </configuration>
                    <executions>
                        <execution>
                            <id>a1</id>
                            <goals>
                                <goal>build-scanner</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>a2</id>
                            <goals>
                                <goal>test-listener</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>a3</id>
                            <goals>
                                <goal>initialize-test-listener</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19</version>
                    <!--<configuration>
                        <properties>
                            <property>
                                <name>listener</name>
                                <value>io.kuku.onpremise.agents.java.agent.integrations.testng.TestListener</value>
                            </property>
                        </properties>
                        <additionalClasspathElements>
                            <additionalClasspathElement>C:\Temp\kuku-java-1.3.160\artifacts\kuku-api-1.3.160.jar</additionalClasspathElement>
                        </additionalClasspathElements>
                    </configuration>-->
                    <executions>
                        <execution>
                            <id>default-test</id>
                            <phase>none</phase>
                        </execution>

                        <execution>
                            <id>run-after-antrun</id>
                            <phase>test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <executions>
                    <execution>
                        <id>default-test</id>
                        <phase>none</phase>
                    </execution>

                    <execution>
                        <id>run-after-antrun</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>


    <modules>
        <module>Only Junit</module>
        <module>Only TestNG</module>
        <module>Both</module>
    </modules>

</project>

This is the pom for one of the children:

 <?xml version="1.0"?>
<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>sllistenertest</groupId>
        <artifactId>parent-artifact</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>sllistenertest</groupId>
    <artifactId>onlyjunit</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Only JUnit</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>io.kuku.on-premise.agents.plugin</groupId>
                <artifactId>kuku-maven-plugin</artifactId>
                <version>1.0.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
            </plugin>

        </plugins>
    </build>

</project>
nadavy
  • 1,755
  • 1
  • 18
  • 33
  • It seems your plugin is invoked on phase `test` so it's invoked after surefire itself. Try to set a phase before that (if it makes sense). – Tunaki May 02 '16 at 09:20
  • As far as I was able to read, when setting the configuration object, the change will persist only if both of the plugins are in the same lifecycle phase. So, in order for my change to take effect, I've put my plugin before the surefire, assuming that will run before it. – nadavy May 02 '16 at 09:25
  • The execution `"default-test"` will be the first invoked no matter what in the phase `test`. You could try to disable that default execution (using the hacky `none`), modify an execution with another id and make sure your plugin is declared before the surefire plugin in the pom. – Tunaki May 02 '16 at 09:27
  • @Tunaki - not sure that I've understand you. You are saying that as part of the declaration on Surefire I need to declare on the 'default-test' execution with the phase 'none' since it is invoked before my plugin? – nadavy May 02 '16 at 09:56
  • Yes, something like this http://stackoverflow.com/a/31256929/1743880 – Tunaki May 02 '16 at 10:12
  • Still not working. Mind if I'll add my pom in you'll have a look? – nadavy May 02 '16 at 10:22
  • First what exactly are you trying to do? What is the intention for creating a plugin which seemed to related to surefire? And you should never change the configuration or depend on the configuration of an other plugin... – khmarbaise May 02 '16 at 10:23
  • @khmarbaise I'm trying to add a custom listener. – nadavy May 02 '16 at 10:28

1 Answers1

-1

You should take a look here:

for JUnit

https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html#Using_Custom_Listeners_and_Reporters

For TestNG: https://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html#Using_Custom_Listeners_and_Reporters

So i don't see the requirement to implement a plugin...

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • I don't need to add a single listener in my project. I'm developing a product which requires a listener to work and I want to ease the integration for the end-user. – nadavy May 02 '16 at 10:37