猿问

Maven - 在构建之前更新 artifactId

我需要将我的项目部署到人工制品中。为此,我将 maven-assembly-plugin 与 artifactory-maven-plugin 一起使用


只有我可以用来构建 mvn 的是这个 CMD(可以进行小的更新):


mvn -e -B -U clean deploy -DskipIntegrationTests=false -DskipCoverageReport=false  -Dservice_name=sample_service

我不能在 mvn 命令中做的是更新服务名称。它将始终是“sample_service”或其他代表服务名称的常量


因为我不知道服务名称(可能有更多服务),我 pom.xml 的基本部分看起来像这样(artifactId 是从属性 service_name 动态创建的):


    <groupId>my.group.id</groupId>

    <artifactId>${service_name}</artifactId>

    <version>2.0.0-SNAPSHOT</version>

问题是参数 -Dservice_name 将始终包含“下划线”。由于约定,工件必须包含“破折号”而不是“下划线”。


有什么办法(例如某些插件)我可以做这样的事情吗?


    <groupId>my.group.id</groupId>

    <artifactId>${service_name}.replaceAll("_","-")</artifactId>

    <version>2.0.0-SNAPSHOT</version>

简而言之,属性 service_name 我需要在构建工件之前用破折号替换下划线。


感谢您的回答。


ITMISS
浏览 231回答 3
3回答

守着星空守着你

我找到了解决问题的方法。但我不确定如何解决它是否正确。我使用了插件gmaven-plugin&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.codehaus.groovy.maven</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>gmaven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>pre-clean</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>execute</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <source>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; project.getModel().setArtifactId(project.properties["service_name"].replaceAll('_', '-'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; project.getArtifact().setArtifactId(project.properties["service_name"].replaceAll('_', '-'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </source>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>之后,我使用 maven-assembly 插件将数据上传到人工制品中。这个插件从实例“project.getArtifacts()”读取工件 ID,所以我直接更新它。所以我直接在 Maven 实例中更新了工件 ID。正如我所说,它不是 100% 正确,但在我的情况下它有帮助

月关宝盒

这是不可能的。内部使用的属性<artifactId>只能通过命令行设置。您没有机会在 Maven 中操纵它们。我看到的唯一机会是更改命令行,以便在将参数发送到 Maven之前进行替换。

波斯汪

您可以使用 buildhelper 插件执行此操作,它有一个目标regex-property,它可以根据初始值(您的 service_name 属性)和一个正则表达式来设置属性以替换为替换值。来自使用页面的示例(因为使用的值没有意义而改编):<project>&nbsp; ...&nbsp; <build>&nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.codehaus.mojo</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>build-helper-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>3.0.0</version>&nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>regex-property</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>regex-property</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name>human.version</name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>${project.version}</value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <regex>-SNAPSHOT</regex>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <replacement> pre-release development version</replacement>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <failIfNoMatch>false</failIfNoMatch>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; </plugins>&nbsp; </build>&nbsp; ...</project>
随时随地看视频慕课网APP

相关分类

Java
我要回答