0
votes

This project was working fine. I was just playing around with Spring boot versions in pom.xml, imported some new maven projects, then saw that for those new projects, Springboot annotations and imports stopped working -

Cannot resolve @SpringBootApplication

pom.xml -

<?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.in28minutes.database</groupId>
    <artifactId>database-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>database-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.11.RELEASE</version>
        <!--<version>1.5.2.RELEASE</version>-->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


</project>

I have another setup, which is using spring boot 1.5.2.RELEASE. I checked that both the versions are there in my local .m2 repository. No clue on what is the issue.

It seems, all new projects imported are throwing the same issue. E.g. https://github.com/callicoder/spring-boot-actuator-demo

Update

I realised my main project, has some dependencies (maven repositories downloaded from a remote server). These are defined in .m2/settings.xml.

When I ran mvn install on command line, I get this error -

[ERROR] Failed to execute goal on project actuator-demo: Could not resolve dependencies for project com.example:actuator-demo:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at org.springframework.boot:spring-boot-starter-actuator:jar:2.1.11.RELEASE: Failed to read artifact descriptor for org.springframework.boot:spring-boot-starter-actuator:jar:2.1.11.RELEASE: Could not transfer artifact org.springframework.boot:spring-boot-starter-actuator:pom:2.1.11.RELEASE from/to nexus (https://project.repository.com/repository/maven-public/): Failed to transfer file: https://project.repository.com/repository/maven-public/org/springframework/boot/spring-boot-starter-actuator/2.1.11.RELEASE/spring-boot-starter-actuator-2.1.11.RELEASE.pom. Return code is: 502 , ReasonPhrase:Bad Gateway. -> [Help 1]

I took a backup of the settings.xml and removed it. I ran mvn clean fresh, this time it downloaded artefacts from mvn remote repository and problem got resolved.

Not sure what is a proper way to switch settings.xml for my working project and demo projects, as former needs those settings.

2
Try to clear cache.. or in the worst case, remove .m2 folder. Also don't foget to check "online mode" which I found set one day and I wondered for hours why the new dependencies are not resolved. Give it a try :) – Nikolas Charalambidis
use mvn dependency:tree -Dincludes=[groupId]:[artifactId]:[type]:[version] to print your dependencies tree to a fike and see what dependencies and versions you are using. – Binyamin Regev

2 Answers

2
votes

Test from the command line to make sure Maven is setup properly. If maven does not work from the CLI, fix it (perhaps you do need to delete your repository and re-download everything). Be careful if you delete your repo if you have artifacts that don't come from an online repo.

Based on your edits, you need a different settings.xml for Maven. Do this:

mvn --settings ~/.m2/settings-demo.xml clean install

That will use the alternate settings file. For Intellij, you can specify your settings.xml file in the maven settings (check "Override" and put the new path in):

intellij maven settings

It Maven works from the CLI - Intellij gets stupid sometimes, try going to File -> Invalidate Caches / Restart and select "Invalidate and Restart"

intellij caches

0
votes

I don't think it's a Spring Boot issue, sounds more like a Maven issue because Spring boot @SpringBootApplication annotation is working. From your description and looking at your pom.xml, it sounds like you caused some conflicts for Maven.

  1. Try to reimport your dependencies.
  2. If that doesn't help then delete your .m2/repository folder and reimport your dependencies.
  3. I noticed in your pom.xml that you don't have a version for Spring Boot starter, it means Maven takes the one that is closest to the root and if its Spring Boot version is lower than 2.0 then @SpringBootApplication is not supported. Also, try to add a version to spring-boot-starters in your pom.xml