Conditionally include exclude files while creating project using maven archetype

一曲冷凌霜 提交于 2019-12-03 02:48:23

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.

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.

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

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.

The answer above by GreyBeardedGeek is the correct one. In case someone needs an example of how that Groovy script should look like, I wrote a small blogpost.

Here's 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, e.g. com.acme
String packageName = properties.get("package")

// convert it into a path, e.g. 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")
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!