You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Maven exec插件执行curl失败:无法处理参数中单双引号共存

解决exec-maven-plugin执行curl时混合单双引号的报错问题

这个报错确实是因为exec-maven-plugin 1.2版本的参数解析bug——它没法正确处理同一个参数里同时存在单引号和双引号的情况。咱们可以用几种方式解决,这里给你推荐两个靠谱的方案:

方案一:拆分参数+XML转义(快速修复)

把curl的每个命令选项都拆成独立的<argument>元素,同时在XML里对双引号进行转义(用&quot;替代"),这样就能避开混合引号的问题。另外注意你原来配置里的{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>&quot;{&quot;targetRepo&quot;:&quot;${docker.repository.release}&quot;,&quot;dockerRepository&quot;:&quot;${docker.image.prefix}/${project.artifactId}&quot;,&quot;tag&quot;:&quot;${project.version}&quot;,&quot;copy&quot;:&quot;false&quot;}&quot;</argument>
        </arguments>
      </configuration>
    </execution>
  </executions>
</plugin>

方案二:用文件传递JSON数据(更易维护)

如果你的JSON结构比较复杂,或者以后可能要修改,把JSON内容放到单独文件里是更稳妥的选择,彻底避开引号问题:

  1. 创建模板JSON文件:在src/main/resources下新建promote.json,内容如下:
{"targetRepo":"${docker.repository.release}","dockerRepository":"${docker.image.prefix}/${project.artifactId}","tag":"${project.version}","copy":"false"}
  1. 配置资源过滤:添加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>
  1. 修改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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:03:03