Skip to content

Commit 0a38c5e

Browse files
authored
CAMEL-23820: support custom directories in YAML DSL validator plugin
Add a 'directories' parameter to the camel-yaml-dsl-validator-maven-plugin that allows users to specify additional directories to scan for YAML files, beyond the default Maven resource directories. Both relative and absolute paths are supported. Also fixes a pre-existing artifactId typo in the documentation examples. Closes #24234
1 parent 2d7da64 commit 0a38c5e

5 files changed

Lines changed: 218 additions & 6 deletions

File tree

docs/user-manual/modules/ROOT/pages/camel-yaml-dsl-validator-maven-plugin.adoc

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ You can also enable the plugin to run automatically as part of the build to catc
2626
----
2727
<plugin>
2828
<groupId>org.apache.camel</groupId>
29-
<artifactId>camel-yaml-dsl-validate-maven-plugin</artifactId>
29+
<artifactId>camel-yaml-dsl-validator-maven-plugin</artifactId>
3030
<executions>
3131
<execution>
3232
<phase>process-classes</phase>
@@ -48,7 +48,7 @@ changed accordingly to `process-test-classes` as shown below:
4848
----
4949
<plugin>
5050
<groupId>org.apache.camel</groupId>
51-
<artifactId>camel-yaml-dsl-validate-maven-plugin</artifactId>
51+
<artifactId>camel-yaml-dsl-validator-maven-plugin</artifactId>
5252
<executions>
5353
<execution>
5454
<configuration>
@@ -102,6 +102,7 @@ The maven plugin *validate* goal supports the following options which can be con
102102
| includes | | To filter the names of YAML files to only include files matching any of the given list of patterns (wildcard and regular expression). Multiple values can be separated by comma.
103103
| excludes | | To filter the names of YAML files to exclude files matching any of the given list of patterns (wildcard and regular expression). Multiple values can be separated by comma.
104104
| onlyCamelYamlExt | false | Whether to only accept files with xxx.camel.yaml as file name. By default, all .yaml files are accepted.
105+
| directories | | Additional directories to scan for YAML files. By default, only the project's resource directories are scanned.
105106
|===
106107

107108
For example to excludes a specific file:
@@ -113,6 +114,34 @@ mvn camel-yaml-dsl-validator:validate -Dcamel.excludes=cheese.yaml
113114

114115
Notice that you must prefix the `-D` command argument with `camel.`, eg `camel.excludes` as the option name.
115116

117+
=== Validating with additional directories
118+
119+
If your YAML route files are stored in a location outside the standard Maven resource directories, you can specify additional directories to scan:
120+
121+
[source,xml]
122+
----
123+
<plugin>
124+
<groupId>org.apache.camel</groupId>
125+
<artifactId>camel-yaml-dsl-validator-maven-plugin</artifactId>
126+
<executions>
127+
<execution>
128+
<configuration>
129+
<directories>
130+
<directory>src/main/routes</directory>
131+
<directory>src/main/camel</directory>
132+
</directories>
133+
</configuration>
134+
<phase>process-classes</phase>
135+
<goals>
136+
<goal>validate</goal>
137+
</goals>
138+
</execution>
139+
</executions>
140+
</plugin>
141+
----
142+
143+
Both relative and absolute paths are supported. Relative paths are resolved against the project base directory.
144+
116145
=== Validating include test
117146

118147
If you have a Maven project then you can run the plugin to validate the endpoints in the unit test source code as well.

dsl/camel-yaml-dsl/camel-yaml-dsl-validator-maven-plugin/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@
8585
<scope>provided</scope>
8686
</dependency>
8787

88+
<dependency>
89+
<groupId>org.junit.jupiter</groupId>
90+
<artifactId>junit-jupiter</artifactId>
91+
<scope>test</scope>
92+
</dependency>
8893
</dependencies>
8994

9095
<build>

dsl/camel-yaml-dsl/camel-yaml-dsl-validator-maven-plugin/src/main/docs/camel-yaml-dsl-validator-maven-plugin.adoc

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ You can also enable the plugin to run automatically as part of the build to catc
2626
----
2727
<plugin>
2828
<groupId>org.apache.camel</groupId>
29-
<artifactId>camel-yaml-dsl-validate-maven-plugin</artifactId>
29+
<artifactId>camel-yaml-dsl-validator-maven-plugin</artifactId>
3030
<executions>
3131
<execution>
3232
<phase>process-classes</phase>
@@ -48,7 +48,7 @@ changed accordingly to `process-test-classes` as shown below:
4848
----
4949
<plugin>
5050
<groupId>org.apache.camel</groupId>
51-
<artifactId>camel-yaml-dsl-validate-maven-plugin</artifactId>
51+
<artifactId>camel-yaml-dsl-validator-maven-plugin</artifactId>
5252
<executions>
5353
<execution>
5454
<configuration>
@@ -102,6 +102,7 @@ The maven plugin *validate* goal supports the following options which can be con
102102
| includes | | To filter the names of YAML files to only include files matching any of the given list of patterns (wildcard and regular expression). Multiple values can be separated by comma.
103103
| excludes | | To filter the names of YAML files to exclude files matching any of the given list of patterns (wildcard and regular expression). Multiple values can be separated by comma.
104104
| onlyCamelYamlExt | false | Whether to only accept files with xxx.camel.yaml as file name. By default, all .yaml files are accepted.
105+
| directories | | Additional directories to scan for YAML files. By default, only the project's resource directories are scanned.
105106
|===
106107

107108
For example to excludes a specific file:
@@ -113,6 +114,34 @@ mvn camel-yaml-dsl-validator:validate -Dcamel.excludes=cheese.yaml
113114

114115
Notice that you must prefix the `-D` command argument with `camel.`, eg `camel.excludes` as the option name.
115116

117+
=== Validating with additional directories
118+
119+
If your YAML route files are stored in a location outside the standard Maven resource directories, you can specify additional directories to scan:
120+
121+
[source,xml]
122+
----
123+
<plugin>
124+
<groupId>org.apache.camel</groupId>
125+
<artifactId>camel-yaml-dsl-validator-maven-plugin</artifactId>
126+
<executions>
127+
<execution>
128+
<configuration>
129+
<directories>
130+
<directory>src/main/routes</directory>
131+
<directory>src/main/camel</directory>
132+
</directories>
133+
</configuration>
134+
<phase>process-classes</phase>
135+
<goals>
136+
<goal>validate</goal>
137+
</goals>
138+
</execution>
139+
</executions>
140+
</plugin>
141+
----
142+
143+
Both relative and absolute paths are supported. Relative paths are resolved against the project base directory.
144+
116145
=== Validating include test
117146

118147
If you have a Maven project then you can run the plugin to validate the endpoints in the unit test source code as well.

dsl/camel-yaml-dsl/camel-yaml-dsl-validator-maven-plugin/src/main/java/org/apache/camel/dsl/yaml/validator/ValidateMojo.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public class ValidateMojo extends AbstractMojo {
8383
@Parameter(property = "camel.includeTest", defaultValue = "false")
8484
private boolean includeTest;
8585

86+
/**
87+
* Additional directories to scan for YAML files. By default, only the project's resource directories are scanned.
88+
*/
89+
@Parameter(property = "camel.directories")
90+
private List<String> directories;
91+
8692
/**
8793
* To filter the names of YAML files to only include files matching any of the given list of patterns (wildcard and
8894
* regular expression). Multiple values can be separated by comma.
@@ -121,7 +127,7 @@ public void execute() throws MojoExecutionException {
121127

122128
// find all XML routes
123129
String ext = onlyCamelYamlExt ? ".camel.yaml" : ".yaml";
124-
findYamlRouters(yamlFiles, includeTest, ext, project);
130+
findYamlRouters(yamlFiles, includeTest, ext, directories, project);
125131
getLog().debug("Found " + yamlFiles.size() + " YAML files ...");
126132

127133
Map<File, List<Error>> reports = new LinkedHashMap<>();
@@ -264,7 +270,9 @@ private static String asRelativeFile(String name, MavenProject project) {
264270
return answer;
265271
}
266272

267-
private static void findYamlRouters(Set<File> yamlFiles, boolean includeTest, String ext, MavenProject project) {
273+
static void findYamlRouters(
274+
Set<File> yamlFiles, boolean includeTest, String ext,
275+
List<String> directories, MavenProject project) {
268276
for (Resource dir : project.getResources()) {
269277
finYamlFiles(new File(dir.getDirectory()), ext, yamlFiles);
270278
}
@@ -273,6 +281,15 @@ private static void findYamlRouters(Set<File> yamlFiles, boolean includeTest, St
273281
finYamlFiles(new File(dir.getDirectory()), ext, yamlFiles);
274282
}
275283
}
284+
if (directories != null) {
285+
for (String dir : directories) {
286+
File d = new File(dir);
287+
if (!d.isAbsolute()) {
288+
d = new File(project.getBasedir(), dir);
289+
}
290+
finYamlFiles(d, ext, yamlFiles);
291+
}
292+
}
276293
}
277294

278295
private static void finYamlFiles(File dir, String ext, Set<File> yamlFiles) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.dsl.yaml.validator;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import java.util.LinkedHashSet;
24+
import java.util.List;
25+
import java.util.Set;
26+
27+
import org.apache.maven.model.Resource;
28+
import org.apache.maven.project.MavenProject;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.io.TempDir;
31+
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
34+
35+
class ValidateMojoTest {
36+
37+
@TempDir
38+
Path tempDir;
39+
40+
@Test
41+
void findYamlRoutersScansCustomDirectories() throws IOException {
42+
Path customDir = tempDir.resolve("custom-routes");
43+
Files.createDirectories(customDir);
44+
Files.writeString(customDir.resolve("my-route.yaml"), "- route:\n from:\n uri: timer:yaml\n");
45+
46+
MavenProject project = new MavenProject();
47+
project.setFile(new File(tempDir.toFile(), "pom.xml"));
48+
49+
Set<File> yamlFiles = new LinkedHashSet<>();
50+
ValidateMojo.findYamlRouters(
51+
yamlFiles, false, ".yaml",
52+
List.of(customDir.toString()), project);
53+
54+
assertEquals(1, yamlFiles.size());
55+
assertTrue(yamlFiles.iterator().next().getName().endsWith("my-route.yaml"));
56+
}
57+
58+
@Test
59+
void findYamlRoutersScansRelativeCustomDirectories() throws IOException {
60+
Path customDir = tempDir.resolve("src/main/routes");
61+
Files.createDirectories(customDir);
62+
Files.writeString(customDir.resolve("route.yaml"), "- route:\n from:\n uri: timer:yaml\n");
63+
64+
MavenProject project = new MavenProject();
65+
project.setFile(new File(tempDir.toFile(), "pom.xml"));
66+
67+
Set<File> yamlFiles = new LinkedHashSet<>();
68+
ValidateMojo.findYamlRouters(
69+
yamlFiles, false, ".yaml",
70+
List.of("src/main/routes"), project);
71+
72+
assertEquals(1, yamlFiles.size());
73+
assertTrue(yamlFiles.iterator().next().getName().endsWith("route.yaml"));
74+
}
75+
76+
@Test
77+
void findYamlRoutersWithNullDirectories() {
78+
MavenProject project = new MavenProject();
79+
project.setFile(new File(tempDir.toFile(), "pom.xml"));
80+
81+
Set<File> yamlFiles = new LinkedHashSet<>();
82+
ValidateMojo.findYamlRouters(
83+
yamlFiles, false, ".yaml",
84+
null, project);
85+
86+
assertTrue(yamlFiles.isEmpty());
87+
}
88+
89+
@Test
90+
void findYamlRoutersScansResourcesAndCustomDirectories() throws IOException {
91+
Path resourceDir = tempDir.resolve("src/main/resources");
92+
Files.createDirectories(resourceDir);
93+
Files.writeString(resourceDir.resolve("resource-route.yaml"), "- route:\n from:\n uri: timer:yaml\n");
94+
95+
Path customDir = tempDir.resolve("custom");
96+
Files.createDirectories(customDir);
97+
Files.writeString(customDir.resolve("custom-route.yaml"), "- route:\n from:\n uri: timer:yaml\n");
98+
99+
MavenProject project = new MavenProject();
100+
project.setFile(new File(tempDir.toFile(), "pom.xml"));
101+
102+
Resource resource = new Resource();
103+
resource.setDirectory(resourceDir.toString());
104+
project.addResource(resource);
105+
106+
Set<File> yamlFiles = new LinkedHashSet<>();
107+
ValidateMojo.findYamlRouters(
108+
yamlFiles, false, ".yaml",
109+
List.of(customDir.toString()), project);
110+
111+
assertEquals(2, yamlFiles.size());
112+
}
113+
114+
@Test
115+
void findYamlRoutersFiltersByCamelYamlExtension() throws IOException {
116+
Path customDir = tempDir.resolve("routes");
117+
Files.createDirectories(customDir);
118+
Files.writeString(customDir.resolve("my-route.camel.yaml"), "- route:\n from:\n uri: timer:yaml\n");
119+
Files.writeString(customDir.resolve("other.yaml"), "key: value\n");
120+
121+
MavenProject project = new MavenProject();
122+
project.setFile(new File(tempDir.toFile(), "pom.xml"));
123+
124+
Set<File> yamlFiles = new LinkedHashSet<>();
125+
ValidateMojo.findYamlRouters(
126+
yamlFiles, false, ".camel.yaml",
127+
List.of(customDir.toString()), project);
128+
129+
assertEquals(1, yamlFiles.size());
130+
assertTrue(yamlFiles.iterator().next().getName().endsWith("my-route.camel.yaml"));
131+
}
132+
}

0 commit comments

Comments
 (0)