ERROR Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
Asked Answered
R

1

5

I'm writing Backend using Quarkus. Below is my POM.xml file:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.abcd.mpt</groupId>
    <artifactId>hc-web</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <properties>
        <compiler-plugin.version>3.11.0</compiler-plugin.version>
        <maven.compiler.release>17</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
        <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
        <quarkus.platform.version>3.1.1.Final</quarkus.platform.version>
        <skipITs>true</skipITs>
        <surefire-plugin.version>3.0.0</surefire-plugin.version>
    </properties>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>${quarkus.platform.group-id}</groupId>
                <artifactId>${quarkus.platform.artifact-id}</artifactId>
                <version>${quarkus.platform.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-arc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-resteasy-reactive</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-jdbc-postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-hibernate-orm-panache</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-agroal</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-flyway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version> <!-- Please check for the latest version -->
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.5.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.5.3.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.smallrye</groupId>
            <artifactId>smallrye-jwt-build</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-rest-client</artifactId>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>${quarkus.platform.group-id}</groupId>
                <artifactId>quarkus-maven-plugin</artifactId>
                <version>${quarkus.platform.version}</version>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <goals>
                            <goal>build</goal>
                            <goal>generate-code</goal>
                            <goal>generate-code-tests</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${compiler-plugin.version}</version>
                <configuration>
                    <compilerArgs>
                        <arg>-parameters</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${surefire-plugin.version}</version>
                <configuration>
                    <systemPropertyVariables>
                        <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                        <maven.home>${maven.home}</maven.home>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${surefire-plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <systemPropertyVariables>
                                <native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
                                <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                                <maven.home>${maven.home}</maven.home>
                            </systemPropertyVariables>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>native</id>
            <activation>
                <property>
                    <name>native</name>
                </property>
            </activation>
            <properties>
                <skipITs>false</skipITs>
                <quarkus.package.type>native</quarkus.package.type>
            </properties>
        </profile>
    </profiles>
</project>


and below is the code where I use @ApplicationScoped,

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.NotFoundException;
import java.util.List;

@ApplicationScoped
@Transactional
public class ReviewServiceImpl implements ReviewService {



    @Override
    @Transactional
    public ReviewDto createReview(ReviewDto reviewDTO) {
        Review review;
        try {
            review = Review.find("id", reviewDTO.getId()).firstResult();
        } catch (NullPointerException e) {
            throw new NullPointerException("Review already exists");
        }
        review = ReviewMapper.INSTANCE.toEntity(reviewDTO);
        review.persist();
        return ReviewMapper.INSTANCE.toDto(review);
    }

    @Override
    @Transactional
    public ReviewDto updateReview(Long id, ReviewDto reviewDTO) {
        Review review;
        try{
            review = Review.find("id",id).firstResult();
        }catch(NullPointerException e){
            throw new NotFoundException("Review not found for id: " + id);
        }

        ReviewMapper.INSTANCE.updateFromDto(reviewDTO, review);
        review.persist();

        return ReviewMapper.INSTANCE.toDto(review);
    }

    @Override
    public ReviewDto getReviewById(Long reviewId) {
        Review review = Review.findById(reviewId);
        if(review == null){
        throw new NotFoundException("Review not found for id: " + reviewId);
        }
        return ReviewMapper.INSTANCE.toDto(review);

    }

    @Override
    public List<ReviewDto> getReviewsByDoctorId(Long doctorId) {
        List<Review> reviews = Review.find("select r from Review r join r.reviewDoctorMaps d where d.doctor_id =?1",doctorId).list();
        return ReviewMapper.INSTANCE.toDtoList(reviews);
    }

    @Override
    public List<ReviewDto> getAllReviews() {
        List<Review> reviews = Review.listAll();
        return ReviewMapper.INSTANCE.toDtoList(reviews);
    }
}


I'm already using jakarta.enterprise.context.ApplicationScoped then why it is asking me to use javax packages ?

there is no javax.* dependency used in the code, nor in any super class.

this is log when I run the application

/Library/Java/JavaVirtualMachines/graalvm-17/Contents/Home/bin/java -Dmaven.multiModuleProjectDirectory=/Users/maulikdave/NetBeansProjects/hc/hc-web -Dmaven.home=/opt/homebrew/Cellar/maven/3.9.2/libexec -Dclassworlds.conf=/opt/homebrew/Cellar/maven/3.9.2/libexec/bin/m2.conf "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=58339:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /opt/homebrew/Cellar/maven/3.9.2/libexec/boot/plexus-classworlds.license:/opt/homebrew/Cellar/maven/3.9.2/libexec/boot/plexus-classworlds-2.7.0.jar org.codehaus.classworlds.Launcher -Didea.version=2022.2.5 clean compile quarkus:dev
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.abcd.mpt:hc-web >----------------
[INFO] Building hc-web 1.0.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- clean:3.2.0:clean (default-clean) @ hc-web ---
[INFO] Deleting /Users/maulikdave/NetBeansProjects/hc/hc-web/target
[INFO] 
[INFO] --- resources:3.3.0:resources (default-resources) @ hc-web ---
[INFO] Copying 42 resources
[INFO] 
[INFO] --- quarkus:3.1.1.Final:generate-code (default) @ hc-web ---
[INFO] 
[INFO] --- compiler:3.11.0:compile (default-compile) @ hc-web ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 79 source files with javac [debug release 17] to target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[INFO] 10 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.029 s
[INFO] Finished at: 2023-06-14T17:02:46+05:30
[INFO] ------------------------------------------------------------------------
[WARNING] 
[WARNING] Plugin validation issues were detected in 1 plugin(s)
[WARNING] 
[WARNING]  * org.apache.maven.plugins:maven-resources-plugin:3.3.0
[WARNING] 
[WARNING] For more or less details, use 'maven.plugin.validation' property with one of the values (case insensitive): [BRIEF, DEFAULT, VERBOSE]
[WARNING] 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project hc-web: Compilation failure: Compilation failure: 
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[ERROR] Couldn't find type javax.enterprise.context.ApplicationScoped. Are you missing a dependency on your classpath?
[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] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Radioactive answered 14/6, 2023 at 10:36 Comment(4)
Adding the javax dependency is a bad idea, because javax and jakarta classes should not be used together. Where is the error you're seeing coming from? Is it in the IDE, or do you have a stack trace? Given that you're importing jakarta.enterprise.context.ApplicationScoped, ReviewServiceImpl should not be using any javax dependencies, unless one is coming in from a superclass?Halophyte
thanks for the reply @HollyCummins, I have updated the question by providing the log while running the application.Radioactive
run maven with -X for verbose outputHeinous
I'd be tempted to run a mvn clean and maybe even clean out a few things from ~/.m2/repository, since the error you're seeing doesn't seem to align with what's in your code.Halophyte
G
17

MapStruct using custom annotation processor during build time for generating mapping sources. That supports several different ways to generate classes.

However, @Mapper(componentModel = "cdi") using javax.enterprise.context.ApplicationScoped, @Mapper(componentModel = "jakarta") configuration imports the jakarta.enterprise.context.ApplicationScoped annotation.

So just change componentModel attribute to:

@Mapper(componentModel = "jakarta")

Note:

  • This componentModel value is supported since MapStruct 1.5.0.Final and above.
  • The updated migration guide also describes this problem and this soulution
Grenadier answered 16/6, 2023 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.