Compile error for hibernate-validator country specific constraints

55 views Asked by At

My project is a JEE with jax-rs/resteasy implementation and I'm using resteasy validation with beans validations(hibernate-validator). When I try to build project by maven the error is:

employee.java:[6,10] package org.hibernate.validator.constraints.br does not exist
cannot find symbol class CPF

package br.com.example.dto;

import org.hibernate.validator.constraints.br.CPF;

public class Employee {

    @NotBlank(message = "blank")
    @CPF(message = "invalid")
    private String cpf;
}

Pom.xml:

<dependency>
    <groupId>org.wildfly.bom</groupId>
    <artifactId>wildfly-jakartaee8-with-tools</artifactId>
    <version>${wildfly.version}</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

If I remove @CPF validation everything works fine.

My question: why <artifactId>hibernate-validator</artifactId> that was declared inside of wildfly-jakartaee8-with-tools was not working for this country specific constraint?

1

There are 1 answers

4
Maksim Eliseev On

Importing a BOM does nothing (in terms of setting dependencies). You will still have to declare dependencies required for your project.

But BOM is pretty good for dependency versioning. In Maven you should set that BOM file as the parent of your pom.xml:

<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>

    <parent>
        <groupId>org.wildfly.bom</groupId>
        <artifactId>wildfly-jakartaee8-with-tools</artifactId>
        <version>26.1.3.Final</version>
        <type>pom</type>
    </parent>

    <dependencies>
        <!-- required dependencies -->
    </dependencies>

    <!-- ... -->

</project>

Or as a dependency in the <dependencyManagement> section:

<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>

    <dependencies>
        <!-- required dependencies -->
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.wildfly.bom</groupId>
                <artifactId>wildfly-jakartaee8-with-tools</artifactId>
                <version>26.1.3.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- ... -->

</project>

And after that you don't need to specify any version of the dependency declared in the <dependencyManagement> section of the parent pom file. For example:

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
</dependency>

I also looked at the wildfly-jakartaee8-with-tools and didn't find hibernate-validator. You should check this out.