今天,当我尝试sam build
在我的机器上为使用 Maven 构建的 Java 11 项目做 a 时,我遇到了同样的问题。
我相信问题是这样的:
- 当 Maven 构建失败时,Maven 会输出一些无效的 UTF-8 文本。这发生在我身上是因为单元测试失败了。
- SAM CLI 尝试在一些 Python 代码中捕获和修剪 Maven 输出,然后将其输出到控制台。当 Maven 输出不是干净的 UTF-8 文本时,这会分崩离析。
- 我们只能看到由无效 UTF-8 文本引起的 Python 代码错误。Maven 输出丢失。
我在 Google 中发现了一些其他 SAM 用户遇到类似问题的结果。不幸的是,解决方法涉及弄乱作为 SAM CLI 的一部分安装的 Python 源代码。我不喜欢这样做,但截至今天我没有找到更清洁的解决方案。
解决步骤:
使用以下命令在调试模式下执行您的 SAM 构建:
sam build --debug
在显示错误之前,您将看到 SAM 正在执行的操作的更多详细信息。
当构建失败时,记下发生错误的 Python 文件和行号。对我来说,它看起来像这样:
2021-11-19 09:41:30,430 | JavaMavenWorkflow:MavenBuild raised unhandled exception
Traceback (most recent call last):
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflow.py", line 278, in run
action.execute()
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\java_maven\actions.py", line 36, in execute
self.subprocess_maven.build(self.scratch_dir)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\java_maven\maven.py", line 31, in build
LOG.debug("Maven logs: %s", stdout.decode("utf8").strip())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 3795: invalid start byte
找到有问题的 Python 文件并在文本编辑器中打开它。对我来说,该文件是:
C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\java_maven\maven.py
注意:在 Windows 上,如果 SAM CLI 安装在“程序文件”下,您需要以管理员身份执行此步骤。
对我来说,这是导致错误的 Python 函数:
def build(self, scratch_dir):
args = ["clean", "install"]
ret_code, stdout, _ = self._run(args, scratch_dir)
LOG.debug("Maven logs: %s", stdout.decode("utf8").strip())
if ret_code != 0:
raise MavenExecutionError(message=stdout.decode("utf8").strip())
编辑代码并将“utf8”更改为不同的字符集代码。这个对我有用:
def build(self, scratch_dir):
args = ["clean", "install"]
ret_code, stdout, _ = self._run(args, scratch_dir)
LOG.debug("Maven logs: %s", stdout.decode("iso8859_2").strip())
if ret_code != 0:
raise MavenExecutionError(message=stdout.decode("iso8859_2").strip())
以下是 Python 编码的官方列表,以防您需要在自己的机器上尝试不同的编码:
https://docs.python.org/3/library/codecs.html#standard-encodings
我将我的编辑保存到 Python 文件中,然后sam build
再次运行。这次我得到了来自 Maven 的正常输出显示。
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] DataFileEntryTest.unmarshallJsonSampleFileWithUnknownProperties:57 Unexpected exception type thrown ==> expected: <com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException> but was: <java.io.FileNotFoundException>
[ERROR] Errors:
[ERROR] DataFileEntryTest.unmarshallJsonSampleFileWithMissingProperties:49 ť FileNotFound
[ERROR] DataFileEntryTest.unmarshallValidJsonSampleFile:33 ť FileNotFound ..\test-data...
[INFO]
[ERROR] Tests run: 4, Failures: 1, Errors: 2, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.979 s
[INFO] Finished at: 2021-11-19T09:48:55-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project hello-sam: There are test failures.
[ERROR]
[ERROR] Please refer to C:\Users\jtough\AppData\Local\Temp\tmpi8or6ls5\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[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/MojoFailureException