Visual Studio Code for Java Maven Project

Not sure what I did with the initial text for this post. I opened my work space today and could not find it. On the bright side I will redo it going over what I did with Visual Studio Code to install Maven support.

In my previous post Designing a System like Yelp I mentioned that one of the key features in the design of the system would be to come up with a mechanism to find Points Of Interest (POI) to the user based on a specified location which might be their current position. A similar use case would be to find hotels close to an airport when planning a trip. In these situations we have a location and wish to find POS within a distance. If a user in New York wishes to have breakfast, chances are that restaurants in Irvine, California would not be of much interest even if they are topped ranked.

The idea was to use sharding for the database and a Geohash mechanism. One way to implement Geohashing is to use R-Trees. I found some implementations which I will use to experiment with. At that point I might go for and implement the code for the algorithm or just spend time understanding the algorithm and how to use it. I have worked with Neo4j and the database engine allows geo location operations.

The R-Tree Java code that I am planning to use is rtree2 and was written by David Moten. After downloading and building the code I noticed that it uses Maven. I have used Maven many times with the Eclipse IDE. I have never used it with Visual Studio Code. I decided to go over the steps that will allow a developer to use VSCode with Maven.

We first need to install or make sure we have a couple extensions installed in VSCode.

Step Description
1 Open VSCode
2 Click on Manage (cogwheel on left bottom pane) and select Extensions
3 In the Extensions text box enter:  Maven
4 Look for “Maven for Java” and install the extension
5 Look for “Java Extension Pack” and install the extension

Now we are ready to create a Maven project. I like to keep all my projects in different work spaces. At this time I am using the folder:  C:\Users\John\workspace7 and will like to create a Maven test project which I will call MavenTest.

Step Description
1 Open VSCode
2 Select File -> Open Folder…
3 Navigate to the desired folder. In this case:  C:\Users\John\workspace7
4 On the bottom right side of the pop-up window click <Select Folder> button
5 In the Explorer pane hover on MAVEN PROJECTS and click on the + sign
6 On the Select an archetype … pop-up field select archetype-quickstart-jdk8
7 In the Select a version … pop-up field select 1.2.0
8 In the Select Folder select the folder that will hold your Maven project.

In our case I select workspace7 and click on the <Select Destination Folder> button

In the Terminal window you will see some text scroll while VSCode starts creating the Maven project. At some point you will be prompted:

Define value for ‘property ‘groupId’:  com.canessa.maven.test

I entered com.canessa.maven.test as the value. You should enter something appropriate for you.

VSCode then prompts as follows:

Define value for property ‘artifactId’:  HelloMaven

I entered HelloMaven as the value. This will be the Java project name.

VSCode will prompt as follows:

Define value for property ‘version’:  1.0-SNAPSHOT:

I accepted the default value.

VSCode then prompts for:

Define value for property ‘package’:  com.canessa.maven.test :

In your case it will prompt with the value you specified for the ‘groupId’. I accepted the default value.

At this point VSCode asks you to verify the values so I just clicked on the <Enter> key on my keyboard.

A few more messages appear and our Maven project is created. The screen capture of my TERMINAL follows:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.    

Try the new cross-platform PowerShell https://aka.ms/pscore6 

Loading personal and system profiles took 1395ms.
(base) PS C:\Users\John\workspace7> cd "c:\Users\John\workspace7"
(base) PS C:\Users\John\workspace7> & mvn archetype:generate 
-DarchetypeArtifactId="archetype-quickstart-jdk8" -DarchetypeGroupId="com.github.ngeor" -DarchetypeVersion="1.2.0"        
[INFO] Scanning for projects...
[INFO]

[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) 
> generate-sources @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) 
< generate-sources @ standalone-pom <<<
[INFO] 
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) 
@ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] Error reading archetype catalog http://repo.maven.apache.org/maven2
org.apache.maven.wagon.TransferFailedException: Failed to transfer file: http://repo.maven.apache.org/maven2. Return code 
is: 501 , ReasonPhrase:HTTPS Required.
        at org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.fillInputData(AbstractHttpClientWagon.java:1021) 
        at org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.fillInputData(AbstractHttpClientWagon.java:960)  
        at org.apache.maven.wagon.StreamWagon.getInputStream(StreamWagon.java:116)
        at org.apache.maven.wagon.StreamWagon.getIfNewer(StreamWagon.java:88)
        at org.apache.maven.wagon.StreamWagon.get(StreamWagon.java:61)
        at org.apache.maven.archetype.source.RemoteCatalogArchetypeDataSource.downloadCatalog(RemoteCatalogArchetypeDataSource.java:119)
        at org.apache.maven.archetype.source.RemoteCatalogArchetypeDataSource.getArchetypeCatalog(RemoteCatalogArchetypeDataSource.java:87)
        at org.apache.maven.archetype.DefaultArchetypeManager.getRemoteCatalog(DefaultArchetypeManager.java:216)
        at org.apache.maven.archetype.DefaultArchetypeManager.getRemoteCatalog(DefaultArchetypeManager.java:205)
        at org.apache.maven.archetype.ui.generation.DefaultArchetypeSelector.getArchetypesByCatalog(DefaultArchetypeSelector.java:200)
        at org.apache.maven.archetype.ui.generation.DefaultArchetypeSelector.selectArchetype(DefaultArchetypeSelector.java:71)
        at org.apache.maven.archetype.mojos.CreateProjectFromArchetypeMojo.execute(CreateProjectFromArchetypeMojo.java:181)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)        
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)        
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)       
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[WARNING] Archetype not found in any catalog. Falling back to central repository (http://repo.maven.apache.org/maven2).
[WARNING] Use -DarchetypeRepository=<your repository> if archetype's repository is elsewhere.
Define value for property 'groupId': : com.canessa.maven.test
Define value for property 'artifactId': : HelloMaven
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  com.canessa.maven.test: :
Confirm properties configuration:
groupId: com.canessa.maven.test
artifactId: HelloMaven
version: 1.0-SNAPSHOT
package: com.canessa.maven.test
 Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: archetype-quickstart-jdk8:1.2.0 
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.canessa.maven.test
[INFO] Parameter: artifactId, Value: HelloMaven
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.canessa.maven.test
[INFO] Parameter: packageInPathFormat, Value: com/canessa/maven/test
[INFO] Parameter: package, Value: com.canessa.maven.test
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.canessa.maven.test
[INFO] Parameter: artifactId, Value: HelloMaven
[INFO] project created from Archetype in dir: C:\Users\John\workspace7\HelloMaven
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11:23 min
[INFO] Finished at: 2020-01-31T08:51:28-06:00
[INFO] Final Memory: 14M/280M
[INFO] ------------------------------------------------------------------------
(base) PS C:\Users\John\workspace7>

You should see your new Maven project in the MAVEN PROJECTS menu in the EXPLORER pane of VSCode. In our case we should see HelloMaven.

C:\>cd users

C:\Users>cd John

C:\Users\John>cd Workspace7

C:\Users\John\workspace7>cd HelloMaven

C:\Users\John\workspace7\HelloMaven>dir
 Volume in drive C is OS
 Volume Serial Number is 26E8-87B0

 Directory of C:\Users\John\workspace7\HelloMaven

01/31/2020  08:51 AM    <DIR>          .
01/31/2020  08:51 AM    <DIR>          ..
01/31/2020  08:51 AM               314 .editorconfig
01/31/2020  08:51 AM                88 .gitattributes
01/31/2020  08:51 AM               351 .gitignore
01/31/2020  08:51 AM                73 .travis.yml
01/31/2020  08:51 AM             6,189 pom.xml
01/31/2020  08:51 AM    <DIR>          src
               5 File(s)          7,015 bytes
               3 Dir(s)  536,563,499,008 bytes free

C:\Users\John\workspace7\HelloMaven>cd source
The system cannot find the path specified.

C:\Users\John\workspace7\HelloMaven>dir
 Volume in drive C is OS
 Volume Serial Number is 26E8-87B0

 Directory of C:\Users\John\workspace7\HelloMaven

01/31/2020  08:51 AM    <DIR>          .
01/31/2020  08:51 AM    <DIR>          ..
01/31/2020  08:51 AM               314 .editorconfig
01/31/2020  08:51 AM                88 .gitattributes
01/31/2020  08:51 AM               351 .gitignore
01/31/2020  08:51 AM                73 .travis.yml
01/31/2020  08:51 AM             6,189 pom.xml
01/31/2020  08:51 AM    <DIR>          src
               5 File(s)          7,015 bytes
               3 Dir(s)  536,563,499,008 bytes free

C:\Users\John\workspace7\HelloMaven>cd src

C:\Users\John\workspace7\HelloMaven\src>cd main

C:\Users\John\workspace7\HelloMaven\src\main>cd java

C:\Users\John\workspace7\HelloMaven\src\main\java>cd com

C:\Users\John\workspace7\HelloMaven\src\main\java\com>cd canessa

C:\Users\John\workspace7\HelloMaven\src\main\java\com\canessa>cd maven

C:\Users\John\workspace7\HelloMaven\src\main\java\com\canessa\maven>cd test

C:\Users\John\workspace7\HelloMaven\src\main\java\com\canessa\maven\test>dir
 Volume in drive C is OS
 Volume Serial Number is 26E8-87B0

 Directory of C:\Users\John\workspace7\HelloMaven\src\main\java\com\canessa\maven\test

01/31/2020  08:51 AM    <DIR>          .
01/31/2020  08:51 AM    <DIR>          ..
01/31/2020  08:51 AM               320 App.java
               1 File(s)            320 bytes
               2 Dir(s)  536,565,985,280 bytes free

C:\Users\John\workspace7\HelloMaven\src\main\java\com\canessa\maven\test>

Using a command prompt we navigate to the source code for our newly created Maven project. It seems that all is well.

Let’s open, make a change and run our new Maven  project. We should open our Maven project like any other project in VSCode.

Step Description
1 Open VSCode
2 Select File -> Open Folder …
3 In the Open Folder pop-up navigate to:  C:\Users\John\workspace7\HelloMaven
4 In the Folder: prompt at the bottom of the pop-up window it should read:  HelloMaven
5 Click on the <Select Folder> button in the lower right side of the pop-up window
6 In the EXPLORER pane expand the HELLOMAVEN project
7 Select HELLOMAVEN -> src -> App.java
8 The App.java file should open.

 

package com.canessa.maven.test;

/**
 * Hello world!
 */
public final class App {
    private App() {
    }

    /**
     * Says hello to the world.
     * 
     * @param args The arguments of the program.
     */
    public static void main(String[] args) {
        System.out.println("main <<< Hello World !!!");
    }
}

I made a change to the message displayed by the app. We can run the app and check out the results.

Step Description
1 Select Debug -> Run Without Debugging
2 The TERMINAL windows displays
3 I like to move the TERMINAL to the right side of my work space. I do this by clicking on TERMINAL -> Move Panel Right

The updated message “main <<< Hello World !!!” was displayed. So far all is well.

I then decided to start experimenting with the RTree class. Added a call to different constructors and each time the code crashed throwing an exception. After some searching I determined that I was missing a second jar file. I had to clone, build and add the additional jar.

The code for the additional jar is guava-mini.

Cloned the Git repository and built the JAR file. That seemed to work well.

I was not able to import a JAR file to a project using VSCode. This also occurred with the first JAR. I closed VSCode and opened the same project with the Eclipse IDE. I then imported the JAR files. All went well.

<?xml version="1.0" encoding="UTF-8" ?>
<classpath>
	<classpathentry kind="src" output="target/classes" path="src/main/java">
		<attributes>
			<attribute name="optional" value="true" />
			<attribute name="maven.pomderived" value="true" />
		</attributes>
	</classpathentry>
	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
		<attributes>
			<attribute name="optional" value="true" />
			<attribute name="maven.pomderived" value="true" />
			<attribute name="test" value="true" />
		</attributes>
	</classpathentry>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
		<attributes>
			<attribute name="maven.pomderived" value="true" />
		</attributes>
	</classpathentry>
	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
		<attributes>
			<attribute name="maven.pomderived" value="true" />
		</attributes>
	</classpathentry>
	<classpathentry kind="src" path="target/generated-sources/annotations">
		<attributes>
			<attribute name="ignore_optional_problems" value="true" />
			<attribute name="optional" value="true" />
			<attribute name="maven.pomderived" value="true" />
			<attribute name="m2e-apt" value="true" />
		</attributes>
	</classpathentry>
    
	<classpathentry kind="lib" path="C:/Users/John/workspace7/rtree2/target/rtree2-0.9-RC2-SNAPSHOT.jar" />
    
	<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
		<attributes>
			<attribute name="ignore_optional_problems" value="true" />
			<attribute name="optional" value="true" />
			<attribute name="maven.pomderived" value="true" />
			<attribute name="m2e-apt" value="true" />
			<attribute name="test" value="true" />
		</attributes>
	</classpathentry>
    
	<classpathentry kind="lib" path="C:/Users/John/workspace7/guava-mini/target/guava-mini-0.1.3-SNAPSHOT.jar" />
    
	<classpathentry kind="output" path="target/classes" />
</classpath>

The lines with reference to the JARS rtree2-0.9-RC2-SNAPSHOT.jar and guava-mini-0.1.3-SNAPSHOT.jar had to be added to the project.

At that point I executed the following test code using the VSCode IDE.

    /**
     * Says hello to the world.
     */
    public static void main(String[] args) {

        // **** ****
        System.out.println("main <<< Hello World !!!");

        // // **** ****
        // RTree<String, Point> tree = RTree.maxChildren(5).create();

        // **** ****
        RTree<String, Geometry> tree = RTree.create();
    }

The output follows:

main <<< Hello World !!!
main <<< bye bye :o)

All seems to work. Now I am ready to attempt to build the RTree code which uses Maven in VSCode.

If you have comments or questions regarding this, or any other post in this blog, or if you would like for me to serve of assistance with any phase in the SDLC (Software Development Life Cycle) of a project associated with a product or service, please do not hesitate and leave me a note below. If you prefer, send me a private message using the following address:  john.canessa@gmail.com. I will reply as soon as possible.

Keep on reading and experimenting. It is the best way to learn, refresh your knowledge and enhance your developer toolset!

John

Twitter:  @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.