使用Maven原型创建项目时有条件地包含排除文件 [英] Conditionally include exclude files while creating project using maven archetype

查看:202
本文介绍了使用Maven原型创建项目时有条件地包含排除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试创建一个原型,该原型将根据用户输入有条件地包含文件.

例如,如果用户将使用此自定义原型并传递参数,例如
-DprojectType = webProject -DprojectType = webDBProject

Am trying to create an archetype which will conditionally include file based on the user input.

Like for eg., if a user will use this custom archetype and pass parameters like
-DprojectType=webProject or -DprojectType=webDBProject


如果webProject仅复制与webProject相关的文件,并且其webDBProject复制与webProject相关的文件以及与DB相关的文件.


if webProject copy only files related to webProject and if its webDBProject copy files related to webProject and DB related files.

我发现使用原型描述符至少在不久的将来不可能有条件地包含/排除文件.

I found out that conditionally include/exclude file is not possible at least in near future using archetype-descriptor.

如何生成项目时,有条件地从原型中包括文件还是从文件中排除文件?

我的另一个选择是在原型生成后执行目标,并包含/删除不需要的文件.但是我们不能与此配合使用Eclipse M2E插件.

The other option i had was to execute a goal after archetype generation and include/remove unwanted files. But we can't utilize the eclipse M2E plugin with this.

我尝试的最后一个选择是利用速度模板本身来执行后处理操作.

The final option i was trying is to utilize velocity template itself to execute the post processing operations.

由于我们无法实例化速度模板中的对象,因此我试图使用反射来创建文件实例并删除某些文件,如下所示,

Since we can't instantiate an object inside velocity template, I was trying to use reflection to create a file instance and delete some file like the following,

$somestring.getClass().forName("java.io.File").getMethod("delete", null).invoke($somestring.getClass().forName("java.io.File").getConstructor($somestring.getClass()).newInstance("delete.txt"), null)

在速度模板文件中编写以上行,并在独立的速度Java程序上运行正常.但是当作为Maven原型生成器的一部分执行时,这是行不通的.

writing the above line in a velocity template file and running against a standalone velocity java program is running fine. But the same is not working when executing as part of maven archetype generator.

我试图逐步进行,直到成功获取类为止,执行都是成功的,但是在运行原型:生成时,getConstructor()部分无法执行.

I tried to go step by step, where the execution was successful until getting the class but the getConstructor() part is failing to execute while running archetype:generate.

有人尝试过知道原因或有其他解决方法吗?

Did anyone tried and know the reason or have alternate solution?

还有人知道在Maven中使用什么版本的速度引擎吗?

Also anyone knows what version of velocity engine being used in Maven ?

推荐答案

我意识到这是一个非常老的问题,但是现在(在2018年),我将通过使用Maven对后发布的支持来完成此任务.生成常规脚本.

I realize that this is a really old question, but now (in 2018), I'm accomplishing this task through the use of Maven's support for a post-generate groovy script.

如果在原型项目的src/main/resources/META-INF目录中包含一个名为"archetype-post-generate.groovy"的常规脚本,那么它将在生成原型后执行.

If you include a groovy script named "archetype-post-generate.groovy" in the archetype project's src/main/resources/META-INF directory, then it will be executed after the archetype is generated.

该脚本将有权访问原型的属性,例如$ {artifactId},包括所有自定义属性.

The script will have access to the archetype's properties, e.g. ${artifactId}, including any custom properties.

我要做的是将所有可能的文件包含在原型中,然后在groovy脚本中检查相关的原型属性,并删除不需要的文件.

What I do is to include all possible files in the archetype, and then in the groovy script, I inspect the relevant archetype properties, and delete the unwanted files.

在我的脚本中,我还重命名了一些文件,并通过读入文件,替换字符串然后将其写回来编辑其中的一些文件.

In my script, I'm also renaming some files, as well as editing some of the files by reading them in, doing string replacements, and then writing them back out.

这有点麻烦,但是有效.

It's a bit cumbersome, but it works.

这篇关于使用Maven原型创建项目时有条件地包含排除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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