Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Tutorial: Java and Maven in Eclipse Che

DZone's Guide to

Tutorial: Java and Maven in Eclipse Che

Eclipse Che was written in Java. Get started with the rich Intellisense and Java tools in this tutorial from Tyler Jewell.

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

Eclipse Che was written in Java. Get started with the rich Intellisense and Java tools in this tutorial.

1. Start Che

Use your SaaS account for the following, or if you have installed Che, open a terminal and use the Che startup script:

Shell

# Launch Che
che start

When you execute this command, you'll see the URL for the user dashboard.

The Che dashboard will open. It is where you manage your projects and workspaces. If you do not have any projects in Che, you'll be asked to create a new project. If you already have projects in Che, click on the New Projectbutton in the menu bar.

2. Create Console Java Project

From the Dashboard page, click Create Workspace.

2a. Select Source

This informs Che where the source code for your project is going to come from. It is possible to start a new blank, template, sample project or import one from another location. Choosing the first option will present you with a set of samples that are preconfigured. If you already have a project with a valid URL, choose the second option. Che gives you choices on how to source the project from Git, GitHub, ZIP, etc.

We will create a project from a provided template.

2b. Select Stack

Your project will be inserted into a workspace, which has a provided Docker runtime. Stacks are the recipes or images used to define the runtime environment for the workspace where your project will be placed. There are three ways to choose a stack:

  1. Ready-To-Go Stacks. Environments that have a large variety of tools already installed optimized for projects of a particular type. For this example, we will select the Java stack which will create a container with Ubuntu git, java-jdk, Maven, Tomcat installed.
  2. Stack Library. Offers finer-grained stacks that can be used to create specific technology layers needed for a project. Ubuntu and Debian stacks, for example, are minimal stacks with only an operating system and Che tools installed.
  3. Custom Stack. You can provide your own custom stack. You'll have the ability to upload a recipe (Dockerfile) or directly edit it from there.

Choose the Ready-To-Go category and select the JAVA stack.

2c. Configure Workspace

Provide a name to your workspace and configure its RAM. RAM will be the memory limit applied to the machines running your workspace environment. For this tutorial, create a new workspace with name tutorial-java and set its RAM to 1GB.

2d. Select Template (Code Sample)

A template is a set of code, configuration, and commands that can be imported to operate within Che. There are two types of templates:

  1. Ready-to-run project samples. These samples have a compilable source tree and embedded commands. The list of templates available is filtered based upon the stack chosen.
  2. Wizard-driven project configuration. This creates a blank project and then opens the IDE's project configuration wizard to let you scaffold a new project based upon a set of configurable parameters. This option is only available if there an appropriate project type available for the stack chosen.

Choose Ready-to-run project samples and select console-java-simple, those options should be preselected by default.

2e. Project Metadata

You can set a name and description of your project. The name is what will appear as the root node in the project explorer in the IDE. Keep the default values.

2f. Create the Project

Hit the button Create at the bottom of the flow. The project construction will start.

The project construction process goes through a number of steps including creating a new workspace, downloading an image to use for the workspace environment, instantiating that environment, installing Che services into the workspace, and then creating your project.

3. Develop in the IDE

The project created is a Maven project. Once you open it in the IDE, you'll see the dependencies updated from the pom.xml file.

3a. Project Explorer

On the left side of the IDE, a panel is displaying the project explorer which allow you to browse the sources of your project. You can use your mouse to expand/collapse the folders and packages, but you are also able to navigate in the project explorer using your keyboard. Use:

  • up arrow and down arrow to navigate in the tree,
  • left arrow and right arrow to expand/collapse folders and packages,
  • enter to open a file.

3b. Editor Overview

Open the file HelloWorld.java in the package org.eclipse.che.examples. The file is displayed with syntax coloration.

The editor is structured in a common way:

  • At the top: the list of all opened files
  • On the left: line number, breakpoints and error marks
  • On the right: the file's minimap and the cursor position bar to navigate in the file
  • At the bottom: file information (cursor exact position, encoding and file's type)

If you have errors in your files, the editor will display error and warning marks:

You can use your keyboard to navigate in the file content, but also between files. You can get the complete list of all keyboard shortcut by going into the menu Assistant > Key Bindings and scrolling to the category Editor

3c. Java Intellisense

There is an Assistant menu that includes language specific capabilities. You can perform auto-complete by hittingctrl-space.

3d. Jump to Definition

While you are editing your Java code, you may want a documentation lookup for a particular symbol (class, attribute or method). Get quick documentation by Assistant > Quick Documentation or ^j.

If you need more information about the symbol, you can also navigate to its definition with Assistant > Open Declaration or F4. The String class will open in a new editor.

3e. Search

Che editor provides various ways to search your projects and workspace.

Search With Editor

Use the editor search to find and replace in a particular file via CTRL+f keyboard shortcut. You can also use regular expressions.

Find Usages

This will find all references of a particular class, method, field, or attribute and search for its usage in your various project's files. Do this with Assistant > Find Usages. A new panel will open and list all references for String into your project. If you select one of the occurrence and double click on it, the editor will open the file to the position of the found reference.

3f. Refactoring

Che provides the ability to refactor your source code.

Rename

Put your cursor on the method, variable, or field that you want to rename and hit Shift + F6. If this hotkey is pressed once, the selected keyword will be highlighted which means it's ready for refactoring. You can type a new name and press Enter.

If you press Shift + F6 twice, an advanced Rename mode is called out:

The preview button will open a side-by-side comparison window that will show changes that you are about to apply.

Move

Choose any Java class you want to move and hit F6. It will call a Move item menu. Choose the destination for your class and click OK.

It's also possible to preview changes. Choose the destination for your class and click Preview. It will show all Java classes and non-Java files (optional), that the replaced class is referenced in.

Manage Maven Modules

Maven Plugin provides the ability to manage Maven modules in multi-module projects entirely through pom.xml.

Open any Java multi-module project and create a new folder with a simple Maven project in it. It will be seen as a folder in the project tree first. Open your parent project POM and add your newly imported module there:
<module>new-module</module>

As a result, it will be automatically configured as a Maven module in your project tree. Maven plugin watches changes in pom.xml and automatically imports changes (dependencies, configuration etc).

Dependency Management

If you make changes to dependencies in POM, they will be automatically updated. You can also manually reimport the project: right click on your Maven project, choose Maven > Reimport.

If you have errors in your POM or add some nonexistent dependency to your POM, the following error will be displayed in the editor:

Configure Classpath

It's possible to view project dependencies at Project > Configure Classpath. Dependencies in the classpath will be categorized as follows:

  • JRE_CONTAINER - Java 1.8 jars;
  • MAVEN2_CLASSPATH_CONFIGURATION - project dependencies.

Generate Effective POM

There is also a possibility to display POM that results from the application of interpolation, inheritance and active profiles. Just open your Maven project and go to Assistant > Generate Effective Pom.

It will open an effective POM with all project dependencies, plugins, plugins configurations up to the root dependencies.

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:
java ,maven ,ide ,tutorial ,devops ,eclipse

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}