Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<json-schema-validator-version>2.2.14</json-schema-validator-version>
<jackson-version>2.11.2</jackson-version>
<jackson-version>2.13.3</jackson-version>
</properties>

<dependencies>
Expand All @@ -59,7 +59,18 @@
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.3.0</version>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-shared-utils</artifactId>
<version>3.3.4</version>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
Expand All @@ -83,7 +94,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
<version>1.7.36</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -100,10 +111,15 @@
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.7.0</version>
<version>5.8.2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-classworlds</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -113,7 +129,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.10.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
Expand All @@ -122,7 +138,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<version>3.0.0-M7</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down Expand Up @@ -154,7 +170,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.2.1</version>
<version>3.3.0</version>
<executions>
<execution>
<id>integration-test</id>
Expand Down Expand Up @@ -205,7 +221,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M3</version>
<version>3.0.0</version>
<executions>
<execution>
<id>enforce-versions</id>
Expand Down Expand Up @@ -251,5 +267,22 @@
</plugins>
</build>
</profile>
<profile>
<id>java9+</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>8</release>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
1 change: 1 addition & 0 deletions src/it/skip-nonexisting-basedir/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.buildResult = success
43 changes: 43 additions & 0 deletions src/it/skip-nonexisting-basedir/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.sylvainlaurent.maven.it</groupId>
<artifactId>validate-it</artifactId>
<version>1.0-SNAPSHOT</version>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>validate</goal>
</goals>
<configuration>
<validationSets>
<validationSet>
<baseDir>${project.basedir}/doesnotexist</baseDir>
<includes>
<include>src/main/resources/*.yml</include>
</includes>
</validationSet>
</validationSets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2 changes: 2 additions & 0 deletions src/it/skip-nonexisting-basedir/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
return new File('target/it/skip-nonexisting-basedir/build.log')
.text.contains('does not exist or is not a directory. Skipping.')
5 changes: 3 additions & 2 deletions src/it/validate-succeeds/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
<detectDuplicateKeys>false</detectDuplicateKeys>
<validationSets>
<validationSet>
<jsonSchema>src/main/resources/swagger-schema.json</jsonSchema>
<baseDir>src</baseDir>
<jsonSchema>main/resources/swagger-schema.json</jsonSchema>
<includes>
<include>src/main/resources/swagger-*.yml</include>
<include>main/resources/swagger-*.yml</include>
</includes>
</validationSet>
</validationSets>
Expand Down
2 changes: 2 additions & 0 deletions src/it/validate-succeeds/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
return !(new File('target/it/validate-succeeds/build.log')
.text.contains('does not exist or is not a directory. Skipping.'))
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
Expand All @@ -29,7 +30,7 @@ public class ValidateMojo extends AbstractMojo {
* and <code>&lt;exclude&gt;</code> elements respectively to specify lists of file masks of
* included and excluded files. The file masks are treated as paths relative to
* <code>${project.basedir}</code> and their syntax is that of
* <code>org.codehaus.plexus.util.DirectoryScanner</code>.</p>
* <code>org.apache.maven.shared.utils.io.DirectoryScanner</code>.</p>
*
* <p>JSON schema can be specified with <code>&lt;jsonSchema&gt;</code> element.</p>
*/
Expand Down Expand Up @@ -70,6 +71,19 @@ public class ValidateMojo extends AbstractMojo {
@Parameter(defaultValue = "false")
private boolean allowTrailingComma;

/**
* Set to <code>true</code> to follow symlinks.
*/
@Parameter(defaultValue = "true")
private boolean followSymlinks;

/**
* Set to <code>true</code> to include default excludes. Default excludes contain excludes for backup files and
* SCM directories.
*/
@Parameter(defaultValue = "true")
private boolean addDefaultExcludes;

@Override
public void execute() throws MojoExecutionException {
boolean encounteredError = false;
Expand All @@ -80,15 +94,27 @@ public void execute() throws MojoExecutionException {
}

for (final ValidationSet set : validationSets) {
InputStream inputStream = openJsonSchema(set.getJsonSchema());
final ValidationService validationService = new ValidationService(
inputStream,
allowEmptyFiles,
detectDuplicateKeys,
allowJsonComments,
allowTrailingComma);

final File[] files = set.getFiles(basedir);
final String validationSetBaseDirStr = set.getBaseDir();
final File validationSetBaseDir = validationSetBaseDirStr != null
? new File(validationSetBaseDirStr) : this.basedir;
final String setJsonSchema = set.getJsonSchema();
final ValidationService validationService;
try (InputStream inputStream = openJsonSchema(
setJsonSchema != null
? new File(validationSetBaseDir, setJsonSchema).toString()
: null
)) {
validationService = new ValidationService(
inputStream,
allowEmptyFiles,
detectDuplicateKeys,
allowJsonComments,
allowTrailingComma);
} catch (IOException ex) {
throw new MojoExecutionException("Failed to read schema", ex);
}

final File[] files = ValidationSet.getFiles(set, getLog(), basedir, followSymlinks, addDefaultExcludes);

for (final File file : files) {
if (verbose) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.sylvainlaurent.maven.yamljsonvalidator;

import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.MissingNode;
Expand Down Expand Up @@ -41,8 +42,8 @@ public ValidationService(final InputStream schemaFile,
this.jsonMapper.enable(Feature.ALLOW_COMMENTS);
}
if (allowTrailingComma) {
this.jsonMapper.enable(Feature.ALLOW_TRAILING_COMMA);
this.yamlMapper.enable(Feature.ALLOW_TRAILING_COMMA);
this.jsonMapper.enable(JsonReadFeature.ALLOW_TRAILING_COMMA.mappedFeature());
this.yamlMapper.enable(JsonReadFeature.ALLOW_TRAILING_COMMA.mappedFeature());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.sylvainlaurent.maven.yamljsonvalidator;

import java.io.File;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.shared.utils.io.DirectoryScanner;

import org.codehaus.plexus.util.DirectoryScanner;
import java.io.File;

public class ValidationSet {
/**
Expand All @@ -14,6 +15,8 @@ public class ValidationSet {

private String[] excludes;

private String baseDir;

public String getJsonSchema() {
return jsonSchema;
}
Expand All @@ -38,21 +41,45 @@ public void setExcludes(final String[] excludes) {
this.excludes = excludes;
}

public File[] getFiles(final File basedir) {
public String getBaseDir() {
return baseDir;
}

public void setBaseDir(String baseDir) {
this.baseDir = baseDir;
}

public static File[] getFiles(final ValidationSet validationSet, final Log log, final File projectBaseDir,
final boolean followSymlinks, final boolean addDefaultExcludes) {
final DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(basedir);
if (includes != null && includes.length > 0) {
ds.setIncludes(includes);
final String validationSetBaseDirStr = validationSet.getBaseDir();
final File validationSetBaseDir = validationSetBaseDirStr != null
? new File(validationSetBaseDirStr) : projectBaseDir;
ds.setBasedir(validationSetBaseDir);
ds.setFollowSymlinks(followSymlinks);
if (addDefaultExcludes) {
ds.addDefaultExcludes();
}
if (validationSet.includes != null && validationSet.includes.length > 0) {
ds.setIncludes(validationSet.includes);
}
if (excludes != null && excludes.length > 0) {
ds.setExcludes(excludes);
if (validationSet.excludes != null && validationSet.excludes.length > 0) {
ds.setExcludes(validationSet.excludes);
}

if (validationSetBaseDir == null
|| !validationSetBaseDir.exists()
|| !validationSetBaseDir.isDirectory()) {
log.warn("Directory " + validationSetBaseDir + " does not exist or is not a directory. Skipping.");
return new File[0];
}

ds.scan();
final String[] filePaths = ds.getIncludedFiles();
final File[] files = new File[filePaths.length];

for (int i = 0; i < filePaths.length; i++) {
files[i] = new File(basedir, filePaths[i]);
files[i] = new File(validationSetBaseDir, filePaths[i]);
}

return files;
Expand Down