Maven 中的自定义生命周期中断 Surefire:test

Custom Lifecycle breaks Surefire:test in Maven

我有一个插件可以在构建过程中创建一个特殊的 zip 文件。
为此,它定义了一个自定义包装类型 'wcc' 和一个自定义生命周期。
当我 运行 mvn package 应用程序构建我的 zip 文件时一切正常。
注意: 跳过 单元测试来做到这一点。)

然而,当我 运行 mvn test 它失败了。

并吐出以下日志:

[ERROR] Failed to execute goal 
org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test 
(default-test) on project LowesMMU: 
  A type incompatibility occured while executing 
   org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test: 
      java.io.File cannot be cast to java.lang.String
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>org.apache.maven.plugins:maven-surefire-plugin:2.19.1
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/.m2/repository/org/apache/maven/plugins/maven-surefire-plugin/2.19.1/maven-surefire-plugin-2.19.1.jar
[ERROR] urls[1] = file:/.m2/repository/org/apache/maven/surefire/maven-surefire-common/2.19.1/maven-surefire-common-2.19.1.jar
[ERROR] urls[2] = file:/.m2/repository/org/apache/maven/surefire/surefire-booter/2.19.1/surefire-booter-2.19.1.jar
[ERROR] urls[3] = file:/.m2/repository/org/codehaus/plexus/plexus-utils/1.5.15/plexus-utils-1.5.15.jar
[ERROR] urls[4] = file:/.m2/repository/junit/junit/4.12/junit-4.12.jar
[ERROR] urls[5] = file:/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
[ERROR] urls[6] = file:/.m2/repository/backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar
[ERROR] urls[7] = file:/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.11/plexus-interpolation-1.11.jar
[ERROR] urls[8] = file:/.m2/repository/org/slf4j/slf4j-jdk14/1.5.6/slf4j-jdk14-1.5.6.jar
[ERROR] urls[9] = file:/.m2/repository/org/slf4j/slf4j-api/1.5.6/slf4j-api-1.5.6.jar
[ERROR] urls[10] = file:/.m2/repository/org/slf4j/jcl-over-slf4j/1.5.6/jcl-over-slf4j-1.5.6.jar
[ERROR] urls[11] = file:/.m2/repository/org/apache/maven/reporting/maven-reporting-api/3.0/maven-reporting-api-3.0.jar
[ERROR] urls[12] = file:/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.3/plexus-sec-dispatcher-1.3.jar
[ERROR] urls[13] = file:/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar
[ERROR] urls[14] = file:/.m2/repository/org/apache/commons/commons-lang3/3.1/commons-lang3-3.1.jar
[ERROR] urls[15] = file:/.m2/repository/org/apache/maven/surefire/surefire-api/2.19.1/surefire-api-2.19.1.jar
[ERROR] urls[16] = file:/.m2/repository/org/apache/maven/plugin-tools/maven-plugin-annotations/3.3/maven-plugin-annotations-3.3.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import  from realm ClassRealm[project>com.lowes.ecm:LowesMMU:1.0.0-SNAPSHOT, parent: ClassRealm[maven.api, parent: null]]]
[ERROR] 
[ERROR] -----------------------------------------------------
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

如果我将 <packaging>wcc 更改为 jar,那么我的测试 运行 并顺利通过。

那么为什么我会收到此错误,更重要的是我该如何解决它?


进一步调查

Maven surefire 插件仅支持 java.lang.String 属性。 我的插件有一对 java.io.File 属性。

@Parameter(property = "componentZip", defaultValue = "${componentLocation}")
protected File componentZip;

@Parameter(property = "manifestFile", defaultValue = "${project.basedir}/manifest.hda")
protected File manifestFile;

我的插件使用这些属性来打包 zip 文件, 以及适当地打包文件并确保它以预期的格式构建。

如何让 surefire 忽略这些配置属性?我是否需要将它们设为 String 并在我的插件中以这种方式使用 File 添加转换?

我找到了我的问题。我为 initialize 阶段定义了一个 Mojo。它正在处理我的配置并设置默认值。它设置的值之一是 componentZip。为了确保它迁移到后面的阶段,它将它存储在 Properties、UserProperties 和 SystemProperties 中。转换它,并将属性更新代码转换为 String 解决了我的问题。