使用ml-gradle部署代码遇TS2307错误,需排除测试TS文件
问题:部署MarkLogic代码时TS2307错误(找不到'marklogic-test-harness'模块)
场景
- 环境:MarkLogic Server 10.0-9.4
- 操作:通过指定
build.gradle部署代码,目标是排除src/test/rule10.test.ts等测试TS文件(依赖缺失且非必需) - 执行命令:
./gradlew -b build.gradle loadRules -i -PmlUsername="" -PmlPassword="" -PmlHost="localhost"
错误日志
> Task :npm_run_build Caching disabled for task ':npm_run_build' because: Build cache is disabled Task ':npm_run_build' is not up-to-date because: Task has not declared any outputs despite executing actions. Starting process 'command '/var/opt/redaction/.gradle/npm/npm-v6.4.1/bin/npm''. Working directory: /var/opt/redaction Command: /var/opt/redaction/.gradle/npm/npm-v6.4.1/bin/npm run build --loglevel silent Successfully started process 'command '/var/opt/redaction/.gradle/npm/npm-v6.4.1/bin/npm'' src/test/rule12.test.ts(3,38): error TS2307: Cannot find module 'marklogic-test-harness'. src/test/rule11.test.ts(3,33): error TS2307: Cannot find module 'marklogic-test-harness'. src/test/rule10.test.ts(2,38): error TS2307: Cannot find module 'marklogic-test-harness'. src/test/test7.ts(3,33): error TS2307: Cannot find module 'marklogic-test-harness'. src/test/test5.ts(3,36): error TS2307: Cannot find module 'marklogic-test-harness'. src/test/rule3.test.ts(3,33): error TS2307: Cannot find module 'marklogic-test-harness'. src/test/rule2.test.ts(2,38): error TS2307: Cannot find module 'marklogic-test-harness'. src/test/rule.test.ts(3,33): error TS2307: Cannot find module 'marklogic-test-harness'. src/test/rule1.test.ts(3,33): error TS2307: Cannot find module 'marklogic-test-harness'. > Task :npm_run_build FAILED :npm_run_build (Thread[Execution worker for ':',5,main]) completed. Took 3.295 secs.
当前build.gradle配置
plugins { // node // id "com.moowork.node" version "1.1.1" // ML-Gradle will handle the marklogic configuration id 'com.marklogic.ml-gradle' version '4.1.0' id "com.github.node-gradle.node" version "3.1.1" } // Set up extra properties to configure ml-gradle ext { // set up the default IDDN databases mlAppConfig { contentDatabaseName = 'data1' schemasDatabaseName = 'Schemas' modulesDatabaseName = 'Modules' triggersDatabaseName = 'Triggers' } mlAppDeployer.getCommand('DeployCustomForestsCommand').setCustomForestsPath("forests") // mlAppDeployer.getCommands().remove(mlAppDeployer.getCommand("DeployRolesCommand")) // do not let ml-gradle create a default rest server - we will only use the json config files mlAppDeployer.getCommands().remove(mlAppDeployer.getCommand('DeployRestApiServersCommand')) } repositories { mavenCentral() // Needed for mlcp dependencies maven { url 'https://developer.marklogic.com/maven2/' } } configurations { mlcp } dependencies { mlcp 'com.marklogic:mlcp:10.0.9.2' mlcp files('lib') } // Control nodejs and NPM build tasks via gradle node { // Version of node to use. version = '10.14.1' npmVersion = '6.4.1' download = true } // do not spam NPM error messages, the tools' own messages are fine. npm_run_clean { args = ['--loglevel', 'silent'] } npm_run_build { args = ['--loglevel', 'silent'] } npm_run_test { args = ['--loglevel', 'silent'] } task loadJSON(type: com.marklogic.gradle.task.MlcpTask, dependsOn: []) { classpath = configurations.mlcp command = 'IMPORT' database = 'Modules' input_file_path = './src/main/ml-modules/root/a1.json' document_type = 'json' output_uri_replace = "^.*root,''" } // temp workaround to facilitate ns and collections task loadJS(type: com.marklogic.gradle.task.MlcpTask, dependsOn: [npm_run_clean, npm_run_build]) { classpath = configurations.mlcp command = 'IMPORT' database = 'Modules' input_file_path = './src/main/ml-modules/root' document_type = 'text' output_uri_replace = "^.*root,''" } task loadRules(type: com.marklogic.gradle.task.MlcpTask, dependsOn: [mlClearModulesDatabase, mlClearSchemasDatabase, mlLoadSchemas, loadJS, loadJSON]) { classpath = configurations.mlcp command = 'IMPORT' database = 'Modules' input_file_path = './src/main/ml-modules/root' document_type = 'text' output_uri_replace = "^.*root,''" output_uri_suffix = ".sjs" } mlLoadSchemas.mustRunAfter(mlClearSchemasDatabase) npm_run_build{}.mustRunAfter(npm_run_clean) loadJS.mustRunAfter(npm_run_build) loadJS.mustRunAfter(mlClearModulesDatabase) loadJSON.mustRunAfter(loadJS) loadRules.mustRunAfter(loadJS)
解决方法
原因分析
npm_run_build任务执行时,TypeScript编译包含了src/test目录下的测试文件,这些文件依赖未安装的marklogic-test-harness模块,导致编译失败。需要让构建过程排除测试文件。
方案1:修改tsconfig.json排除测试文件
在项目根目录的tsconfig.json中添加exclude字段,跳过测试文件编译:
{ "compilerOptions": { // 保留原有编译配置 }, "exclude": ["src/test/**/*.ts"] }
方案2:调整Gradle的npm_build任务,指定仅编译源码
修改build.gradle中的npm_run_build配置,传递参数让npm只处理src/main目录下的代码(具体参数需匹配项目package.json中build脚本的逻辑,以下示例针对tsc编译):
npm_run_build { args = ['--loglevel', 'silent', '--', '--project', './src/main/tsconfig.json'] }
方案3:拆分构建任务,使用生产专用构建脚本
- 在项目
package.json中添加生产构建脚本,例如:
"scripts": { "build": "tsc", "build:prod": "tsc --project src/main/tsconfig.json" }
- 修改
build.gradle,新增生产构建任务并替换原有依赖:
task npm_run_build_prod(type: NpmTask) { args = ['run', 'build:prod', '--loglevel', 'silent'] } npm_run_build_prod.mustRunAfter(npm_run_clean) task loadJS(type: com.marklogic.gradle.task.MlcpTask, dependsOn: [npm_run_clean, npm_run_build_prod]) { classpath = configurations.mlcp command = 'IMPORT' database = 'Modules' input_file_path = './src/main/ml-modules/root' document_type = 'text' output_uri_replace = "^.*root,''" }
方案4:在MLCP任务中排除测试文件(防止误导入)
如果构建后测试文件仍被打包到导入目录,可在loadJS或loadRules任务中添加文件过滤规则:
task loadJS(type: com.marklogic.gradle.task.MlcpTask, dependsOn: [npm_run_clean, npm_run_build]) { classpath = configurations.mlcp command = 'IMPORT' database = 'Modules' input_file_path = './src/main/ml-modules/root' document_type = 'text' output_uri_replace = "^.*root,''" input_file_pattern = "*.js" // 仅导入JS文件,排除TS测试文件 }
内容的提问来源于stack exchange,提问作者ravvi
相关产品推荐
相关产品推荐

