我今天在尝试做一个时遇到了同样的问题sam build
在我的机器上使用 Maven 构建的 Java 11 项目。
我相信问题是这样的:
- 当 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 安装在“Program Files”下,则您需要以管理员身份执行此步骤。
对我来说,这是错误来源的 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 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