From b417905c0a8c14032c7ac322494f79153b7d2c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Boutemy?= Date: Sun, 15 Jan 2023 19:18:57 +0100 Subject: [PATCH 1/2] support overriding basedir --- .../org/codehaus/modello/maven/ModelloVelocityMojo.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/ModelloVelocityMojo.java b/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/ModelloVelocityMojo.java index b33346ac8..24988c818 100644 --- a/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/ModelloVelocityMojo.java +++ b/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/ModelloVelocityMojo.java @@ -65,12 +65,12 @@ public class ModelloVelocityMojo private File outputDirectory; /** - * A list of template files to be run against the loaded modello model. + * A list of template paths to be run against the loaded modello model. * Those are {@code .vm} files as described in the * Velocity Users Guide. */ @Parameter - private List templates; + private List templates; /** * A list of parameters using the syntax {@code key=value}. @@ -93,9 +93,6 @@ protected void customizeParameters( Properties parameters ) 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( "," ) ) ); parameters.put( VelocityGenerator.VELOCITY_PARAMETERS, params ); } From ffac2771c69a840bee53a1c785629fbb4d3128f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Boutemy?= Date: Wed, 18 Jan 2023 08:19:00 +0100 Subject: [PATCH 2/2] introduce velocityBasedir parameter --- .../modello/maven/ModelloVelocityMojo.java | 37 +++++++++++-------- .../plugin/velocity/VelocityGenerator.java | 4 +- .../src/site/xdoc/index.xml | 2 +- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/ModelloVelocityMojo.java b/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/ModelloVelocityMojo.java index 24988c818..109f4e413 100644 --- a/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/ModelloVelocityMojo.java +++ b/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/ModelloVelocityMojo.java @@ -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 paths 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 - * Velocity Users Guide. + * Velocity Users Guide + * relative to {@code velocityBasedir}. */ @Parameter private List 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 @@ -87,13 +94,15 @@ protected String getGeneratorType() protected void customizeParameters( Properties parameters ) { super.customizeParameters( parameters ); - Map 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() - .collect( Collectors.joining( "," ) ) ); + + Map 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 ); } @@ -102,13 +111,9 @@ protected boolean producesCompilableResult() return true; } + @Override public File getOutputDirectory() { return outputDirectory; } - - public void setOutputDirectory( File outputDirectory ) - { - this.outputDirectory = outputDirectory; - } } diff --git a/modello-plugins/modello-plugin-velocity/src/main/java/org/codehaus/modello/plugin/velocity/VelocityGenerator.java b/modello-plugins/modello-plugin-velocity/src/main/java/org/codehaus/modello/plugin/velocity/VelocityGenerator.java index d47daae27..c5010c1cb 100644 --- a/modello-plugins/modello-plugin-velocity/src/main/java/org/codehaus/modello/plugin/velocity/VelocityGenerator.java +++ b/modello-plugins/modello-plugin-velocity/src/main/java/org/codehaus/modello/plugin/velocity/VelocityGenerator.java @@ -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"; @@ -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 ); diff --git a/modello-plugins/modello-plugin-velocity/src/site/xdoc/index.xml b/modello-plugins/modello-plugin-velocity/src/site/xdoc/index.xml index e260e5642..0c4674588 100644 --- a/modello-plugins/modello-plugin-velocity/src/site/xdoc/index.xml +++ b/modello-plugins/modello-plugin-velocity/src/site/xdoc/index.xml @@ -17,7 +17,7 @@
-

The plugin is configured with a list of template files to evaluate.

+

The plugin is configured with a list of template files to evaluate, rfelative to velocityBasedir (which defaults to Maven's ${project.basedir}).

During template evaluation, #MODELLO-VELOCITY#SAVE-OUTPUT-TO {relative path to file} pseudo macro is available to send the rendered content to a file.

The Velocity context contains some variables related to the Modello model context that you can use: