artur-shaik / vim-javacomplete2

DEPRECATED in favor of jc.nvim
Vim License
973 stars 83 forks source link

Unable to auto-import maven dependencies #426

Closed kinowarrior closed 3 years ago

kinowarrior commented 5 years ago

Newbie question, most likely relates to classpath setup?

Have a simple SpringBoot application (using maven build):

package com.example.app.ws.ui.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("users")
public class UserController {

  @GetMapping
  public String getUser(
      @RequestParam(value = "page") int page, @RequestParam(value = "limit") int limit) {
    return "get users was called";
  }

  @GetMapping(path = "/{userId}")
  public String getUser(@PathVariable String userId) {
    return "get user was called with userId " + userId;
  }

}

When I run any of my auto-import shortcuts, it fails to understand the "symbol" under the cursor.

nmap <leader>ji <Plug>(JavaComplete-Imports-AddSmart)
nmap <leader>jii <Plug>(JavaComplete-Imports-Add)

Actual behavior (Required!)

I want to auto-import @PathVariable

Receive an error message:

JavaComplete: classname 'PathVariable' not found in any scope

Expected behavior (Required!)

It auto-imports the PathVariable annotation for me.

Effectively, adds:

import org.springframework.web.bind.annotation.PathVariable;

Environment (Required!)

This plugin is working, and highlighting the line that is missing the import with message:

 error: cannot find symbol: PathVariable% [javac]

I am just unclear how I tell the plugin how to find the maven dependencies for this project (app).

As a workaround, I also have the same app open in an IDE, and can use the autofix there to handle any missing imports. Be much easier if I could just leverage this plugin & stay 100% in VIM.

Please can you offer any advice. Thanks in advance.

kinowarrior commented 5 years ago

Hmm, in the help docs it states:

 Set classpath using the following variable:

        g:JavaComplete_LibsPath - path of you jar files. This path will 
        always appended with '~/.m2/repository' directory. Here you can 
        add your glassfish libs directory or your project libs. It will 
        be automatically appended with you jre home path.

Does this mean I should manually add jars (even if they are maven dependencies that live inside '~/.m2/repository') ?

kinowarrior commented 5 years ago

Here is the output of:

:echo g:JavaComplete_LibsPath

.: ~/workspace-spring-tool-suite-4-4.4.0.RELEASE/mobile-app-ws/target/classes: ~/workspace-spring-tool-suite-4-4.4.0.RELEASE/mobile-app-ws/target/classes: ~/workspace-spring-tool-suite-4-4.4.0.RELEASE/mobile-app-ws/target/test-classes: ~/workspace-spring-tool-suite-4-4.4.0.RELEASE/mobile-app-ws/target/test-classes: ~/workspace-spring-tool-suite-4-4.4.0.RELEASE/mobile-app-ws/target/classes

So it is not picking up any maven dependencies by default. Is that expected behaviour?

kinowarrior commented 5 years ago

Have explicitly set:

        let g:JavaComplete_MavenRepositoryDisable = 0
kinowarrior commented 5 years ago

Bump. Hi, any thoughts?

It seems that no dependencies inside '~/.m2/repository' are picked up. How would I (succinctly) register all my project maven dependencies on classpath?

artur-shaik commented 5 years ago

Hi. What you have in your pom.xml?

arvyy commented 5 years ago

Not same person, but I have same problem. My pom looks like this:

<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>mygroupid</groupId>
  <artifactId>myartifactid</artifactId>
  <version>1.0.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

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

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

I'm using windows 10

artur-shaik commented 5 years ago

Would be nice to have little project that I can run to reproduce this problem. Can you create it?

arvyy commented 4 years ago

Sure. https://github.com/arvyy/javacomplete2_pom_example

None of spring classes are suggested when using f5

artur-shaik commented 4 years ago

I cannot reproduce issues.

Can you show logs of server startup?

You need to run:

:JCdebugEnableLogs
:JCserverTerminate
:JCserverStart
:JCdebugGetLogContent
arvyy commented 4 years ago
 33.274403 [util] running command: 'java -cp C:\Users\Arvydas\vimfiles\bundle\vim-javacomplete2\libs\javavi\target\classes;C:\Users\Arvydas\vimfiles\bundle\vim-javacomplete2\libs\javaparser-core-3.5.20.jar;C:\Users\Arvydas\vimfiles\bundle\vim-javacomplete2\libs\javavi_log4j-api.jar;C:\Users\Arvydas\vimfiles\bundle\vim-javacomplete2\libs\javavi_log4j-core.jar; kg.ash.javavi.Javavi -version'
 33.277606 [server] executing python file: C:\Users\Arvydas\vimfiles\bundle\vim-javacomplete2\autoload\javavibridge.py
 33.300515 [server] server classpath: -cp C:\\Users\\Arvydas\\vimfiles\\bundle\\vim-javacomplete2\\libs\\javavi\\target\\classes;C:\\Users\\Arvydas\\vimfiles\\bundle\\vim-javacomplete2\\libs\\javaparser-core-3.5.20.jar;C:\\Users\\Arvydas\\vimfiles\\bundle\\vim-javacomplete2\\libs\\javavi_log4j-api.jar;C:\\Users\\Arvydas\\vimfiles\\bundle\\vim-javacomplete2\\libs\\javavi_log4j-core.jar;.;C:/Users/Arvydas/workspace/javacomplete-test/src/main/java/test/target/classes;C:/Users/Arvydas/workspace/javacomplete-test/src/main/java/test/target/classes;C:/Users/Arvydas/workspace/javacomplete-test/src/main/java/test/target/test-classes;C:/Users/Arvydas/workspace/javacomplete-test/src/main/java/test/target/test-classes;C:/Users/Arvydas/workspace/javacomplete-test/src/main/java/test/target/classes;C:\\Program Files\\Java\\jdk1.8.0_144\\lib
 33.300547 [server] server arguments:-Dlog.level=debug -Dlog.directory=C:\Users\Arvydas\vim_javacomplete2_logs -Ddaemon.port=57993 -Dlog4j.configurationFile=C:\Users\Arvydas\vimfiles\bundle\vim-javacomplete2\libs\javavi\target\classes\log4j2.xml kg.ash.javavi.Javavi -sources  -base C:\Users\Arvydas\.cache\javacomplete2\ -compiler javac
artur-shaik commented 4 years ago

Can you run this command from project directory:

mvn -B --file pom.xml help:effective-pom dependency:build-classpath -DincludeScope=test
arvyy commented 4 years ago

hmm are you interested in the "effective pom" part, because it's ~5000 lines, or only the classpath output

artur-shaik commented 4 years ago

So you have error on this command?

arvyy commented 4 years ago

No, it's just that the resulting pom is super long. To avoid further confusion I'll just paste it then: https://pastebin.com/F9Ty8LSi

artur-shaik commented 4 years ago

Can you try to remove effective pom part in this place: /autoload/javacomplete/classpath/maven.vim:30?

And try again.

artur-shaik commented 4 years ago

Or just try last version, I just pulled this change in master.

arvyy commented 4 years ago

hmm it didn't work. It feels abit unproductive if you can't reproduce. When I have more time I'll try to learn abit about the vimscript and try to debug it myself

artur-shaik commented 4 years ago

If you steel don't see spring libs in output of this command :echo g:JavaComplete_LibsPath this mean, you should debug this file /javacomplete/classpath/maven.vim to find out why in your enviroment this doesn't work.

I will be glad to your participation in this issue.

zw8677174 commented 4 years ago

I got same problem. JavaComplete-Imports-Add not work. same response: classname 'OkHttpClient' not found in any scope. it may the problem. i used neovim same. (NVIM v0.4.3)

something else, i notice that : without JavaComplete-Imports-Add, when i finished that third class. it shows this : class : Class this time, i click the space. it import this class auto. i don,t know why. but i get this class has find by plug. It just not work in JavaComplete-Imports-Add.

last, once more. neovim too. may the problem. I am new one. it may be wrong. bad english. sry

geofflangenderfer commented 4 years ago

Try using gradle.

I tested the same project with both. Gradle works. Maven doesn't.

hj-hejie commented 3 years ago

pull request