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..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 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 - * Velocity Users Guide. + * Velocity Users Guide + * relative to {@code velocityBasedir}. */ @Parameter - private List templates; + 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,16 +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() - .map( File::toPath ) - .map( basedir::relativize ) - .map( Path::toString ) - .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 ); } @@ -105,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: