English 中文(简体)
How do I conditionally include or exclude a file from an archetype when project is generated?
原标题:

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?

问题回答

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.

Thanks for the info Dan !

I just looked at looked at archetype plugin source code, and http://jira.codehaus.org/browse/ARCHETYPE-58 doesn t appear to have resolved this issue.

Just created http://jira.codehaus.org/browse/ARCHETYPE-424 to track it.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签