I'm trying to create a Maven Archetype for a Java web app and I have some JavaScript files I want it to generate. Is it possible to use Maven Archetype properties in the JavaScript file such that it will get replaced by the defined value at generation?
example.js
#set( $symbol_escape = '$' )
var name = "${artifactId}"
The above doesn't appear to work. I've tried other properties as well. It would seem it just isn't supported. Maybe due to collision possibilities with '$'?
The end resulting example.js file that is created just has the text "${artifactId}" instead of replacing it with the value of the property artifactId.
I'm using Maven Archetype plugin/extension version 2.2.
archetype-metadata.xml
...
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/javascript</directory>
<includes>
<include>**/*.js</include>
</includes>
</fileSet>
JS Path: src/main/javascript/my/package/name/example.js
In Maven Archetype, you can use property placeholders in your files to be generated, including JavaScript files. However, the default property delimiter in Maven Archetype is also the dollar sign ($), which conflicts with JavaScript's use of the dollar sign for variables.
To resolve this conflict, you can change the property delimiter in the archetype by configuring the fileSet element in the archetype-metadata.xml file. Here's an example:
<fileSet filtered="true" encoding="UTF-8"> <directory>src/main/javascript</directory> <includes> <include>**/*.js</include> </includes> <excludes> <!-- Exclude the generated archetype-metadata.xml from being filtered --> <exclude>archetype-metadata.xml</exclude> </excludes> <filtered>true</filtered> <delimiter>@</delimiter> </fileSet>
In this example, I've set the property delimiter to @ by adding the <delimiter> element inside the <fileSet> element. Now you can use @ as the property delimiter in your JavaScript files, like this:
var name = "@artifactId@";
During archetype generation, Maven will replace @artifactId@ with the actual value of the artifactId property defined by the user.
Make sure to update your archetype-metadata.xml file in the appropriate location in your Maven project hierarchy. After making this change, regenerate your archetype and try creating a new project from it. The generated JavaScript files should now contain the replaced property values.
Remember to update your project's pom.xml or other configuration files to reflect the new property delimiter if necessary.