Gangmax Blog

自由之思想,独立之精神

Running Spring Boot Gets MyBatis Not Found Wrong in IntelliJ IDEA

| Comments

I create a Spring Boot project under IntellliJ IDEA by using Groovy. The problem is that, running a unit test class in IDEA it reports error: the Groovy classes which use MyBatis classes cannot be compiled since the “[org.mybatis/mybatis “3.4.1”]” dependency cannot be found. However, if I use “gradle build” command to run the test directly, everything works.

  1. Use the “gradle idea” command to generate the IDEA project files(you may need to clean the existing files if there is). In my case, I just keep the “.idea/” directory after running the command and remove the 3 files(<project>.iml, <project>.ipr, <project>.iws) under the project directory.

  2. In IDEA open the project, go to “File -> Project Structure -> Modules” to configure the “source/test/resources” directories if IDEA cannot find them correctly. In my case, select “src/main/groovy/” as the source directory, “src/main/java/” as the test directory, “src/main/resources/” as the resources directory. This action actually updates the “<content>” content in the your “.idea/modules/<project>.iml” file.

  3. Now if you find some classes in your project cannot be compiled, go to “File -> Project Structure -> Problems”. If you find there is a list which shows “xxx jar is not used…”, click the “[fix]” link at the end of each item in the list, by doing so the dependency will be added in the “.idea/modules/<project>.iml” file. Then you classes should be able to be compiled.

  4. If you find running your unit test class failed and IDEA tells you that “output path is not set…”, go to “File -> Project Structure -> Modules -> Paths”, set the “Output path” as “<project>/build/classes/main”, and “Test output path” as “<project>/build/classes/main”.

Then it works.

My environment: “IntelliJ IDEA 2016.3.1”, “Gradle 3.2.1”.

I found this today, and it may be a faster solution.

Comments