5

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)  
                    }  
                }  
            }  
        }  
    }  
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
sakshi garg
  • 141
  • 1
  • 7
  • you want to convert java plugin to maven right? – Ajay Pandya Oct 28 '15 at 10:45
  • Yeah.. which I can use in maven pom as a plugin. – sakshi garg Oct 28 '15 at 10:48
  • Okz try http://crunchify.com/how-to-convert-existing-java-project-to-maven-in-eclipse/ if i'm wrong please correct me with explain me how you want to do it imean in which ide you want to do this – Ajay Pandya Oct 28 '15 at 10:52
  • I am using Eclispe ide, where I have the following code in build.gradle of one of my projects which is being used to run jive test cases. Now I need to generate a Maven pom.xml file for the build.gradle for which I need to convert this plugin to maven plugin – sakshi garg Oct 28 '15 at 11:06
  • you want to generate poim.xml from build.gradle? – Ajay Pandya Oct 28 '15 at 11:23
  • Yes. I need to disable gradle nature and configure maven to build the project rather than gradle. – sakshi garg Oct 29 '15 at 04:39
  • have you look at http://stackoverflow.com/questions/12888490/gradle-build-gradle-to-maven-pom-xml?because i'm not good to write pom so i cant help you by code bt i can suggest as per my knowledge. – Ajay Pandya Oct 29 '15 at 05:09
  • Yes, but this just includes dependencies. Tasks are not included in the pom – sakshi garg Oct 29 '15 at 05:18
  • your question need attention of appropriate person for writing pom plugin by offer boundary – Ajay Pandya Oct 29 '15 at 06:24

1 Answers1

1

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.

Verhagen
  • 3,885
  • 26
  • 36
Stepan Vavra
  • 3,884
  • 5
  • 29
  • 40
  • Hello.. With reference to the question, I have following properties which needs to be mapped : mappedSystemProperties = ['jivetests', 'jive.suite.name', 'jive.package.base', 'jive.package.include', 'jive.package.exclude', 'jive.testclass.include', 'jive.testclass.exclude', 'jive.testclass.id.include', 'jive.testclass.id.exclude'] .... So how will I get the property value for a specific property name in pom.xml (as System.getProperty won't help here)? – sakshi garg Dec 07 '15 at 10:18