Skip to content

Commit fe34ce9

Browse files
authored
add velocityBasedir to select where to load (shared) .vm from (#292)
1 parent 0e5036f commit fe34ce9

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

modello-maven-plugin/src/main/java/org/codehaus/modello/maven/ModelloVelocityMojo.java

+22-20
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,26 @@ public class ModelloVelocityMojo
6161
/**
6262
* The output directory of the generated files.
6363
*/
64-
@Parameter( defaultValue = "${project.build.directory}/generated-sources/modello", required = true )
64+
@Parameter( defaultValue = "${project.build.directory}/generated-sources/modello" )
6565
private File outputDirectory;
6666

6767
/**
68-
* A list of template files to be run against the loaded modello model.
68+
* The directory where Velocity templates are looked for.
69+
*/
70+
@Parameter( defaultValue = "${project.basedir}" )
71+
private File velocityBasedir;
72+
73+
/**
74+
* A list of template paths to be run against the loaded Modello model.
6975
* Those are {@code .vm} files as described in the
70-
* <a href="https://velocity.apache.org/engine/devel/user-guide.html">Velocity Users Guide</a>.
76+
* <a href="https://velocity.apache.org/engine/devel/user-guide.html">Velocity Users Guide</a>
77+
* relative to {@code velocityBasedir}.
7178
*/
7279
@Parameter
73-
private List<File> templates;
80+
private List<String> templates;
7481

7582
/**
76-
* A list of parameters using the syntax {@code key=value}.
83+
* A list of parameters, using the syntax {@code key=value}.
7784
* Those parameters will be made accessible to the templates.
7885
*/
7986
@Parameter
@@ -87,16 +94,15 @@ protected String getGeneratorType()
8794
protected void customizeParameters( Properties parameters )
8895
{
8996
super.customizeParameters( parameters );
90-
Map<String, String> params = this.params != null ? this.params.stream().collect( Collectors.toMap(
91-
s -> s.substring( 0, s.indexOf( '=' ) ), s -> s.substring( s.indexOf( '=' ) + 1 )
92-
) ) : Collections.emptyMap();
93-
parameters.put( "basedir", Objects.requireNonNull( getBasedir(), "basedir is null" ) );
94-
Path basedir = Paths.get( getBasedir() );
95-
parameters.put( VelocityGenerator.VELOCITY_TEMPLATES, templates.stream()
96-
.map( File::toPath )
97-
.map( basedir::relativize )
98-
.map( Path::toString )
99-
.collect( Collectors.joining( "," ) ) );
97+
98+
Map<String, String> params = this.params == null ? Collections.emptyMap()
99+
: this.params.stream().collect( Collectors.toMap( s -> s.substring( 0, s.indexOf( '=' ) ),
100+
s -> s.substring( s.indexOf( '=' ) + 1 ) ) );
101+
102+
parameters.put( VelocityGenerator.VELOCITY_BASEDIR, velocityBasedir.getAbsolutePath() );
103+
104+
parameters.put( VelocityGenerator.VELOCITY_TEMPLATES,
105+
templates.stream().collect( Collectors.joining( "," ) ) );
100106
parameters.put( VelocityGenerator.VELOCITY_PARAMETERS, params );
101107
}
102108

@@ -105,13 +111,9 @@ protected boolean producesCompilableResult()
105111
return true;
106112
}
107113

114+
@Override
108115
public File getOutputDirectory()
109116
{
110117
return outputDirectory;
111118
}
112-
113-
public void setOutputDirectory( File outputDirectory )
114-
{
115-
this.outputDirectory = outputDirectory;
116-
}
117119
}

modello-plugins/modello-plugin-velocity/src/main/java/org/codehaus/modello/plugin/velocity/VelocityGenerator.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
public class VelocityGenerator
4646
extends AbstractModelloGenerator
4747
{
48+
public static final String VELOCITY_BASEDIR = "modello.velocity.basedir";
49+
4850
public static final String VELOCITY_TEMPLATES = "modello.velocity.templates";
4951

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

6365
Properties props = new Properties();
64-
props.put( "resource.loader.file.path", getParameter( parameters, "basedir" ) );
66+
props.put( "resource.loader.file.path", getParameter( parameters, VELOCITY_BASEDIR ) );
6567
RuntimeInstance velocity = new RuntimeInstance();
6668
velocity.init( props );
6769

modello-plugins/modello-plugin-velocity/src/site/xdoc/index.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<section name="Velocity Processing">
1919

20-
<p>The plugin is configured with a list of <code>template</code> files to evaluate.</p>
20+
<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>
2121
<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>
2222
<p>The Velocity context contains some variables related to the Modello model context that you can use:
2323
<table>

0 commit comments

Comments
 (0)