Skip to content

Commit 10923ec

Browse files
committed
Default velocityBasedir to project basedir in ModelloTask like the mojo
1 parent fde1eca commit 10923ec

3 files changed

Lines changed: 43 additions & 24 deletions

File tree

‎modello-ant-task/README.md‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ The `<modello>` task supports several attributes and nested elements to configur
3333

3434
### Attributes
3535

36-
| Attribute | Type | Description | Required | Default |
37-
|:-------------------------------|:----------|:--------------------------------------------------------------------------------------|:--------:|:---------:|
38-
| **`version`** | `String` | The model version to generate (e.g., `1.0.0`). | **Yes** | — |
39-
| **`outputDirectory`** | `File` | The directory where the generated files will be written. | **Yes** | — |
40-
| **`javaSource`** | `String` | Target Java version for generated source code. | No | `8` |
41-
| **`encoding`** | `String` | Character encoding for generated files. | No | `"utf-8"` |
42-
| **`packageWithVersion`** | `boolean` | Whether to append the version to the package name (`"true"` / `"false"`). | No | `false` |
43-
| **`packagedVersions`** | `String` | Additional historical versions, each generated into a versioned package. | No | — |
44-
| **`domAsXpp3`** | `boolean` | Whether to generate DOM content as Xpp3Dom (`"true"`) or W3C DOM Element (`"false"`). | No | `true` |
45-
| **`licenseFile`** | `File` | Path to file containing license header text to prepend to generated files. | No | — |
46-
| **`licenseText`** | `String` | License header text to prepend to generated files. | No | — |
47-
| **`extendedClassnameSuffix`** | `String` | Writer class name suffix for the `xpp3-extended-writer` goal. | No | `Ex` |
48-
| **`xsdFileName`** | `String` | Custom output file name for generated XSD schema (`xsd` goal). | No | — |
49-
| **`enforceMandatoryElements`** | `boolean` | Whether to enforce mandatory elements in XSD (`"true"` / `"false"`). | No | `false` |
50-
| **`jsonSchemaFileName`** | `String` | Custom output file name for generated JSON Schema (`jsonschema` goal). | No | — |
51-
| **`firstVersion`** | `String` | Earliest version to document when generating documentation (`xdoc` goal). | No | — |
52-
| **`xdocFileName`** | `String` | Custom output file name for generated XDoc documentation (`xdoc` goal). | No | — |
53-
| **`velocityBasedir`** | `File` | Base directory of template files (required only for Velocity-based goals). | No | — |
36+
| Attribute | Type | Description | Required | Default |
37+
|:-------------------------------|:----------|:--------------------------------------------------------------------------------------|:--------:|:---------------:|
38+
| **`version`** | `String` | The model version to generate (e.g., `1.0.0`). | **Yes** | — |
39+
| **`outputDirectory`** | `File` | The directory where the generated files will be written. | **Yes** | — |
40+
| **`javaSource`** | `String` | Target Java version for generated source code. | No | `8` |
41+
| **`encoding`** | `String` | Character encoding for generated files. | No | `"utf-8"` |
42+
| **`packageWithVersion`** | `boolean` | Whether to append the version to the package name (`"true"` / `"false"`). | No | `false` |
43+
| **`packagedVersions`** | `String` | Additional historical versions, each generated into a versioned package. | No | — |
44+
| **`domAsXpp3`** | `boolean` | Whether to generate DOM content as Xpp3Dom (`"true"`) or W3C DOM Element (`"false"`). | No | `true` |
45+
| **`licenseFile`** | `File` | Path to file containing license header text to prepend to generated files. | No | — |
46+
| **`licenseText`** | `String` | License header text to prepend to generated files. | No | — |
47+
| **`extendedClassnameSuffix`** | `String` | Writer class name suffix for the `xpp3-extended-writer` goal. | No | `Ex` |
48+
| **`xsdFileName`** | `String` | Custom output file name for generated XSD schema (`xsd` goal). | No | — |
49+
| **`enforceMandatoryElements`** | `boolean` | Whether to enforce mandatory elements in XSD (`"true"` / `"false"`). | No | `false` |
50+
| **`jsonSchemaFileName`** | `String` | Custom output file name for generated JSON Schema (`jsonschema` goal). | No | — |
51+
| **`firstVersion`** | `String` | Earliest version to document when generating documentation (`xdoc` goal). | No | — |
52+
| **`xdocFileName`** | `String` | Custom output file name for generated XDoc documentation (`xdoc` goal). | No | — |
53+
| **`velocityBasedir`** | `File` | Base directory of Velocity templates (`velocity` goal). | No | project basedir |
5454

5555
### Nested Elements
5656

‎modello-ant-task/src/main/java/org/codehaus/modello/ant/ModelloTask.java‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,15 @@ public void execute() throws BuildException {
240240
// Arbitrary properties
241241
parameters.putAll(properties);
242242

243-
// Attach Velocity configs if provided
244-
if (velocityBasedir != null) {
245-
parameters.put("modello.velocity.basedir", velocityBasedir.getAbsolutePath());
246-
parameters.put("modello.velocity.templates", String.join(",", templates));
247-
parameters.put("modello.velocity.parameters", velocityParams);
243+
// Like ModelloVelocityMojo: basedir defaults to project basedir
244+
File basedir = velocityBasedir != null
245+
? velocityBasedir
246+
: getProject() != null ? getProject().getBaseDir() : null;
247+
if (basedir != null) {
248+
parameters.put("modello.velocity.basedir", basedir.getAbsolutePath());
248249
}
250+
parameters.put("modello.velocity.templates", String.join(",", templates));
251+
parameters.put("modello.velocity.parameters", velocityParams);
249252

250253
for (File modelFile : models) {
251254
log("Generating sources for " + modelFile.getName());

‎modello-ant-task/src/test/java/org/codehaus/modello/ant/ModelloTaskTest.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.file.Path;
2323

2424
import org.apache.tools.ant.BuildException;
25+
import org.apache.tools.ant.Project;
2526
import org.junit.jupiter.api.Test;
2627
import org.junit.jupiter.api.io.TempDir;
2728

@@ -307,6 +308,15 @@ public void testInvalidModelFileThrowsBuildException() throws IOException {
307308

308309
@Test
309310
public void testVelocityGoal() throws IOException {
311+
assertVelocityGeneration(false);
312+
}
313+
314+
@Test
315+
public void testVelocityGoalDefaultsToProjectBasedir() throws IOException {
316+
assertVelocityGeneration(true);
317+
}
318+
319+
private void assertVelocityGeneration(boolean projectBasedir) throws IOException {
310320
File modelFile = tempDir.resolve("test-velocity.mdo").toFile();
311321
try (FileWriter writer = new FileWriter(modelFile)) {
312322
writer.write("<model xmlns=\"http://codehaus-plexus.github.io/MODELLO/2.0.0\" "
@@ -341,7 +351,13 @@ public void testVelocityGoal() throws IOException {
341351
ModelloTask task = new ModelloTask();
342352
task.setVersion("1.0.0");
343353
task.setOutputDirectory(outDir.toFile());
344-
task.setVelocityBasedir(velocityBasedir);
354+
if (projectBasedir) {
355+
Project project = new Project();
356+
project.setBaseDir(velocityBasedir);
357+
task.setProject(project);
358+
} else {
359+
task.setVelocityBasedir(velocityBasedir);
360+
}
345361

346362
ModelloTask.ModelElement modelElement = new ModelloTask.ModelElement();
347363
modelElement.setFile(modelFile);

0 commit comments

Comments
 (0)