Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle to maven plugin conversion

Tags:

java

maven

gradle

How can I write equivalent maven plugin for the following gradle plugin defined?

/*
* Plugin to copy system properties from gradle JVM to testing JVM  
* Code was copied from gradle discussion froum:  
* http://forums.gradle.org/gradle/topics/passing_system_properties_to_test_task
*/  
class SystemPropertiesMappingPlugin implements Plugin{  
    public void apply(Project project){  
        project.tasks.withType(Test){ testTask ->  
            testTask.ext.mappedSystemProperties = []  
            doFirst{  
                mappedSystemProperties.each{mappedPropertyKey ->  
                    def systemPropertyValue = System.getProperty(mappedPropertyKey)  
                    if(systemPropertyValue){  
                        testTask.systemProperty(mappedPropertyKey, systemPropertyValue)  
                    }  
                }  
            }  
        }  
    }  
}
like image 644
sakshi garg Avatar asked Nov 10 '22 02:11

sakshi garg


1 Answers

It really depends on what exactly you want to achieve.

In case you want to help with writing a maven plugin in general, you'll have to read the documentation.

In case you want to filter system properties that Maven JVM passes to your test JVM, I don't see any other option than extending the maven-surefire-plugin plugin and add there an option to do such mapping. (Note that by default Maven passes all its System Properties to the test JVM.) That is definitely doable but maybe you can achieve your goal with something maven already offers.

You can definitely pass additional system properties to your test JVM from Maven by using:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.19</version>
     <configuration>
         <systemPropertyVariables>
              <propertyName>propertyValue</propertyName>
              <anotherProperty>${myMavenProperty}</buildDirectory>
         </systemPropertyVariables>
     </configuration>
</plugin>

as documented http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html.

In this case you can set the value of anotherProperty from command line by invoking maven

mvn test -DmyMavenProperty=theValueThatWillBePassedToTheTestJVMAsProperty_anotherProperty

You can also use Surefire argline to pass multiple properties to the JVM. For instance

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.19</version>
     <configuration>
         <argLine>${propertiesIWantToSetFromMvnCommandLine}</argLine>
     </configuration>
</plugin>

and execute maven as follows

mvn test -DpropertiesIWantToSetFromMvnCommandLine="-Dfoo=bar -Dhello=ahoy"

in this case, you'll see properties foo and hello with values bar and ahoy, respectively, in your test JVM.

like image 121
Stepan Vavra Avatar answered Nov 14 '22 21:11

Stepan Vavra