Build tools for java projects

Java build tools in VS Code

This document is an overview of how to work with your Java build tools in Visual Studio Code. It covers the Maven for Java and Gradle for Java extensions as well as other tools.

If you run into any issues when using the features below, you can contact us by entering an issue.

Maven

Maven is a software tool that helps you manage Java projects and automate application builds. The Maven for Java extension for Visual Studio Code provides fully integrated Maven support, allowing you to explore Maven projects, execute Maven commands, and perform the goals of build lifecycle and plugins. We recommend installing the Extension Pack for Java, which includes Maven support and other important Java development features.

Exploring Maven project

Once a Maven project is loaded, the extension will be activated and it will automatically scan for pom.xml files in your workspace and displays all Maven projects and their modules in the side bar.

Maven Explorer

Resolve unknown type

The Maven extension also supports searching Maven Central to resolve unknown types in your source code. You can do this by selecting the Resolve unknown type link shown on hover.

Читайте также:  Ubuntu ca certificates java

Working with POM.xml

The extension provides code snippets and auto completion for adding Maven dependencies based on local Maven repositories. See how easy it is to add a new dependency to your pom.xml with those convenient features.

The extension also enables you to generate effective POM.

You can also use the command Maven: Add a Dependency (or maven.project.addDependency ) to help add a new dependency to pom.xml . The process is interactive.

You can also add dependencies through the project view, which calls the same Maven command.

Furthermore, VS Code also supports showing dependencies in a tree view, which allows you to inspect all dependencies in your project at a single place and check for potential issues.

Execute Maven commands and goals

By right-clicking each Maven project in the Explorer, you can conveniently run Maven goals.

The extension also preserves the history of goals for each project, so you can quickly rerun the previous command, which is useful when you’re running a long custom goal.

There are two ways to rerun a goal:

  1. In the Command Palette, run Maven: History, then select a project and a command from its history.
  2. Right-click a project and select History. You can then select a previous command from history.

You can also specify your favorite commands in settings for future execution.

For each plug-in you use with your project, the extension also provides you an easy way to access the goals within each plugin.

To debug Maven goals, right-click on a goal and start debugging. The Maven extension will call the Java debugger with the right parameters. This is a handy, time-saving feature.

Generate project from Maven Archetype

Another handy feature provided by this extension is to generate a Maven project from Archetype. The extension loads archetypes listed in local/remote catalogs. After selection, the extension sends mvn archetype:generate -D. to the terminal.

There are several ways to create a Maven project:

Create Maven Project

  1. From the Maven Explorer, select the +Create Maven Project button.
  2. Open the Command Palette ( ⇧⌘P (Windows, Linux Ctrl+Shift+P ) ), search for Create Java Project command.
  3. Right-click on a target folder and select Create Maven Project.

Gradle

VS Code supports Gradle Java project (not including Android) via the Gradle for Java extension. The extension provides a visual interface for your Gradle build, you can use this interface to view Gradle Tasks and Project dependencies, or run Gradle Tasks as VS Code Tasks. The extension also offers a better Gradle build file authoring experience including syntax highlighting, error reporting, and auto-completion.

Working with Gradle tasks

When you open a Gradle project in VSCode, you can find some useful Gradle views by clicking the Gradle Side Bar item. Gradle Projects view lists all the Gradle projects found in the workspace. You can view, run, or debug Gradle tasks here.

When there are many Gradle tasks in the workspace, it might be hard to find a specific task. The extension offers a Pinned Tasks view to help you pin your favorite tasks so that you can easily find them in a separate view. You can also see recently executed tasks in the Recent Tasks view.

Viewing Gradle dependencies

In the Gradle Projects view, you can find a Dependencies item under each Gradle project item. It includes all the dependencies in your specified configuration, you can easily check the dependency status of your project.

Gradle Dependencies

Managing Gradle Daemons

The Gradle Daemons view shows the daemon status of the current workspace. It lists all the running Gradle daemons in the same version as the workspace. You can choose to stop a specific one or all the daemons in this view.

Authoring build files

The extension provides some useful authoring features on the Gradle build files.

When opening a Groovy Gradle file, the extension will analyze the Gradle file and provide semantic tokens information, providing more precise highlighting results.

Gradle Highlighting

In the Outline view, the extension provides the document symbols of the opened Gradle file, which can help you to navigate to any part of the file easily.

Gradle Outline

If there is any syntax error (missing characters, type not found, etc.) in the opened Gradle file, you can find them in the Problems view.

Gradle Problems

The extension supports basic auto completions for a Gradle file, when you’re trying to type Gradle closures or properties in a Gradle script, the extension will suggest available closures or properties for you.

When you are trying to declare a new dependency, the extension will provide a dependency candidate list for you.

Additional resources

Visit the GitHub Repo of the Maven extension for additional configurations and a troubleshooting guide.

In addition to Maven, there’s also a Bazel extension if you use Bazel to build and test your project.

Next steps

Read on to find out more about:

  • Java editing — Explore the editing features for Java in VS Code.
  • Java debugging — Find out how to debug your Java project with VS Code.
  • Java testing — Use VS Code for your JUnit and TestNG cases.
  • Java extensions — Learn about more useful Java extensions for VS Code.

Источник

In this article, we will go through the possible options for any Java developer to choose from the various options that are available out there to build your Java projects. As each of these options available in the market today, have their pros and cons, we will go through them in much more detail and understand the best possible options that we can choose out of the rest.

Also, we would be discussing the options which are pretty awkwardly present but are still valid tools that could be used by any of the Java developers in detail to whatever extent possible.

Enroll now for the ultimate Core Java Training from Mindmajix.

Best Open Source Java Build Tools

Any Java developer who would have worked on a project of considerable size would have definitely come across any of the following that we would discuss further in great detail. There can be possibilities that you might not have come across them as nubile Java developers, but it is a very good option for you to take up the necessary learning in understanding any of the build technologies that you are inclined towards. As we progress towards automating most of the mundane tasks and activities that are being taken up by the developers, these options come to the rescue of doing just that.

Let us not dwell any further on the discussions whether or not these build tools are required for an average Java developer or not, but let us get into the core of the discussion on how these build tools help any of the Java developers in their day to day activities. We will be discussing more the following three build tools in a great deal:

1.ANT with Ivy:

ANT was one of the only available options back in the 2000s where there weren’t many options available other than ANT. It was released in the year 2000 and within a short span of time had become the most popular build tool for Java projects. ANT in conjunction with Ivy can be termed as the MODERN build tool.

Ant is a java library, which helps to drive the process defined in the build file. Mainly Ant is used to build Java applications. Ant is very flexible, it does not impose any rules like coding conventions, directory structure. Ivy is a subproject of Ant, which acts as a dependency manager.

ANT has a very low learning curve, as it is very close to understanding similar to XML files and hence it was imbibed as a must-know technology for Java developers than on. It was basically on the procedure-oriented way of coding.

One of the biggest advantages and also the biggest disadvantage of ANT was the usage of XML as the medium to write the build scripts. This was not a perfect fit for a procedural-oriented programming approach ANT uses but has improved a great deal on accepting plug-ins after the initial release.

If the build scripts are not well documented with proper comments, then it becomes unmanageably big and maintenance gets tougher. Later on, as dependency management over the network became a must, Ant adopted Apache Ivy. The main benefit of Ant is its control of the build process.

2.Apache Maven

Apache Maven was released in the year 2004 after ANT was released in the year 2000. The main goal was planned to fix a few of the developer hiccups while using ANT. Maven hasn’t solved the problem with the XML configuration but has solved it to an extent that needs a little bit of configuration rather than writing up tons of lines of XML code. XML as build configuration format has imposed strictly a structured and a standardized way.

Maven has taken a step further on relying upon conventions and provisioning targets or better understood as goals that could be invoked instead of developers putting in all the commands that lead to the successful execution of a task. Maven has also answered the problem of dependency manager while ANT had to rely on its subproject Ivy for these tasks to be done.

That been said, Maven has its own set of issues with dependency management where it can’t handle conflicts between different versions of the same library (where Ivy was way better with ANT in this particular aspect). Since Maven addresses more over dependency management, writing complex build scripts gets tougher in Maven than ANT.

Maven on bigger projects may have few hundreds of lines of code without actually doing anything great but just handling the dependency management. One of the biggest advantages that Maven can handle with much ease is the handling of the life cycle and comes at the cost of flexibility. Maven is more than a build tool. Maven even describes how software is built and helps in dependency management also. Maven is used mainly for java based projects.

3.Gradle:

Gradle as a build tool has imbibed in the good parts of both the build tools namely ANT and Maven with DSL and also various other improvements. Gradle has the power of ANT and also the flexibility of Maven with its life-cycle abilities. The end result of this amalgam is the advent of Gradle in the year 2012 which has gained a lot of attention in the shortest span of time.

Gradle doesn’t use XML but has its own DSL based on Groovy (one of the JVM languages available). Gradle uses Groovy scripts for declaring the project configuration. Having said that, Gradle build scripts look much smaller in the code lines and looks clearer and neater than ANT or Maven put together. Gradle had initially used Apache Ivy for its dependency management and later on has moved on to use a native dependency resolution engine.

MindMajix YouTube Channel

Gradle’s effort can be summarized into the smallest phrase that aptly fits it – “Convention is good and so is flexibility”. Google is a perfect example who has absorbed Gradle as their default build tool to build their Android OS and also has become their default build tool for Java projects as well.

Gradle was designed for multi-project builds and supports incremental builds by determining which parts of the build are up-to-date. Ant is mostly treated as a legacy right now. Industry going forward with Gradle build tool. I personally feel, Ant and Maven still we can use, it mainly depends on the project. Sometimes we can use a combination of Ant and Gradle, Maven and Gradle, or even three together.

Conclusion:

In this article, we have gone through the requirement of a build tool for any Java developer in particular. With the basic understanding of what can be achieved by using a build tool, we have introduced you with the 3 best tools that are basically available for usage. Going through all the details that are provided, selecting a build tool for your java projects with further depending on the choices that you make.

Источник

Оцените статью