Skip to content

Commit

Permalink
add velocityBasedir to select where to load (shared) .vm from (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Jan 19, 2023
1 parent 0e5036f commit fe34ce9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,26 @@ public class ModelloVelocityMojo
/**
* The output directory of the generated files.
*/
@Parameter( defaultValue = "${project.build.directory}/generated-sources/modello", required = true )
@Parameter( defaultValue = "${project.build.directory}/generated-sources/modello" )
private File outputDirectory;

/**
* A list of template files to be run against the loaded modello model.
* The directory where Velocity templates are looked for.
*/
@Parameter( defaultValue = "${project.basedir}" )
private File velocityBasedir;

/**
* A list of template paths to be run against the loaded Modello model.
* Those are {@code .vm} files as described in the
* <a href="https://velocity.apache.org/engine/devel/user-guide.html">Velocity Users Guide</a>.
* <a href="https://velocity.apache.org/engine/devel/user-guide.html">Velocity Users Guide</a>
* relative to {@code velocityBasedir}.
*/
@Parameter
private List<File> templates;
private List<String> templates;

/**
* A list of parameters using the syntax {@code key=value}.
* A list of parameters, using the syntax {@code key=value}.
* Those parameters will be made accessible to the templates.
*/
@Parameter
Expand All @@ -87,16 +94,15 @@ protected String getGeneratorType()
protected void customizeParameters( Properties parameters )
{
super.customizeParameters( parameters );
Map<String, String> params = this.params != null ? this.params.stream().collect( Collectors.toMap(
s -> s.substring( 0, s.indexOf( '=' ) ), s -> s.substring( s.indexOf( '=' ) + 1 )
) ) : Collections.emptyMap();
parameters.put( "basedir", Objects.requireNonNull( getBasedir(), "basedir is null" ) );
Path basedir = Paths.get( getBasedir() );
parameters.put( VelocityGenerator.VELOCITY_TEMPLATES, templates.stream()
.map( File::toPath )
.map( basedir::relativize )
.map( Path::toString )
.collect( Collectors.joining( "," ) ) );

Map<String, String> params = this.params == null ? Collections.emptyMap()
: this.params.stream().collect( Collectors.toMap( s -> s.substring( 0, s.indexOf( '=' ) ),
s -> s.substring( s.indexOf( '=' ) + 1 ) ) );

parameters.put( VelocityGenerator.VELOCITY_BASEDIR, velocityBasedir.getAbsolutePath() );

parameters.put( VelocityGenerator.VELOCITY_TEMPLATES,
templates.stream().collect( Collectors.joining( "," ) ) );
parameters.put( VelocityGenerator.VELOCITY_PARAMETERS, params );
}

Expand All @@ -105,13 +111,9 @@ protected boolean producesCompilableResult()
return true;
}

@Override
public File getOutputDirectory()
{
return outputDirectory;
}

public void setOutputDirectory( File outputDirectory )
{
this.outputDirectory = outputDirectory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
public class VelocityGenerator
extends AbstractModelloGenerator
{
public static final String VELOCITY_BASEDIR = "modello.velocity.basedir";

public static final String VELOCITY_TEMPLATES = "modello.velocity.templates";

public static final String VELOCITY_PARAMETERS = "modello.velocity.parameters";
Expand All @@ -61,7 +63,7 @@ public void generate( Model model, Properties parameters ) throws ModelloExcepti
String output = getParameter( parameters, ModelloParameterConstants.OUTPUT_DIRECTORY );

Properties props = new Properties();
props.put( "resource.loader.file.path", getParameter( parameters, "basedir" ) );
props.put( "resource.loader.file.path", getParameter( parameters, VELOCITY_BASEDIR ) );
RuntimeInstance velocity = new RuntimeInstance();
velocity.init( props );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<section name="Velocity Processing">

<p>The plugin is configured with a list of <code>template</code> files to evaluate.</p>
<p>The plugin is configured with a list of <code>template</code> files to evaluate, rfelative to <code>velocityBasedir</code> (which defaults to Maven's <code>${project.basedir}</code>).</p>
<p>During template evaluation, <code>#MODELLO-VELOCITY#SAVE-OUTPUT-TO {relative path to file}</code> pseudo macro is available to send the rendered content to a file.</p>
<p>The Velocity context contains some variables related to the Modello model context that you can use:
<table>
Expand Down

0 comments on commit fe34ce9

Please sign in to comment.