2016-05-02 4 views
0

Ich entwickle benutzerdefinierte Maven-Plugin. Mein Plugin benötigt eine spezifische Konfiguration für das surefire Plugin. Als Ergebnis suche ich als Teil meines MOJO nach 'surefire' und wenn es vorhanden ist, versuche ich seine Konfiguration zu ändern.Maven Plugin - Änderung der Konfiguration eines anderen Plugins

Mein Problem ist, dass die Konfiguration nicht verwendet wird. Hier die meisten meiner 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; 
    } 


} 

ich Ihre Hilfe zu schätzen wissen.

N.

EDIT - Dies ist meine Mutter 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> 

Dies ist der pom für eines der Kinder:

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

es das Plugin scheint auf Phase 'test' aufgerufen also wird es nach dem tod selbst aufgerufen. Versuchen Sie, vorher eine Phase einzustellen (wenn es Sinn macht). – Tunaki

+0

Soweit ich lesen konnte, bleibt die Änderung beim Setzen des Konfigurationsobjekts nur bestehen, wenn sich beide Plugins in derselben Lebenszyklusphase befinden. Damit meine Änderung wirksam wird, habe ich mein Plugin vor dem todsicheren gestellt, vorausgesetzt, dass es vorher ausgeführt wird. – nadavy

+0

Die Ausführung '" default-test "' wird zuerst aufgerufen, egal was in der Phase 'test'. Sie könnten versuchen, diese Standardausführung zu deaktivieren (mit dem Hacky ' keine'), eine Ausführung mit einer anderen ID ändern und sicherstellen, dass Ihr Plugin vor dem todsicheren Plugin im Pom deklariert ist. – Tunaki

Antwort

0
+0

Ich muss keinen einzigen Listener in meinem Projekt hinzufügen. Ich entwickle ein Produkt, bei dem ein Zuhörer arbeiten muss und ich möchte die Integration für den Endbenutzer erleichtern. – nadavy

Verwandte Themen