maven - 使用 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
...
谢谢您的帮助 ;)
解决方案
我检查了 xldeploy-maven-plugin-6.0.1 的来源,似乎这是当前实现的限制。
问题是GenerateDeploymentPackageMojo
不依赖 maven 来做属性的替换。相反,它使用自定义AbstractConfigurationConverter
命名DeployitCIConverter
(委托给MavenDeployableConverter
)将<deployables>
节点转换为MavenDeployable
)
这归结为:
- 您可以访问有效的 POM
- 您无权访问通过构建助手插件动态定义的属性
当然,动态定义的属性可以在任何 mojo 中访问:
- 如果你使用一个参数
- 如果您通过表达式评估器手动评估它们(检查:在 maven 插件中获取 mojo 参数)
对于 antrun 插件,它ExpressionEvaluator
在 echo 任务中明确使用。资料性文章:
Maven 中的属性解析及其对 Antrun 插件的影响
解决问题的想法:
- 在外部脚本中进行组和版本解析并将值传递给 mvn 命令。
- 在 execute 方法中创建一个 cutom mojo 扩展
GenerateDeploymentPackageMojo
和预处理部署(并不像听起来那么难)。
推荐阅读
- asp.net-core - 引导选择下拉菜单不呈现视图袋项目
- r - 使用 ESS 的块对齐注释
- mongodb - 具有多个任务的分布式官方 Mongodb Kafka 源连接器不起作用
- php - 使用 MySQL 相关性的搜索引擎开发,不适用于 PHP
- domain-driven-design - 在事件溯源中命名事件类型的最佳实践
- java - 如何在java 8中的不同字符串值列表中获取所有最大的数组实例
- java - 如何捕获 NoClassDefFoundError
- excel - NodeXL - 没有边的节点
- compiler-errors - 我如何编译 MPI 程序
- java - 无法将 128 分配给字节,为什么它会出错而不是溢出?