With the help of Groovy, we can delete a file from a generated project if it meets a condition (for instance if a maven property equals a certain value).
Here is the procedure :
1) Create the archetype. To do that, type the following command from the root of a Maven project :
mvn archetype:create-from-project
result ==> [INFO] Archetype project created in E:\eclipse\workspace\celinio\target\generated-sources\archetype
The archetype project is here :
/celinio/target/generated-sources/archetype
Copy-paste this folder structure into another folder in the workspace and import it as an existing maven project.
By default, I have decided that this archetype will generate a project which contains a file /src/main/resources/META-INF/configuration.xml. This is the file that I do not want to include if the maven property configurable is set to no.
2) Add the following required property to /celinioArchetype/src/main/resources/META-INF/maven/archetype-metadata.xml
<requiredProperties>
<requiredProperty key="configurable">
<defaultValue>yes</defaultValue>
</requiredProperty>
</requiredProperties>
Also add this property to /celinioArchetype/src/test/resources/projects/basic/archetype.properties :
configurable=yes
3) Add the following groovy script archetype-post-generate.groovy under the folder /celinioArchetype/src/main/resources/META-INF/
It will be executed upon creating a project from this archetype.
https://maven.apache.org/archetype/maven-archetype-plugin/advanced-usage.html
/**
* CF : This script will be executed upon creating a project from this archetype.
* https://maven.apache.org/archetype/maven-archetype-plugin/advanced-usage.html
* It will delete a folder called configuration.xml if the value for the "configurable" property
* is set to true.
*/
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
println "artifactId: " + artifactId
println "request: " + request
println "archetypeArtifactId: " + request.getArchetypeArtifactId()
println "archetypeGroupId: " + request.getArchetypeGroupId()
println "archetypeVersion: " + request.getArchetypeVersion()
println "archetypeName: " + request.getArchetypeName()
println "artifactId: " + request.getArtifactId()
println "groupId: " + request.getGroupId()
println "version: " + request.getVersion()
Path projectPath = Paths.get(request.outputDirectory, request.artifactId)
Properties properties = request.properties
String configurableProperty = properties.get("configurable")
println "configurableProperty : " + configurableProperty
if (!configurableProperty.equals("yes")) {
println "Deleting the configuration.xml file"
Path configPath = projectPath.resolve("src/main/resources/META-INF")
String configurationFile = "configuration.xml";
Path cxfConfigPath = configPath.resolve(configurationFile)
println "cxfConfigPath " + cxfConfigPath
Files.deleteIfExists cxfConfigPath
}
To test it in eclipse, you will need to create a project from a run configuration as the groovy script is not executed if you go through the wizard (File > New Maven Project > select archetype etc)
See this link for details :
https://bugs.eclipse.org/bugs/show_bug.cgi?id=514993
I have created a repository on github to host the code for this basic maven archetype :
https://github.com/longbeach/mavenArchetypeGroovy
I added two run configuration files which will help testing it.
