How do I conditionally include or exclude a file from an archetype when project is generated?

Viewed 4681

I'm creating Maven 2 archetypes for our project (Weld). I would like to be able to control which files are placed into the generated project based on the value of a property that is defined during archetype:generate. For instance, I foresee the following prompt:

Define value for groupId: : com.example
Define value for artifactId: : myproject
Define value for package:  com.example: :
Define value for includeGradleSupport: : y

Based on the value of includeGradleSupport, I want to include (or not include) the build.gradle file in the generated project. If the user does not want Gradle support, I don't want to clutter up the generated project with unnecessary files.

Another example is that I might need to provide a Jetty web fragment (to activate a listener perhaps) if the user wants Jetty support.

It's all about customization of the project based on what the developer intends to use. While I could create a whole other archetype, sometimes the changes are so slight that it would be easier to include/exclude a file.

Is there a way to control this behavior using the archetype-metadata.xml descriptor?

4 Answers

I personally would move the parts that can be removed/added on user request and put the into different maven profiles so u can build different part using different profiles

I can have a look into what coding it would take to enable this in the archetype plugin.

I think the primary vehicle to do this today would be to conditionally produce two different archetype artifacts during the original build. The archetype user would then explicitly use yourarchetype-withthing or yourarchetype-withoutthing.

I know this isn't perfectly what you are after and I agree that what you are asking for is a sensible use case.

While I could create a whole other archetype, sometimes the changes are so slight that it would be easier to include/exclude a file.

This sentence made me think...

It seems like you have a default project structure.
Let's suppose it is big, has many files. Of course, you don't want to duplicate the logic and the files in a different archetype.

Now sometimes, a project has an additional behavior (related to Gradle).
This sound a typical use-case for another archetype that does not start from nothing, but that comes after the first one. I've seen several examples of such archetypes on the web. The developper triggers this archetype only if the project needs Graddle. :-)

So I suggest : create your Graddle archetype, that adds only the files relevant to Graddle.

Related