首页 > 解决方案 > 使用 build-helper-maven-plugin

问题描述

我有下面的 pom.xml。

我想tag使用以下命令将属性传递给我的构建:

mvn clean package -Dtag=test

它应该将此属性拆分为另外两个,my.group一个my.version,然后在一个 URI 中使用它来构建一个 XLDeploy 包,使用xldeploy-maven-plugin( https://docs.xebialabs.com/xldeploy-maven-plugin/6.0.x/ )。

我的问题是regex-properties目标实际上是在完成这项工作,正如我所看到的,这要归功于maven-antrun-plugin

[INFO] --- maven-antrun-plugin:1.1:run (default) @ myArtifactId ---
[INFO] Executing tasks
     [echo] Displaying value of 'my.group' property
     [echo] [my.group] group/test
     [echo] Displaying value of 'my.version' property
     [echo] [my.version] test

但是该命令grep fileUri target\deployit-working-dir\deployit-manifest.xml显示 Uri 中的变量没有被替换:

grep fileUri target\deployit-working-dir\deployit-manifest.xml
      <fileUri>http://mynexus.ur/service/local/repositories/my-repo/content/${my.group}/anArtefact/${my.version}/anArtefact-1.0.zip</fileUri>

POM 是以下文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>myGroupId</groupId>
    <artifactId>myArtifactId</artifactId>
    <version>1.0</version>

    <packaging>dar</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>regex-properties</id>
                        <goals>
                            <goal>regex-properties</goal>
                        </goals>
                        <configuration>
                            <regexPropertySettings>
                                <regexPropertySetting>
                                    <name>my.group</name>
                                    <value>group/${tag}</value>
                                    <regex>(asm-[0-9]+)-([0-9]+.[0-9]+)-([0-9]+)$</regex>
                                    <replacement>$1</replacement>
                                    <failIfNoMatch>false</failIfNoMatch>
                                </regexPropertySetting>
                                <regexPropertySetting>
                                    <name>my.version</name>
                                    <value>${tag}</value>
                                    <regex>(asm-[0-9]+)-([0-9]+.[0-9]+)-([0-9]+)$</regex>
                                    <replacement>$1-SNAPSHOT</replacement>
                                    <failIfNoMatch>false</failIfNoMatch>
                                </regexPropertySetting>
                            </regexPropertySettings>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>Displaying value of 'my.group' property</echo>
                                <echo>[my.group] ${my.group}</echo>
                                <echo>Displaying value of 'my.version' property</echo>
                                <echo>[my.version] ${my.version}</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.xebialabs.xldeploy</groupId>
                <artifactId>xldeploy-maven-plugin</artifactId>
                <version>6.0.0</version>

                <extensions>true</extensions>

                <configuration>
                    <deployables>
                        <file.Folder name="file">
                            <scanPlaceholders>false</scanPlaceholders>
                            <fileUri>http://mynexus.ur/service/local/repositories/my-repo/content/${my.group}/anArtefact/${my.version}/anArtefact-${project.version}.zip</fileUri>
                        </file.Folder>
                    </deployables>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我不太确定build-helper-maven-plugin我的 POM 中是否有错误或其他任何地方,或者是否只是缺少替换xldeploy-maven-plugin...

谢谢您的帮助 ;)

标签: mavenxl-deploybuild-helper-maven-plugin

解决方案


我检查了 xldeploy-maven-plugin-6.0.1 的来源,似乎这是当前实现的限制。

问题是GenerateDeploymentPackageMojo不依赖 maven 来做属性的替换。相反,它使用自定义AbstractConfigurationConverter命名DeployitCIConverter(委托给MavenDeployableConverter)将<deployables>节点转换为MavenDeployable

这归结为:

  • 您可以访问有效的 POM
  • 您无权访问通过构建助手插件动态定义的属性

当然,动态定义的属性可以在任何 mojo 中访问:

对于 antrun 插件,它ExpressionEvaluator在 echo 任务中明确使用。资料性文章: Maven 中的属性解析及其对 Antrun 插件的影响

解决问题的想法:

  • 在外部脚本中进行组和版本解析并将值传递给 mvn 命令。
  • 在 execute 方法中创建一个 cutom mojo 扩展GenerateDeploymentPackageMojo和预处理部署(并不像听起来那么难)。

推荐阅读