jsonschema-generator icon indicating copy to clipboard operation
jsonschema-generator copied to clipboard

Maven plugin not working for JavaxValidation

Open sabz90 opened this issue 6 years ago • 7 comments

Hello, thanks for maintaining this wonderful project I am trying to use the maven plugin to generate schema I want to enable JavaxValidation module. However I get this error while doing a mvn clean install:

[ERROR] Failed to execute goal com.github.victools:jsonschema-maven-plugin:4.13.0:generate (default) on project contracts: Execution default of goal com.github.victools:jsonschema-maven-plugin:4.13.0:generate failed: A required class was missing while executing com.github.victools:jsonschema-maven-plugin:4.13.0:generate: javax/validation/constraints/NotNull
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>com.github.victools:jsonschema-maven-plugin:4.13.0
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-maven-plugin/4.13.0/jsonschema-maven-plugin-4.13.0.jar
[ERROR] urls[1] = file:/Users/secretuser/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.10.3/jackson-core-2.10.3.jar
[ERROR] urls[2] = file:/Users/secretuser/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.10.3/jackson-databind-2.10.3.jar
[ERROR] urls[3] = file:/Users/secretuser/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.10.3/jackson-annotations-2.10.3.jar
[ERROR] urls[4] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-generator/4.13.0/jsonschema-generator-4.13.0.jar
[ERROR] urls[5] = file:/Users/secretuser/.m2/repository/com/fasterxml/classmate/1.5.1/classmate-1.5.1.jar
[ERROR] urls[6] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-module-jackson/4.13.0/jsonschema-module-jackson-4.13.0.jar
[ERROR] urls[7] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-module-swagger-1.5/4.13.0/jsonschema-module-swagger-1.5-4.13.0.jar
[ERROR] urls[8] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-module-swagger-2/4.13.0/jsonschema-module-swagger-2-4.13.0.jar
[ERROR] urls[9] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-module-javax-validation/4.13.0/jsonschema-module-javax-validation-4.13.0.jar
[ERROR] urls[10] = file:/Users/secretuser/.m2/repository/org/reflections/reflections/0.9.12/reflections-0.9.12.jar
[ERROR] urls[11] = file:/Users/secretuser/.m2/repository/org/javassist/javassist/3.26.0-GA/javassist-3.26.0-GA.jar
[ERROR] urls[12] = file:/Users/secretuser/.m2/repository/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import  from realm ClassRealm[maven.api, parent: null]]
[ERROR] 
[ERROR] -----------------------------------------------------
[ERROR] : javax.validation.constraints.NotNull
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]

The same works fine when I disable JavaxValidation and enable just Jackson

My pom file:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.contracts</groupId>
  <artifactId>contracts</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>contracts</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>2.0.1.Final</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate.validator</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>6.0.2.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate.validator</groupId>
      <artifactId>hibernate-validator-annotation-processor</artifactId>
      <version>6.0.2.Final</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.11.1</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>com.github.victools</groupId>
        <artifactId>jsonschema-maven-plugin</artifactId>
        <version>4.13.0</version>
        <executions>
          <execution>
            <goals>
               <goal>generate</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <classNames>
            <!-- <className>pojo.SamplePojo</className>-->
          </classNames>
          <packageNames>pojo</packageNames>
          <schemaVersion>DRAFT_7</schemaVersion>
          <schemaFileName>{0}.schema</schemaFileName>
          <schemaFilePath>result/schemas</schemaFilePath>
          <options>
            <enabled>
              <option>DEFINITIONS_FOR_ALL_OBJECTS</option>
              <option>NULLABLE_FIELDS_BY_DEFAULT</option>
            </enabled>
          </options>
          <modules>
            <module>
              <name>JavaxValidation</name>
            </module>
            <!-- <module>-->
            <!-- <name>Jackson</name>-->
            <!-- </module>-->
          </modules>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

My POJO class:

package pojo;

import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
public class SamplePojo {

  @NotNull
  @Pattern(regexp = "^[A-Za-z0-9-_]{1,36}$")
  @JsonPropertyDescription("This is the person's first name")
  public String firstName;

  @NotNull
  @Pattern(regexp = "^[A-Za-z0-9-_]{1,36}$")
  @JsonPropertyDescription("This is the person's last name")
  public String lastName;
}

sabz90 avatar Jul 23 '20 14:07 sabz90

Hi,

I will have a look at it and try to reproduce to propose a fix.

Regards,

Jan Labrie

Op do 23 jul. 2020 16:58 schreef Sabz [email protected]:

Hello, thanks for maintaining this wonderful project I am trying to use the maven plugin to generate schema I want to enable JavaxValidation module. However I get this error while doing a mvn clean install:

[ERROR] Failed to execute goal com.github.victools:jsonschema-maven-plugin:4.13.0:generate (default) on project contracts: Execution default of goal com.github.victools:jsonschema-maven-plugin:4.13.0:generate failed: A required class was missing while executing com.github.victools:jsonschema-maven-plugin:4.13.0:generate: javax/validation/constraints/NotNull [ERROR] ----------------------------------------------------- [ERROR] realm = plugin>com.github.victools:jsonschema-maven-plugin:4.13.0 [ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy [ERROR] urls[0] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-maven-plugin/4.13.0/jsonschema-maven-plugin-4.13.0.jar [ERROR] urls[1] = file:/Users/secretuser/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.10.3/jackson-core-2.10.3.jar [ERROR] urls[2] = file:/Users/secretuser/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.10.3/jackson-databind-2.10.3.jar [ERROR] urls[3] = file:/Users/secretuser/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.10.3/jackson-annotations-2.10.3.jar [ERROR] urls[4] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-generator/4.13.0/jsonschema-generator-4.13.0.jar [ERROR] urls[5] = file:/Users/secretuser/.m2/repository/com/fasterxml/classmate/1.5.1/classmate-1.5.1.jar [ERROR] urls[6] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-module-jackson/4.13.0/jsonschema-module-jackson-4.13.0.jar [ERROR] urls[7] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-module-swagger-1.5/4.13.0/jsonschema-module-swagger-1.5-4.13.0.jar [ERROR] urls[8] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-module-swagger-2/4.13.0/jsonschema-module-swagger-2-4.13.0.jar [ERROR] urls[9] = file:/Users/secretuser/.m2/repository/com/github/victools/jsonschema-module-javax-validation/4.13.0/jsonschema-module-javax-validation-4.13.0.jar [ERROR] urls[10] = file:/Users/secretuser/.m2/repository/org/reflections/reflections/0.9.12/reflections-0.9.12.jar [ERROR] urls[11] = file:/Users/secretuser/.m2/repository/org/javassist/javassist/3.26.0-GA/javassist-3.26.0-GA.jar [ERROR] urls[12] = file:/Users/secretuser/.m2/repository/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar [ERROR] Number of foreign imports: 1 [ERROR] import: Entry[import from realm ClassRealm[maven.api, parent: null]] [ERROR] [ERROR] ----------------------------------------------------- [ERROR] : javax.validation.constraints.NotNull [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR]

The same works fine when I disable JavaxValidation and enable just Jackson

My pom file: ` POJO

4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.1.RELEASE com.contracts contracts 0.0.1-SNAPSHOT contracts Demo project for Spring Boot

1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine javax.validation validation-api 2.0.1.Final org.hibernate.validator hibernate-validator 6.0.2.Final org.hibernate.validator hibernate-validator-annotation-processor 6.0.2.Final com.fasterxml.jackson.core jackson-core 2.11.1 org.springframework.boot spring-boot-maven-plugin com.github.victools jsonschema-maven-plugin 4.13.0 generate pojo DRAFT_7 {0}.schema result/schemas JavaxValidation

`

My POJO class:

package pojo;

import com.fasterxml.jackson.annotation.JsonPropertyDescription; import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor;

@AllArgsConstructor @NoArgsConstructor public class SamplePojo {

@NotNull @Pattern(regexp = "^[A-Za-z0-9-_]{1,36}$") @JsonPropertyDescription("This is the person's first name") public String firstName;

@NotNull @Pattern(regexp = "^[A-Za-z0-9-_]{1,36}$") @JsonPropertyDescription("This is the person's last name") public String lastName; }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/victools/jsonschema-generator/issues/128, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABPRADGV5HGRP5KNL2HHLLDR5BFX5ANCNFSM4PFZ4XLA .

JanLabrie avatar Jul 23 '20 15:07 JanLabrie

The problem is found. It is a missing dependency in the pom of the maven plugin. I am making a branch and a PR now.

JanLabrie avatar Jul 23 '20 21:07 JanLabrie

As per comment on the PR, I'm not convinced of the solution yet. I agree that it might fix the issue described here, but with potential side-effects that I'd prefer to avoid. Since the correct dependency is already mentioned in the above pom.xml, I still don't understand why it doesn't work:

<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>2.0.1.Final</version>
</dependency>

@sabz90 you mentioned that the issue occurs on clean install. Does it also fail on subsequent builds (without clean)?

CarstenEnglert avatar Jul 23 '20 21:07 CarstenEnglert

I am investigating. There are some strange things. But I think there is a workaround to be used for the moment:

Adding the dependency explicitly to the plugin solves the problem in my test project. So your pom would be.

  <plugin>
    <groupId>com.github.victools</groupId>
    <artifactId>jsonschema-maven-plugin</artifactId>
    <version>4.13.0</version>
    <executions>
      <execution>
        <goals>
           <goal>generate</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <classNames>
        <!-- <className>pojo.SamplePojo</className>-->
      </classNames>
      <packageNames>pojo</packageNames>
      <schemaVersion>DRAFT_7</schemaVersion>
      <schemaFileName>{0}.schema</schemaFileName>
      <schemaFilePath>result/schemas</schemaFilePath>
      <options>
        <enabled>
          <option>DEFINITIONS_FOR_ALL_OBJECTS</option>
          <option>NULLABLE_FIELDS_BY_DEFAULT</option>
        </enabled>
      </options>
      <modules>
        <module>
          <name>JavaxValidation</name>
        </module>
        <!-- <module>-->
        <!-- <name>Jackson</name>-->
        <!-- </module>-->
      </modules>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
      </dependencies>
  </plugin>

JanLabrie avatar Jul 24 '20 06:07 JanLabrie

As per comment on the PR, I'm not convinced of the solution yet. I agree that it might fix the issue described here, but with potential side-effects that I'd prefer to avoid. Since the correct dependency is already mentioned in the above pom.xml, I still don't understand why it doesn't work:

<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>2.0.1.Final</version>
</dependency>

@sabz90 you mentioned that the issue occurs on clean install. Does it also fail on subsequent builds (without clean)?

Yes it fails without clean as well (just mvn install)

sabz90 avatar Jul 24 '20 07:07 sabz90

I am investigating. There are some strange things. But I think there is a workaround to be used for the moment:

Adding the dependency explicitly to the plugin solves the problem in my test project. So your pom would be.

  <plugin>
    <groupId>com.github.victools</groupId>
    <artifactId>jsonschema-maven-plugin</artifactId>
    <version>4.13.0</version>
    <executions>
      <execution>
        <goals>
           <goal>generate</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <classNames>
        <!-- <className>pojo.SamplePojo</className>-->
      </classNames>
      <packageNames>pojo</packageNames>
      <schemaVersion>DRAFT_7</schemaVersion>
      <schemaFileName>{0}.schema</schemaFileName>
      <schemaFilePath>result/schemas</schemaFilePath>
      <options>
        <enabled>
          <option>DEFINITIONS_FOR_ALL_OBJECTS</option>
          <option>NULLABLE_FIELDS_BY_DEFAULT</option>
        </enabled>
      </options>
      <modules>
        <module>
          <name>JavaxValidation</name>
        </module>
        <!-- <module>-->
        <!-- <name>Jackson</name>-->
        <!-- </module>-->
      </modules>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
      </dependencies>
  </plugin>

This workaround works. Thank you

sabz90 avatar Jul 24 '20 09:07 sabz90

The release v4.14.0 contains @JanLabrie's fix for this, i.e. the above workaround should no longer be necessary.

CarstenEnglert avatar Aug 02 '20 12:08 CarstenEnglert