gradle to maven插件转换

sak*_*arg 5 java gradle maven

如何为定义的以下gradle插件编写等效的maven插件?

/*
* 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)  
                    }  
                }  
            }  
        }  
    }  
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*vra 1

这实际上取决于您到底想要实现什么。

如果您想帮助编写 Maven 插件,则必须阅读文档

如果您想过滤 Maven JVM 传递到测试 JVM 的系统属性,除了扩展插件maven-surefire-plugin并添加一个选项来执行此类映射之外,我没有看到任何其他选项。(请注意,默认情况下 Maven 将其所有系统属性传递给测试 JVM。)这绝对是可行的,但也许您可以使用 Maven 已经提供的东西来实现您的目标。

您绝对可以使用以下命令将其他系统属性从 Maven 传递到您的测试 JVM:

<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>
Run Code Online (Sandbox Code Playgroud)

如记录的http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

anotherProperty在这种情况下,您可以通过调用 maven 从命令行设置值

mvn test -DmyMavenProperty=theValueThatWillBePassedToTheTestJVMAsProperty_anotherProperty
Run Code Online (Sandbox Code Playgroud)

您还可以使用 Surefireargline将多个属性传递给 JVM。例如

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.19</version>
     <configuration>
         <argLine>${propertiesIWantToSetFromMvnCommandLine}</argLine>
     </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

并执行maven如下

mvn test -DpropertiesIWantToSetFromMvnCommandLine="-Dfoo=bar -Dhello=ahoy"
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您将在测试 JVM 中分别看到属性foohello以及值bar和。ahoy