Fork me on GitHub

Create an archetype with a property file

Creating an archetype using a property file is possible by defining a text file containing the properties that will lead the archetype creation, and by calling the Archetype Plugin with the command line property archetype.properties giving it the path to the property file.

The directory above the would be archetype project:

  1. $ tree
  2. .
  3. |____archetype.properties
  4. |____some-project
  5. | |____pom.xml
  6. | |____src
  7. | | |____main
  8. | | | |____groovy
  9. | | | | |____com
  10. | | | | | |____company
  11. | | | | | | |____App.groovy
  12. | | | |____java
  13. | | | | |____com
  14. | | | | | |____company
  15. | | | | | | |____App.java
  16. | | |____test
  17. | | | |____java
  18. | | | | |____com
  19. | | | | | |____company
  20. | | | | | | |____AppTest.java
  21. | | | |____resources
  22. | | | | |____App.properties

The content of the property file:

  1. $ cat archetype.properties
  2. archetype.groupId=my.group.id
  3. archetype.artifactId=archetype-with-properties
  4. archetype.version=2.0
  5.  
  6. archetype.filteredExtensions=java
  7. archetype.languages=groovy
  8.  
  9. an_additional_property=my specific value

Creating the archetype:

  1. $ cd some-project
  2. $ mvn archetype:create-from-project -Darchetype.properties=../archetype.properties
  3. [INFO] Scanning for projects...
  4. [INFO] Searching repository for plugin with prefix: 'archetype'.
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO] Building some-project
  7. [INFO] task-segment: [archetype:create-from-project] (aggregator-style)
  8. [INFO] ------------------------------------------------------------------------
  9. [INFO] Preparing archetype:create-from-project
  10. [INFO] ------------------------------------------------------------------------
  11. [INFO] Building some-project
  12. [INFO] ------------------------------------------------------------------------
  13. [INFO] No goals needed for project - skipping
  14. [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
  15. [INFO] Setting property: velocimacro.messages.on => 'false'.
  16. [INFO] Setting property: resource.loader => 'classpath'.
  17. [INFO] Setting property: resource.manager.logwhenfound => 'false'.
  18. [INFO] [archetype:create-from-project]
  19. [INFO] Setting default groupId: com.company
  20. [INFO] Setting default artifactId: some-project
  21. [INFO] Setting default version: 1.0-SNAPSHOT
  22. [INFO] Setting default package: com.company
  23. [INFO] Archetype created in target/generated-sources/archetype
  24. [INFO] ------------------------------------------------------------------------
  25. [INFO] BUILD SUCCESSFUL
  26. [INFO] ------------------------------------------------------------------------
  27. [INFO] Total time: 2 seconds
  28. [INFO] Finished at: Tue Sep 16 19:42:06 CEST 2008
  29. [INFO] Final Memory: 8M/15M
  30. [INFO] ------------------------------------------------------------------------
  31.  
  32. $ cd target/generated-sources/archetype/
  33. $ mvn install
  34. [INFO] Scanning for projects...
  35. [INFO] ------------------------------------------------------------------------
  36. [INFO] Building archetype-with-properties
  37. [INFO] task-segment: [install]
  38. [INFO] ------------------------------------------------------------------------
  39. [INFO] [resources:resources]
  40. [INFO] Using default encoding to copy filtered resources.
  41. [INFO] [resources:testResources]
  42. [INFO] Using default encoding to copy filtered resources.
  43. [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
  44. [INFO] Setting property: velocimacro.messages.on => 'false'.
  45. [INFO] Setting property: resource.loader => 'classpath'.
  46. [INFO] Setting property: resource.manager.logwhenfound => 'false'.
  47. [INFO] [archetype:jar]
  48. [INFO] [archetype:add-archetype-metadata]
  49. [INFO] [archetype:integration-test]
  50. [INFO] [install:install]
  51. [INFO] Installing /private/tmp/archetypes/some-project/target/generated-sources/archetype/target/archetype-with-properties-2.0.jar to /Users/raphaelpieroni/.m2/repository/my/group/id/archetype-with-properties/2.0/archetype-with-properties-2.0.jar
  52. [INFO] [archetype:update-local-catalog]
  53. [INFO] ------------------------------------------------------------------------
  54. [INFO] BUILD SUCCESSFUL
  55. [INFO] ------------------------------------------------------------------------
  56. [INFO] Total time: 5 seconds
  57. [INFO] Finished at: Tue Sep 16 19:43:57 CEST 2008
  58. [INFO] Final Memory: 10M/20M
  59. [INFO] ------------------------------------------------------------------------

Using that archetype:

  1. $ mvn archetype:generate -DarchetypeCatalog=local
  2. $ mvn archetype:generate -DarchetypeCatalog=local
  3. [INFO] Scanning for projects...
  4. [INFO] Searching repository for plugin with prefix: 'archetype'.
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO] Building Maven Default Project
  7. [INFO] task-segment: [archetype:generate] (aggregator-style)
  8. [INFO] ------------------------------------------------------------------------
  9. [INFO] Preparing archetype:generate
  10. [INFO] No goals needed for project - skipping
  11. [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
  12. [INFO] Setting property: velocimacro.messages.on => 'false'.
  13. [INFO] Setting property: resource.loader => 'classpath'.
  14. [INFO] Setting property: resource.manager.logwhenfound => 'false'.
  15. [INFO] [archetype:generate]
  16. [INFO] Generating project in Interactive mode
  17. [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
  18. Choose archetype:
  19. 1: local -> archetype-with-properties (archetype-with-properties)
  20. Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1
  21. Define value for groupId: : foo
  22. Define value for artifactId: : bar
  23. Define value for version: 1.0-SNAPSHOT: :
  24. Define value for package: foo: :
  25. Confirm properties configuration:
  26. an_additional_property: my specific value
  27. groupId: foo
  28. artifactId: bar
  29. version: 1.0-SNAPSHOT
  30. package: foo
  31. Y: : N
  32. Define value for an_additional_property: my specific value: : my new value
  33. Define value for groupId: : my.new.group
  34. Define value for artifactId: : my-new-artifact
  35. Define value for version: 1.0-SNAPSHOT: :
  36. Define value for package: my.new.group: :
  37. Confirm properties configuration:
  38. an_additional_property: my new value
  39. groupId: my.new.group
  40. artifactId: my-new-artifact
  41. version: 1.0-SNAPSHOT
  42. package: my.new.group
  43. Y: :
  44. [INFO] ------------------------------------------------------------------------
  45. [INFO] BUILD SUCCESSFUL
  46. [INFO] ------------------------------------------------------------------------
  47. [INFO] Total time: 58 seconds
  48. [INFO] Finished at: Tue Sep 16 19:51:09 CEST 2008
  49. [INFO] Final Memory: 8M/15M
  50. [INFO] ------------------------------------------------------------------------

As you can see, the additional property is provided by default. As one want to override the value, one just have to answer N to the first confirmation, then the whole bunch of properties is proposed.

The tree of the generated project show that the only language recognized as such is groovy. The property archetype.languages gives the list of the languages. The languages are the directories in src/main and src/test that have the capacity to contain a "packaged" directory tree. This means that the package value is used to generate the new directory for that language.

  1. $ cd my-new-artifact/
  2. tree
  3. .
  4. |____pom.xml
  5. |____src
  6. | |____main
  7. | | |____groovy
  8. | | | |____my
  9. | | | | |____new
  10. | | | | | |____group
  11. | | | | | | |____App.groovy
  12. | | |____java
  13. | | | |____com
  14. | | | | |____company
  15. | | | | | |____App.java
  16. | |____test
  17. | | |____java
  18. | | | |____com
  19. | | | | |____company
  20. | | | | | |____AppTest.java
  21. | | |____resources
  22. | | | |____App.properties

The contents of both App.groovy and App.java shows that the only filtered files are java files. The property archetype.filteredExtensions gives the list of file extensions that permit the filtering. In the example, only the files whose names ending with .java are filtered.

  1. $ cat src/main/groovy/my/new/group/App.groovy
  2. package com.company
  3.  
  4. class App
  5. {
  6. def value = 'my specific value'
  7. }
  8.  
  9. $ cat src/main/java/com/company/App.java
  10. package my.new.group;
  11.  
  12. /**
  13. * Hello world!
  14. *
  15. */
  16. public class App
  17. {
  18. String value = "my new value";
  19. public static void main( String[] args )
  20. {
  21. System.out.println( "Hello World!" );
  22. }
  23. }

The languages and the filtered extensions are used even if that don't make any sense. All the properties defined in the property file can be used as command line properties.

When undefined, the archetype.languages and archetype.filteredExtensions properties are given sensible default values:

  • archetype.filteredExtensions have: java, xml, txt, groovy, cs, mdo, aj, jsp, gsp, vm, html, xhtml, properties, .classpath, .project. Notice the dotted filtered extensions that contains the complete file names.
  • archetype.languages have: java, groovy, csharp, aspectj.