Conditionally enable exclude files when creating a project using the maven archetype

I'm trying to create an archetype that will conditionally include a file based on user input.

For example, if the user will use this own archetype and pass parameters such as
-DprojectType = webProject or -DprojectType = webDBProject


if webProject copies only files related to webProject, and if its webDBProject copies files related to webProject and files associated with the database.

I found that conditionally including / excluding a file is impossible, at least in the near future, using an archetype descriptor.

How can I conditionally include or exclude a file from an archetype when creating a project?

Another option that I had was to fulfill the goal after generating the archetype and include / delete unnecessary files. But we cannot use the eclipse M2E plugin with this.

The last option I tried is to use the speed template itself to perform post-processing operations.

Since we cannot create an instance of the object inside the speed template, I tried to use reflection to create an instance of the file and delete some file, as shown below:

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

writing the above line to the speed template file and running it on an isolated speed java program works fine. But the same does not work when executed as part of the Maven archetype generator.

I tried to go step by step where the execution was successful until the class was received, but the getConstructor () part does not execute when archetype: generate is run.

Has anyone tried to find out the reason or found an alternative solution?

Also does anyone know which version of the speed engine is used in Maven?

+10
source share
2 answers

I understand this is a really old question, but now (in 2018) I am doing this task with Maven support for a post-generated groovy script.

If you included the groovy script directory with the name "archetype-post-generate.groovy" in the src / main / resources / META-INF archetype project, then it will be executed after the archetype is created.

The script will have access to the archetype properties, for example. $ {artifactId}, including any custom properties.

What I do is include all possible files in the archetype, and then in the groovy script, I check the corresponding archetype properties and delete the unnecessary files.

In my script, I also rename some files, and also edit some files, reading them, doing line replacements, and then writing them back.

This is a bit cumbersome, but it works.

+9
source

The answer above GreyBeardedGeek is correct. If someone needs an example of what this Groovy script should look like, I wrote a short post .

Here is the Groovy script from my post:

 import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths // the path where the project got generated Path projectPath = Paths.get(request.outputDirectory, request.artifactId) // the properties available to the archetype Properties properties = request.properties // connectionType is either ftp or sftp String connectionType = properties.get("connectionType") // the Java package of the generated project, eg com.acme String packageName = properties.get("package") // convert it into a path, eg com/acme String packagePath = packageName.replace(".", "/") if (connectionType == "sftp") { // delete the FTP file Files.deleteIfExists projectPath.resolve("src/main/java/" + packagePath + "/polling/FtpFlowBuilder.java") } else if (connectionType == "ftp") { // delete the SFTP file Files.deleteIfExists projectPath.resolve("src/main/java/" + packagePath + "/polling/SftpFlowBuilder.java") } 
0
source

Source: https://habr.com/ru/post/959178/


All Articles