【问题标题】:curl with maven exec plugin not working because of "Can't handle single and double quotes in same argument"由于“无法在同一参数中处理单引号和双引号”,带有 maven exec 插件的 curl 无法正常工作
【发布时间】:2018-09-02 10:13:55
【问题描述】:

为了在成功部署工件的 docker 镜像后促进构建,我们必须像这样进行 curl:

curl -k -I -u usr:pw -X POST https://company.com/artifactory/api/docker... - H "Content-Type: application/json" -d '{"targetRepo":"project-release","dockerRepository":"project/sample","tag":"1.0.0","copy":"false"}'

使用 maven 尝试此操作时,请使用 exec 插件:

...
        <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 ${USER}:{PWD}</argument>
                  <argument>-X POST https://company.com/artifactory/api/docker/docker-local/v2/promote</argument>
                  <argument>-H "Content-Type: application/json"</argument>
                  <argument>-d &apos;{&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;}&apos;</argument>
                </arguments>
              </configuration>
            </execution>
          </executions>
        </plugin>
...

但这会导致 CI​​ 构建失败,原因是:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2:exec (promote-image) on project myproject: Execution promote-image of goal org.codehaus.mojo:exec-maven-plugin:1.2:exec failed: Can't handle single and double quotes in same argument -> [Help 1]

我认为这是因为 -d '" - 这怎么解决?

【问题讨论】:

  • 我不确定我是否正确,我已经对 -d '{"targetRepo 部分进行了编码,但我仍然遇到问题......
  • 对不起,我的错。我混淆了-H和-d。您可以尝试仅使用双引号并将其转义。像-d &amp;quot;{&amp;#92;&amp;quot;targetRepo...-d "{\"targetRepo... 的编码形式
  • 我能够在不转义或转换引号的情况下卷曲:&lt;argument&gt;-d {"request":{"method":"ANY","urlPattern":"/user-service/.*"},"response":{"proxyBaseUrl":"http://localhost:${service.http.port}"}}&lt;/argument&gt;

标签: maven curl maven-exec-plugin


【解决方案1】:

首先我认为你需要像这样声明你的论点,看看会发生什么:

<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>-H</argument>
              <argument>"Content-Type: application/json"</argument>
              <argument>-d</argument>
              <argument>&apos;{&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;}&apos;</argument>
              <argument>https://company.com/artifactory/api/docker/docker-local/v2/promote</argument>
            </arguments>
          </configuration>
        </execution>
      </executions>
    </plugin>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多