I created a Maven Archetype. My META-INF/maven/archetype-metadata.xml
looks like this:
<archetype-descriptor xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8" >
<directory>src/main/java</directory>
</fileSet>
</fileSets>
</archetype-descriptor>
This works, as in that it creates a Java source folder and puts my classes in the package as defined by the groupId
and the artifactId
.
However, I want to modify his package name. For example, if my groupId is com.example
and my artifactId wvdz
, then my package should be:
com.example.wvdz.mypackage
Question: How do I accomplish this?
To accomplish your objectives and since you are already using the
packaged
attribute totrue
(explained later), you can simply add directories to your path below.Keeping the same configuration, with an additional
include
element as following:You could then place under
src/main/java/mypackage
your Java sources templated where the package statement would be as following:Note the
.mypackage
reflects exactly themypackage
folder directly undersrc/main/java
. However, when creating the archetype, Maven will then place as a folder (and as such as a package) in between the ${package} property value, which by default would be the ${groupId}.You can always pass the
-Dpackage
property and override it the default value (thegroupId
), which will then be used as a prefix of the package, based on the template above.This happens because of the
packaged
attribute set totrue
in thefileSet
section above. In this casetrue
means: add to it the folder hierarchy specified by the${package}
property. Setting it atfalse
would result in${package}
ignored, which can be used if you really want to hard-code the folder structure and obviously reflect it in to thepackage
statement of your Java code, for consistency.The behavior above is documented in the official How is metadata about an archetype stored?:
And also:
The same details (about the
packaged
property) can also be found in the official Archetype Descriptor Model.Another possible solution is to use an additional property or define your
package
property value directly in thearchetype-metadata.xml
file as following:Note the new
requiredProperties
section: here we are setting the default value for thepackage
property, no need to provide it at runtime anymore (yet possible to override the value above though).As such, the Java source template under
src/main/java
(no need for further static folders) would simply be:During the creation (
archetype:generate
) Maven will then use thecom.sample.something.mypackage
as package value (in the Java source file) and populate thepackageInPathFormat
property with the valuecom/sample/something/mypackage
(the same property, but in path format) and create the desired package hierarchy, consistent with what the Java source code would expect to be placed in.