Skip to main content

Customization Support

Custom Anchors

Custom anchors are points in the templates where developers can add custom code so that the code is reinserted after re-expanding.

You can add a custom anchor to the template of an Expander or Feature by using @custom:{anchor-name}:

base() ::= <<
package <packageName>;

public class <className> {
@custom:fields
}
>>

This will generate a start and end anchor to mark where developers can add custom code.

package org.example;

public class MyClass {
// anchor:custom-fields:start
// anchor:custom-fields:end
}
Custom Anchor Names

Custom Anchor names should be unique within each Artifact. When you have a part of the template that reoccurs, you can use interpolation to create a unique name.

E.g. this template contains a custom anchor for each field to add annotations:

base() ::= <<
package <packageName>;

public class <className> {
<fields:fieldDeclaration();separator="\n">
}
>>

fieldDeclaration(field) ::= <<
@custom:<field.name>-annotations
private <field.type> <field.name>;
>>

Harvesting ext code

Ext code in JEE Application Projects

In JEE Application projects, the base expanders generate /ext directories that are harvested. Expansion resources that expand into these projects do not need to implement ext harvesting.

Harvesting ext code is implemented by expansion steps, which copy the code from and to the harvest directories in the source repository.

The harvest-expanders provide a reusable way to harvest ext code. It has a builtin mechanism that remembers which files are generated by expanders. This allows it to harvest only files that were added by the developer, so that the custom files no longer need to be placed in a separate ext directory.

To use them:

  1. Add the harvest expanders as a dependency to your expansion resource.
pom.xml
<!-- pom.xml -->
<dependencies>
<dependency>
<groupId>net.democritus.expander</groupId>
<artifactId>harvest-expanders</artifactId>
<version>${harvest-expanders.version}</version>
</dependency>
</dependencies>

Add a rule that adds the #harvest.mixedSourceFiles tag to the program and module elements of your model.

data/elementTagRules.xml
<dataResource type="expansionControl::ElementTagRule">
<tag name="Enable mixed source harvest on Application">
#harvest.mixedSourceFiles :- instanceOf(elements::Application)
</tag>
<tag name="Enable mixed source harvest on Component">
#harvest.mixedSourceFiles :- instanceOf(elements::Component)
</tag>
</dataResource>

Finally, create an Expander to generate a .harvestInclude file. This file specifies which directories are eligible to harvest. Each line contains a glob of a relative path to include, lines starting with # are ignored. For example:

expansions/myapp/.harvestInclude
# include java sources
src/main/java/**

# include docs
docs/*