Maven插件 - 修改另一个插件

时间:2016-05-02 09:18:25

标签: maven maven-2 maven-plugin

我正在开发自定义maven插件。 我的插件需要surefire插件的特定配置。因此,作为我的MOJO的一部分,我正在搜索'surefire',如果它存在,我试图修改其配置。

我的问题是没有使用配置。 这是我的大部分代码:

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


}

我感谢你的帮助。

<磷>氮

编辑 - 这是我的父母:

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

这是其中一个孩子的pom:

 <?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>