Maven插件-修改另一个插件的配置

Maven插件-修改另一个插件的配置,maven,maven-2,maven-plugin,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.mode

我正在开发自定义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;
    }


}
包io.kuku.agents.plugin;
导入org.apache.maven.model.Dependency;
导入org.apache.maven.model.model;
导入org.apache.maven.model.Plugin;
导入org.apache.maven.model.PluginExecution;
导入org.apache.maven.model.io.xpp3.MavenXpp3Writer;
导入org.apache.maven.plugin.MojoExecutionException;
导入org.apache.maven.plugin.MojoFailureException;
导入org.codehaus.plexus.util.xml.Xpp3Dom;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.List;
/**
*初始化与测试框架的集成。
*
*@相位测试
*@goal初始化测试侦听器
*@自1.0.0以来
*/
公共类SetupMojo扩展了AbstractKukusMojo{
布尔hasJunit=false;
布尔hasTestNG=false;
public void execute()抛出MojoExecutionException,MojoFailureException{
解析依赖项();
Plugin-surefirePlugin=lookupPlugin(“org.apache.maven.plugins:maven-surefire-Plugin”);
Object config=updateConfiguration(hasJunit、hasTestNG、surefirePlugin.getConfiguration());
surefirePlugin.setConfiguration(配置);
List executions=surefirePlugin.getExecutions();
for(PluginExecution执行:执行){
if(execution.getId().equals(“默认测试”)){
System.out.println(“设置默认测试”);
config=updateConfiguration(hasJunit、hasTestNG、execution.getConfiguration());
setConfiguration(config);
打破
}
}
}
私有void analyzeDependencies(){
列表依赖项=this.project.getDependencies();
对于(int i=0;i
我会感谢你的帮助

N

编辑-这是我的父母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>
 <?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>

4.0.0
李斯特试验
父工件
1.0-快照
聚甲醛
Sl侦听器测试(父级)
http://maven.apache.org
UTF-8
io.kuku.on-premise.agents.plugin
KukuMaven插件
1.0.0
纳达夫2
https://fake.kuku.co/api
假应用程序名
假模块名
${project.basedir}
52
假枝
*李斯特试验*
排除在外*
*.类
信息
c:\fake日志文件夹
真的
真的
a1
构建扫描仪
a2
测试侦听器
a3
初始化测试侦听器
org.apache.maven.plugins
maven surefire插件
2.19
默认测试
没有一个
追赶安特伦
测试
测试
org.apache.maven.plugins
maven surefire插件
2.19
默认测试
没有一个
追赶安特伦
测试
测试