使用Ansible修改pom.xml

问题描述 投票:0回答:1

我想即时修改

pom.xml
文件,以使 Maven 针对特定
groupId
使用本地安装的 JAR。所以,我需要:

  1. 将每个匹配依赖项的范围更改/设置为
    system
  2. systemPath
    添加到每个匹配的依赖项,将其指向一个文件,该文件的名称将是
    artifactId
    的函数。

例如,

                <dependency>
                       <groupId>myGroup</groupId>
                       <artifactId>myGroup-agent-api</artifactId>
                       <version>3.1.38.13</version>
                       <scope>provided</scope>
                </dependency>

需要成为:

                <dependency>
                       <groupId>myGroup</groupId>
                       <artifactId>myGroup-agent-api</artifactId>
                       <version>3.1.38.13</version>
                       <scope>system</scope>
                       <systemPath>${application_path}/jar/agent-api.jar</systemPath>
                </dependency>

我弄清楚的第一部分。使用 xml 模块的 Ansible 任务应该更改/设置范围:

- name: Set scope to system for myGroup if {{ module.name }} uses any
  xml:
    path: '{{ app_path }}/{{ module.name }}/pom.xml'
    namespaces:
      x: http://maven.apache.org/POM/4.0.0
    xpath: '//x:dependency/x:groupId[text()="myGroup"]/../x:scope'
    value: system
  register: xmlfound
  when: java_files.matched
  failed_when:
    - xmlfound is failed
    - >-
      'in order to spawn nodes' not in xmlfound.msg

(摆弄

failed_when
是必要的,因为 lxml 会在 xpath 上抛出一个问题 if 它找不到匹配的依赖项。)

但是我将如何实现第二部分——根据每个匹配依赖项的

systemPath
生成
artifactId

xml maven xpath ansible pom.xml
1个回答
0
投票

我更喜欢 提到的 Maven 配置文件 解决方案 Alexander Pletnev 在对问题的评论中,因为这使您的构建更加独立于外部工具/配置:

首先,我会将您的

myGroup-agent-api
依赖项存储在您的:

这样能够从 Maven 通常的依赖解析中获益。

其次,在你的 POM 中:

  ...
  <profiles>
    <profile>
      <id>agent-provided</id>
      <!-- See https://maven.apache.org/guides/introduction/introduction-to-profiles.html#details-on-profile-activation
          "All profiles that are active by default are automatically
          deactivated when a profile in the POM is activated on the
          command line or through its activation config."
      --> 
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <dependencies>
        <dependency>
          <groupId>myGroup</groupId>
          <artifactId>myGroup-agent-api</artifactId>
          <version>3.1.38.13</version>
          <scope>provided</scope>
        </dependency>
      <dependencies>
    </profile>

    <profile>
      <id>agent-repo</id>
      <dependencies>
        <dependency>
          <groupId>myGroup</groupId>
          <artifactId>myGroup-agent-api</artifactId>
          <version>3.1.38.13</version>   
        </dependency>
      <dependencies>
    </profile>
    ...
  </profiles>

  ...

运行

mvn <phase>
进行具有依赖项
<scope>provided
的构建。

运行

mvn <phase> -P agent-repo
进行构建,并从存储库解析依赖项。然后配置文件
agent-provided
将被停用;请参阅上面 POM 声明中的注释。

© www.soinside.com 2019 - 2024. All rights reserved.