当前位置: 首页 > 知识库问答 >
问题:

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

羊和光
2023-03-14
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>
 <?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>

共有1个答案

吴欣悦
2023-03-14

你应该看看这里:

对于JUnit

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

 类似资料:
  • BFA插件似乎仍然使用jackson2-api插件中的jackson类,而不是直接依赖maven。导致https://issues.jenkins-ci.org/browse/jenkins-62214在MongoJack内部的导入中使用了错误的jackson版本,导致缺少方法: Jenkins的类路径中Jackson2-api插件的依赖性是否更高?

  • 我想创建一个自动应用其他插件(外部插件)的插件。这需要在调用“apply plugin”之前设置插件的buildscript依赖项。然而,我似乎无法在插件中添加buildscript依赖项,或者我得到:您无法更改未处于未解析状态的配置! 有解决办法吗? 我的示例(非工作)代码:

  • 我使用了约曼的“redux生成器”并安装了一些东西。 当我运行“npm start”时,项目将加载但显示此日志 错误/js/index。js模块构建失败:ReferenceError:插件“react.displayName”在PluginManager上与另一个同名插件冲突。在PluginManager上验证(/Users/acejordan/Projects/redcli/node_modul

  • 主要内容:如下表所示,Maven 提供了如下 2 种类型的插件。,插件目标,插件绑定Maven 实际上是一个依赖插件执行的框架,它执行的每个任务实际上都由插件完成的。Maven 的核心发布包中并不包含任何 Maven 插件,它们以独立构件的形式存在, 只有在 Maven 需要使用某个插件时,才会去仓库中下载。 如下表所示,Maven 提供了如下 2 种类型的插件。 插件类型 描述 Build plugins 在项目构建过程中执行,在 pom.xml 中的 build 元素中配置 

  • 问题内容: 据我了解,这不是通过使用想法插件(即通过调用)在Intellij中打开使用Maven构建的项目的最佳方法。 但是直接打开pom文件(Intellij具有Maven的默认插件);同样的东西,对于日食。 您能否提供一些关于为什么这是一种更好方法的论点? 问题答案: 这同样适用于IntelliJ和Maven: 更改文件未反映在您的IDE中,因此每次都必须重新生成项目(可能丢失某些配置(?))

  • 我试图在我的项目pom中使用配置文件。xml文件,用于控制将我的war文件部署到特定服务器。例如,我有一个本地配置文件和一个开发服务器配置文件。下面是我如何设置个人资料的 我打电话来 当我尝试此操作时,我可以看到,它不是尝试连接到我的配置中提供的IP地址,而是尝试连接到。我在本地包含用户名和密码的文件。 如果我然后添加选项,以获得更多信息,我可以在插件的调试输出中看到(强调部分已添加) 似乎没有遵