I'm trying to create a maven archetype to generate a number of projects from a predefined template.
The template project generated by the archetype is a Spring Boot service. The following is the contents of the archetype project folder tree:
my-archetype
¦ pom.xml
¦
+---src
¦ +---main
¦ ¦ +---resources
¦ ¦ +---archetype-resources
¦ ¦ ¦ ¦ mvnw
¦ ¦ ¦ ¦ mvnw.cmd
¦ ¦ ¦ ¦ pom.xml
¦ ¦ ¦ ¦ rebuild.sh
¦ ¦ ¦ ¦
¦ ¦ ¦ +---src
¦ ¦ ¦ +---main
¦ ¦ ¦ ¦ +---docker
¦ ¦ ¦ ¦ ¦ Dockerfile
¦ ¦ ¦ ¦ ¦
¦ ¦ ¦ ¦ +---java
¦ ¦ ¦ ¦ ¦ __moduleClassName__ServiceApplication.java
¦ ¦ ¦ ¦ ¦ __moduleClassName__ServiceController.java
¦ ¦ ¦ ¦ ¦
¦ ¦ ¦ ¦ +---resources
¦ ¦ ¦ ¦ application.yml
¦ ¦ ¦ ¦ bootstrap.yml
¦ ¦ ¦ ¦
¦ ¦ ¦ +---test
¦ ¦ ¦ +---java
¦ ¦ ¦ __moduleClassName__ServiceApplicationTests.java
¦ ¦ ¦
¦ ¦ +---META-INF
¦ ¦ +---maven
¦ ¦ archetype-metadata.xml
¦ ¦
...
${moduleClassName}
is a custom property defined to allow customization of class names. I install the archetype in my local repository with mvn clean install, then try to create a project from it with mvn archetype:generate ... -DmoduleClassName=<myModule>
.
Everything seems to work correctly except that, while the *.java files are correctly named and placed in the correct pacakges (and strings inside them are replaced with property values as well), for other files the replacement doesn't happen. In particular this is the content of src\main\resources\archetype-resources\src\main\resources\bootstrap.yml
spring:
application:
name: ${project.artifactId}
and this is the content of src\main\resources\archetype-resources\src\main\docker\Dockerfile
FROM java:8
VOLUME /tmp
ADD ${project.artifactId}.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
I would like the ${project.artifactId}
variable to be replaced in both cases, while it don't happen in the generated project.
These are the relevant parts of my archetype-metadata.xml:
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor ... >
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/docker</directory>
<includes>
<include>**/Dockerfile</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
.
.
.
</fileSets>
<requiredProperties>
<requiredProperty key="moduleClassName"/>
</requiredProperties>
</archetype-descriptor>
In the end I found the solution by trial and error: the problem was just that I was using the wrong syntax for variable names, using ${project.artifactId}
where I should have written simply ${artifactId}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With