I tend to try use Visual Studio Code for tasks and languages I don't currently use on a day to day basis. Over the last few weeks that has included Java and Delphi. Then today I was trying to launch my blog from VS Code and ran into an issue because Pretzel listens for a console key. The only fix I could find for this was to launch a new Powershell window. I thought this as good a time as any to post a few of these tasks.
Tasks in VS Code allow you to run commands that execute and usually feedback some status. Tasks are configured in the file /.vscode/tasks.json from the workspace root. Hit Ctrl+Shift+P and type Tasks:C and hit enter or click 'Tasks: Configure Task Runner'. If the file does not exist it will be created.
- What you see may not exactly match Figure 1 as it opens up in the same state it was.
- Creating a Java Project Maven Visual Studio Code for Java Creating Your Project with Maven Java extension can use information contained on Maven build artifacts to extract information such as the location of source files, dependencies and compiler preferences.
- Install the “Java Extension Pack” This installs the most essential extensions for Java support.
- Lightweight Mode, Maven Support, Java Package, and Dependency Management in Visual Studio Code Java project management in VS Code This document will give you an overview of how to manage your Java project in Visual Studio Code.
Compiling a Java application
Apache Maven and Visual Studio Code are primarily classified as 'Java Build'and 'Text Editor'tools respectively.
This command uses javac
to compile the Java application and will report on compile errors. Note that this uses a single task (others in the post have multiple tasks in the file). It assumes javac
is on your PATH. I also have the Language Support for Java extension from Red Hat installed in VS Code.
Control Maven for a Java project
These control different Maven phases. Note that on the exec
task you need to change the me.devonburriss.App
to the entrypoint of your application. It assumes mvn
is on your PATH. Not needed for this but note that I have the Language Support for Java extension from Red Hat installed.
Delphi (Free Pascal) Build
This is using the Free Pascal compiler to compile Delphi code. It assumes that fpc
is on your PATH. You can get it here.
This only compiles a single unit, not a complete project. Not needed for this to work but for syntax highlighting I have the OmniPascal extension installed.
Powershell, Cake, Pretzel blog Build
This is one I use to call PS, which executes my Cake build and and run this blog locally. The targets for that are Bake and Taste (from Pretzel). See this post for details on that.
I use a run.ps1 file because I needed to launch a new Powershell window so Pretzel can wait and watch for changes.
Just a note that I have the Powershell extension from Microsoft for VS Code installed. Not needed for the task to run but it gives nice support for ps1 files.
Extra: F5 Launch of Pretzel Blog
If you want to use F5 to run the blog you can press Ctrl+Shift+P and type launch. If it doesn't exist a launch.json file will be created.
Where my run.ps1 looks like this:
Visual Studio Code is a great editor and has plenty of extension points. If you have any great tips I would love to hear about them in the comments.
You may be interested in
Categories
AgileDevOpsLeadershipLifeModelingProject ManagementSoftware DevelopmentToolsVirtualizationTags
usingcode.visualstudio.com
Prerequisites
Maven, Java 1.8 or newer, These VS Code extensions:
Java Extension Pack- Language support for Java™ for Visual Studio Code
Java Linting, Intellisense, formatting, refactoring, Maven/Gradle support and more… - Maven for Java
Manage maven projects, execute goals, generate project from archetype, improve user experience for Java developers.
Create Maven Project
In the Command Palette, go directly to the commandsCtrl+Shift+P
Select the
Or in the explorer sidebar, expand Maven Projects, Select the + sign to create a Maven Project.
Selectmaven-archetype-webapp
org.apache.maven.archetypes
Answer the prompts to setup the project.
The artifactId
will be used for the maven project folder name. for example,
Property | Value |
---|---|
groupId | com.example |
artifactId | webapp |
version | 0.0.1-SNAPSHOT |
package | com.example.webapp |
Typically, the groupId
and artifactId
combine to make the base package.
Update POM
Update the pom.xml
for the version of Java being used. e.g.,
pom.xml
Docker Tomcat Container
Setup a Tomcat Docker container and develop the Maven Project within it.
Assumptions
System has Docker installed. For Linux, it’s setup to run as a non-root user.
Required VS Code Extensions
- Remote - Containers VS Code Extension
- Remote - SSH VS Code Extension
In the Command Palette, go directly to the commandsCtrl+Shift+P OR F1
Select the folder that contains the Maven project, for example webapp
.
In the Add Development Container Configuration Files dropdown, enter tomcat
to filter the list. Then select a Java Tomcat server, for example, Java 8 & Tomcat 8.5 Server.
Visual Studio Code Maven Build Jar
The remote VS Code instance will open, the Docker image will be created along with the dev container. Expand .devcontainer
in the Explorer panel and inspect devcontainer.json
and the Dockerfile
for more info.
To see the dev container that was created, in a terminal, enter docker ps -a
.
After the container is built, VS Code automatically connects to it and maps the webapp
Maven Project from the local file system into the container.
Visual Studio Code Maven Java
Add Tomcat Server
Visual Studio Code Maven Plugin
In the Command Palette, go directly to the commandsCtrl+Shift+P
Or in the explorer sidebar, expand Tomcat Servers, Select the + sign to add a Tomcat Server.
Enter the path for the server. e.g.,
Generate war
Given the current terminal session is Dev Containers
, open a new terminal session, Ctrl+Shift+`
The new terminal session should have you in the /workspaces/webapp
directory as root within the dev container. e.g.,
Run this mvn
command to generate the war file.
Run on server
In the explorer sidebar, right click on the war file and select Run on Tomcat Server. e.g., target/webapp.war
In the explorer sidebar, expand Tomcat Servers, tomcat and right click on the webapp to open in a browser.
The browser should open to localhost:8080 with “Hello World” rendered from the index.jsp
.
Additional Resources
Related