我可以在运行时指定Maven project.artifactId吗? [英] can I specify a maven project.artifactId at runtime?

查看:548
本文介绍了我可以在运行时指定Maven project.artifactId吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个maven POM,我想将其用作模板,根据传递给它的参数,生成具有不同名称的工件.但是我不知道如何在运行时指定artifactId.

I have a maven POM that I'd like to use as a template, producing artifacts with different names depending on what arguments I pass to it. But I can't figure out how to specify the artifactId at runtime.

如果我像这样参数化<artifactId>元素:

If I parameterize the <artifactId> element like this:

<artifact>foo-${bar}</artifact>

行家抱怨:

[警告]'artifactId'包含表达式,但应为常量.

[WARNING] 'artifactId' contains an expression but should be a constant.

如果我在POM中遗漏了<artifactId>并尝试在命令行上使用

进行指定,

If I leave out <artifactId> in the POM and try to specify it on the command line with

mvn -Dproject.artifactId=foo ...

行家抱怨:

[错误]"artifactId"丢失.

[ERROR] 'artifactId' is missing.

除了依靠模板动态生成POM文件以外,还有其他技巧可以用来完成此操作吗? [嗯,也许我可以使用Maven资源过滤来做到这一点...]

Is there another trick I could use to accomplish this, other than resorting to generating the POM file on-the-fly from a template? [Hmm, maybe I could do that using maven resource filtering...]

推荐答案

如果我做对了,您想重用一个蓝图Maven应用程序并能够更改artifactId.

If I get it right you want to reuse a blueprint maven application and be able to change the artifactId.

使用 Maven原型可以最好地实现此用例.请参阅此入门.这相当简单,值得学习.您拥有正常的Maven项目,并在蓝图pom中添加了${groupId}之类的变量.然后,它们会被您在原型生成时给定的参数替换:

This use case can be best accomplished with Maven archetypes. See this to get you started. It's fairly straight forward and its worth learning. You have your normal Maven project and you add variables like ${groupId} in your blueprint pom. They then get replaced by parameters given by you at the archetype generation:

mvn archetype:generate                                  \
-DarchetypeGroupId=<archetype-groupId>                \
-DarchetypeArtifactId=<archetype-artifactId>          \
-DarchetypeVersion=<archetype-version>                \
-DgroupId=<my.groupid>                                \
-DartifactId=<my-artifactId>

GitHub上还有很多人创建的原型,您可以在其中了解有关Maven原型中的结构化和过滤的更多信息

There are also a lot of archetypes created by people on GitHub where you can learn more about structuring and filtering in Maven archetypes For example.

或者,您可以尝试不使用原型系统而设置Maven过滤,但是我对此没有经验.我认为您不能在没有有效的artifactId的情况下运行项目,在此之前必须进行一些生成(例如从原型生成),但是我不确定.

Alternatively you can try to set up the Maven filtering without using the archetype system but I have no experience with that. I don't think you can run a project without it having a valid artifactId, some generation must happen before that (like in generating from an archetype) but I'm not sure.

这篇关于我可以在运行时指定Maven project.artifactId吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆