将npm管理的T-REX作为Maven依赖引入的方案及Jenkins部署咨询
一、替换ZIP下载为Git仓库克隆(Maven实现)
用maven-git-plugin替代ZIP包下载,既能保证版本可控,又符合规范的源码获取流程。在pom.xml中添加以下配置:
<plugin> <groupId>com.github.eirslett</groupId> <artifactId>maven-git-plugin</artifactId> <version>0.1.0</version> <executions> <execution> <id>clone-t-rex</id> <phase>initialize</phase> <goals> <goal>clone</goal> </goals> <configuration> <repositoryUrl>https://github.com/TokenySolutions/T-REX.git</repositoryUrl> <directory>${project.build.directory}/t-rex</directory> <!-- 可选:指定分支/标签,保证版本稳定 --> <branch>main</branch> </configuration> </execution> </executions> </plugin>
该配置会在Maven初始化阶段,将T-REX仓库克隆到项目构建目录下的t-rex文件夹。
二、保留目标文件并清理冗余内容
克隆完成后,先执行npm install拉取合约依赖,再通过Maven插件或shell命令清理不需要的文件:
1. 执行npm安装依赖
用exec-maven-plugin在Maven流程中触发npm命令:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>npm-install</id> <phase>process-resources</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>npm</executable> <workingDirectory>${project.build.directory}/t-rex</workingDirectory> <arguments> <argument>install</argument> </arguments> </configuration> </execution> </executions> </plugin>
2. 清理冗余文件/目录
用exec-maven-plugin执行shell命令,仅保留contracts文件夹和合约依赖相关的node_modules(按需调整),删除其他内容:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>clean-unneeded-files</id> <phase>process-resources</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>bash</executable> <workingDirectory>${project.build.directory}/t-rex</workingDirectory> <arguments> <argument>-c</argument> <argument> # 保留contracts和node_modules,删除其他根目录文件/目录 ls -1 | grep -vE 'contracts|node_modules' | xargs rm -rf </argument> </arguments> </configuration> </execution> </executions> </plugin>
如果不需要完整的node_modules,可以进一步筛选其中的核心合约依赖目录(比如仅保留@openzeppelin等)。
3. 复制目标文件到项目指定目录
最后用maven-resources-plugin将清理后的contracts和依赖文件复制到项目可访问的目录:
<plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> <executions> <execution> <id>copy-t-rex-contracts</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.basedir}/src/main/resources/contracts</outputDirectory> <resources> <resource> <directory>${project.build.directory}/t-rex/contracts</directory> <filtering>false</filtering> </resource> <!-- 可选:复制node_modules中的合约依赖 --> <resource> <directory>${project.build.directory}/t-rex/node_modules/@openzeppelin/contracts</directory> <filtering>false</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
三、Jenkins持续部署到Docker镜像的差异
和本地Maven构建流程相比,Jenkins部署到Docker主要有以下不同点:
1. 源码获取更灵活
直接用Jenkins Git插件克隆T-REX仓库,无需依赖Maven的git插件,支持绑定Git webhook触发自动构建,还能指定commit哈希、分支或标签,版本控制更精准。
2. 环境隔离与依赖缓存
- 使用Docker Agent作为Jenkins构建节点,保证npm、Git等工具版本统一,避免本地环境差异。
- 利用Jenkins Pipeline的缓存机制缓存
node_modules目录,避免每次构建重复下载依赖,大幅提升速度:
pipeline { agent { docker { image 'node:18-alpine' } } stages { stage('Clone T-REX') { steps { git url: 'https://github.com/TokenySolutions/T-REX.git', branch: 'main' } } stage('Install Dependencies') { steps { cache(path: 'node_modules', key: "npm-\${checksum 'package-lock.json'}") { sh 'npm install' } } } } }
3. 清理与文件处理更直接
在Jenkinsfile中直接用shell命令完成冗余文件清理,无需配置Maven插件,逻辑更直观:
stage('Clean Unneeded Files') { steps { sh ''' ls -1 | grep -vE 'contracts|node_modules' | xargs rm -rf ''' } }
4. Docker镜像构建集成
将处理好的合约文件直接打包到Docker镜像中,在Jenkins Pipeline中添加镜像构建与推送步骤:
stage('Build Docker Image') { steps { script { docker.build('your-image-name:latest', '--build-arg CONTRACTS_DIR=contracts .') docker.push('your-image-name:latest') } } }
同时在Dockerfile中添加COPY指令,将合约文件复制到镜像内指定路径:
FROM your-base-image ARG CONTRACTS_DIR COPY ${CONTRACTS_DIR} /app/contracts
内容的提问来源于stack exchange,提问作者András Ballai

