Skip to content

add velocityBasedir to select where to load (shared) .vm from #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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