使用Maven exec插件执行curl失败:无法处理参数中单双引号共存
解决exec-maven-plugin执行curl时混合单双引号的报错问题
这个报错确实是因为exec-maven-plugin 1.2版本的参数解析bug——它没法正确处理同一个参数里同时存在单引号和双引号的情况。咱们可以用几种方式解决,这里给你推荐两个靠谱的方案:
方案一:拆分参数+XML转义(快速修复)
把curl的每个命令选项都拆成独立的<argument>元素,同时在XML里对双引号进行转义(用"替代"),这样就能避开混合引号的问题。另外注意你原来配置里的{PWD}少了$符号,要改成${PWD}:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2</version> <executions> <execution> <id>promote-image</id> <phase>deploy</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>curl</executable> <arguments> <argument>-k</argument> <argument>-i</argument> <argument>-u</argument> <argument>${USER}:${PWD}</argument> <argument>-X</argument> <argument>POST</argument> <argument>https://company.com/artifactory/api/docker/docker-local/v2/promote</argument> <argument>-H</argument> <argument>Content-Type: application/json</argument> <argument>-d</argument> <argument>"{"targetRepo":"${docker.repository.release}","dockerRepository":"${docker.image.prefix}/${project.artifactId}","tag":"${project.version}","copy":"false"}"</argument> </arguments> </configuration> </execution> </executions> </plugin>
方案二:用文件传递JSON数据(更易维护)
如果你的JSON结构比较复杂,或者以后可能要修改,把JSON内容放到单独文件里是更稳妥的选择,彻底避开引号问题:
- 创建模板JSON文件:在
src/main/resources下新建promote.json,内容如下:
{"targetRepo":"${docker.repository.release}","dockerRepository":"${docker.image.prefix}/${project.artifactId}","tag":"${project.version}","copy":"false"}
- 配置资源过滤:添加
maven-resources-plugin,在构建时替换JSON里的Maven变量,输出到构建目录:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> <executions> <execution> <id>filter-promote-json</id> <phase>prepare-package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}</outputDirectory> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>promote.json</include> </includes> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
- 修改exec插件配置:让curl通过
@file语法读取过滤后的JSON文件:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2</version> <executions> <execution> <id>promote-image</id> <phase>deploy</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>curl</executable> <arguments> <argument>-k</argument> <argument>-i</argument> <argument>-u</argument> <argument>${USER}:${PWD}</argument> <argument>-X</argument> <argument>POST</argument> <argument>https://company.com/artifactory/api/docker/docker-local/v2/promote</argument> <argument>-H</argument> <argument>Content-Type: application/json</argument> <argument>-d</argument> <argument>@${project.build.directory}/promote.json</argument> </arguments> </configuration> </execution> </executions> </plugin>
另外补充一句:如果你的CI环境允许,也可以尝试把exec-maven-plugin升级到最新版本(比如3.1.0),新版本对参数引号的处理逻辑做了优化,可能直接就能兼容你原来的写法。
内容的提问来源于stack exchange,提问作者MemLeak
相关产品推荐
相关产品推荐

