February 19th, 2020
0 reactions

Java on Visual Studio Code Update – February 2020

Senior Program Manager

Welcome back to the second update of Java on Visual Studio Code in 2020.

In this update, we will show you the new ways to manage your dependencies and configure your multiple JDK. You will learn the additional tools you can leverage for popular frameworks and runtimes. There’re also a few new code actions and improvements to checkout. On to the update.

Managing Dependencies

Whether your use any build tool or not, Java Dependency Viewer now provides you an easy way to deal with your dependencies.

Maven

If you’re using Maven, now it’s very convenient for you to add dependency for your Maven project through Java dependency explorer by clicking the +  button as below.

Image Kapture 2020 02 19 at 10 33 12

Other projects

If you’re not using any build tool like maven or gradle, and you want to reference binary jars in local file system, the experience is very similar to the maven experience above.

Image manage dependencies

Behind the scene, we’ve introduced a new setting java.project.referencedLibaries. Below are the details about how to customize this setting.

Include libraries

The libraries to reference is described using a set of glob patterns:

"java.project.referencedLibraries": [ 
    "library/**/*.jar", 
    "/home/username/lib/foo.jar" 
]

In this way, all .jar files in workspace’s library folder, and foo.jar in the specified absolute path is added to the project’s external dependencies.

The referenced libraries is then watched by VS Code, and the project will be refreshed once there’s change in these dependent files.

By default, VS Code will reference all jar files in workspace’s lib directory using glob lib/**/*.jar.

Exclude some libraries

If you want to exclude some libraries from the project, just expand java.project.referencedLibraries to full include-exclude-sources pattern and provide globs to exclude field:

"java.project.referencedLibraries": { 
    "include": [ 
        "library/**/*.jar", 
        "/home/username/lib/foo.jar" 
    ], 
    "exclude": [ 
        "library/sources/**" 
    ] 
}

In this way, any binary jar in library/sources folder is ignored from the project’s external dependencies.

Attach source jars

By default, a referenced {binary}.jar will try to search {binary}-sources.jar under the same directory, and attach it as source if one match is found.

If you want to manually specify a jar as source attachment, you can provide a key-value map in the sources field:

"java.project.referencedLibraries": { 
    "include": [ 
        "library/**/*.jar", 
        "/home/username/lib/foo.jar" 
    ], 
    "exclude": [ 
        "library/sources/**" 
    ], 
    "sources": { 
        "library/bar.jar": "library/sources/bar-src.jar" 
    } 
}

In this way, bar-src.jar is attached to bar.jar as its source.

Once external jar files are added, you can also see their file paths as description in Java Dependency Explorer.