【问题标题】:gradle to maven plugin conversiongradle 到 maven 插件的转换
【发布时间】:2016-01-28 02:20:19
【问题描述】:

如何为以下定义的 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)  
                    }  
                }  
            }  
        }  
    }  
}

【问题讨论】:

  • 你想把java插件转换成maven吗?
  • 是的..我可以在 maven pom 中用作插件。
  • 好吧,试试crunchify.com/…,如果我错了,请纠正我,告诉我你想怎么做,意思是你想在哪个 ide 做这个
  • 我正在使用 Eclispe ide,其中我的一个项目的 build.gradle 中有以下代码,用于运行 jive 测试用例。现在我需要为 build.gradle 生成一个 Maven pom.xml 文件,我需要将此插件转换为 maven 插件
  • 你想从 build.gradle 生成 poim.xml 吗?

标签: java maven gradle


【解决方案1】:

这真的取决于你到底想要达到什么目标。

如果你想帮助编写一个一般的 maven 插件,你必须read the documentation

如果您想过滤 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>

如文档所述http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

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

mvn test -DmyMavenProperty=theValueThatWillBePassedToTheTestJVMAsProperty_anotherProperty

您还可以使用 Surefire argline 将多个属性传递给 JVM。比如

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

并按如下方式执行maven

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

在这种情况下,您将在测试 JVM 中看到属性 foohello 的值分别为 barahoy

【讨论】:

  • 你好..关于这个问题,我有以下需要映射的属性: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 '] .... 那么我将如何获取 pom.xml 中特定属性名称的属性值(因为 System.getProperty 在这里没有帮助)?
猜你喜欢
  • 2018-01-17
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2017-01-24
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
相关资源
最近更新 更多