IntelliJ IDEA is a next-generation integrated development environment (IDE) for Java. As the term IDE implies, IDEA combines all the tools you need to develop Java software into a single application and interface. In other words, IDEA is a tool that helps you develop Java applications more quickly, easily, and intelligently. IDEA can help you with every phase of your project, from design and development to testing and deployment, provided you know how to make the most of it.
IntelliJ IDEA provides a powerful environment for managing your Java projects and source files as well as building and testing your applications. Its support for popular industry standards like J2EE, Ant, JUnit, XML, and various revision control systems ties together all the core aspects of Java development under a single tool.
Beyond the integrated tools, fancy editor, and snazzy project management features, IDEA provides a huge number of features aimed at increasing your productivity and improving your development experience. IDEA can help improve your efficiency by eliminating mundane and repetitive coding tasks, alerting you to errors before compilation, tracking code changes, and enabling powerful refactoring. By digging a little deeper into IDEA and embracing these features, you can become a more effective Java developer, because you?re free to spend time on project design rather than code management.
Work in IntelliJ IDEA begins with the concept of a project. A project encapsulates all your
source code, library files, and build instructions into a single organizational unit.
Since version 4.0, IDEA’s modules and libraries let you segregate larger, complex
projects in more manageable structures that can share common code. Modularized
projects are also a great benefit when you’re building enterprise applications
composed of several different components with complex interdependencies.
Understanding IDEA’s project strategy
When you work on source code in IDEA, you do so in the context of a project.
Because everything in IDEA revolves around the project, it’s important to have a
firm understanding of how and why IDEA handles projects the way it does.
Examining the IDEA project hierarchy
If you’ve never used an integrated development environment (IDE) like IDEA
before, you may not immediately understand why you have to define a project
before diving into your work. Remember, however, that IDEA isn’t a simple text
editor; it’s a Java development environment. As such, you can’t just start typing in
your source code willy-nilly. First you have to create a project.
What is a project?
A project in IDEA is an organizational unit that represents a complete software
solution. Your finished product may be decomposed into a series of discrete, isolated
modules, but it’s a project definition that brings them together, relates them
with dependencies, and ties them into a greater whole.
Projects don’t themselves contain development artifacts such as source code,
build scripts, or documentation. They’re the highest level of organization in the
IDE, and they define project-wide settings as well as collections of what IDEA
refers to as modules and libraries.
What is a module?
A module in IDEA is a discrete unit of functionality that can be run, tested, and
debugged independently. Modules contain the development artifacts for their
specific task; this includes such things as source code, build scripts, unit tests,
documentation, and deployment descriptors. IDEA supports different types of
modules, from plain Java applications to web apps, EJB modules, and so on. For
many projects, a single module will suffice.
Although the project may be the center of attention, the module does all the
work. Without a module, your project has no source code, no output, nothing
other than a collection of configuration preferences. Like projects, modules
often require alterations after their initial creation. Their participation in a
project, as well as their individual internal settings, is controlled through IDEA’s
Settings interface.
What is a library?
A library is an archive of compiled code that your modules depend on. Such an
archive is typically represented as a JAR file or an expanded JAR in a directory.
Libraries may optionally contain references to source files and API documentation:
Including these references doesn’t alter the usage of the library in any way,
but it does add valuable information to the editor during class navigation and
inspection. Examples of libraries include a DBMS vendor’s private JDBC driver, or
an open-source XML parser.
Using IDEA’s library features makes it easy to manage the often-burdensome task
of building applications utilizing third-party toolkits. IDEA manages not only the
classes and JAR libraries for you, but the source and reference documentation as
well. IDEA supports three different library configurations, which determine the
scope and reusability of the library within your environment:
- Project libraries are defined within a project for its exclusive use.
- Module libraries are defined within a module for its exclusive use.
- Global libraries can be used by any project.
Selecting different types of modules
IDEA provides four distinct types of modules, which fall into two categories: basic
Java modules and enterprise Java (or J2EE) modules. As a head
start, here’s the purpose of each of the available module types shipped with IDEA:
Idea 5 |
---|
With version 5.0, this list has been expanded. Now there are six types of modules, |
- Java modules are the simplest module type and represent a basic Java application
project, whether it’s a command-line tool, a Swing application, or a
JAR library. When configuring this type of module, you can specify a set of
Java source paths that will be compiled to a single class folder. The
basic capabilities of this module are carried over into the web module. - The web module is an extension of the Java module that adds support for
web applications. In addition to providing the ability to create and build
Java sources, it lets you edit your web application’s deployment descriptor,
build and deploy it to your application server, and configure other web
application capabilities. You create a web module for each web application
in your project. - An EJB module lets you design and package a collection of Enterprise Java-
Beans. - A J2EE application module is different than the other module types discussed
so far. The J2EE module type is primarily concerned with packaging J2EE
applications for deployment as enterprise archive (EAR) files. As such, it
references Web and EJB modules that it packages for deployment.
Idea 5 |
---|
IDEA version 5.0 comes with two new module types: J2ME modules and IntelliJ |
Selecting a project structure
Because it’s the module that defines a set of source files, a typical project must be
composed of at least one module (you can create a project with no modules, but
it’s useless until the first module is added). For many projects, a single module is
all you need. For more complex projects, especially J2EE projects or software
suites composed of several discrete applications, a multi-module structure is
more convenient. Separate modules let you build and test each piece separately
while maintaining a common configuration. You obtain three primary benefits
from breaking your project into modules:
- Reusability and sharing of modules between projects
- Improved project structure
- Module specific features
One benefit of the IDEA’s modular projects is that a module can be shared among
several projects if the need arises. Take, for example, a collection of utility classes
that you’d like to share among several different types of projects. By putting them
into their own module, you can easily add it to your other projects while maintaining
the ability to develop it independently.
Modules can be built, tested, and versioned independently, so they’re a great
way to reduce the complexity of large projects. You can choose to compile and test
a single module for example, without waiting on the rest of the application to be
built. In addition, you can take a single module from many in a complex project
and place it into a second project by itself, allowing you to remove the overhead
and distraction of the larger project.
Modules in IDEA come in several different flavors, each designed with a particular
type of application in mind. These application-specific modules extend
the capabilities of IDEA to support new types of applications and to assist in their
development and deployment. Web modules offer one-click deployment,
whereas the J2EE module packages your application into an EAR file. No doubt
future releases and third-party extensions to IDEA will add new types of modules.
Imagine you’re building a Microsoft Office-style suite of office applications
consisting of spreadsheet, word processor, and presentation designer applications.
One natural way to model this project is to create a separate module for
each application in the suite along with a module representing a set of utility
functions common to all three applications.
Working with projects
Project creation is simplified with the assistance of the New Project Wizard, a
process you’ve already run through with the creation of your “Hello World” and
ACME projects. Things have a habit of changing over time, however, and it’s a
rare project that doesn’t need some sort of reconfiguration during its lifetime.
Ongoing project maintenance is handled through IDEA’s IDE Settings window,
which lets you change almost every aspect of the project you’re working in.
Creating a new project
You create a new project by selecting the File | New Project command to launch
the project wizard. The project wizard takes you through the steps required to set
up a basic project and, if you desire, set up the project’s initial module. When you
first launch IDEA, it automatically directs you to the New Project Wizard.
Idea 5 |
---|
In version 5.0, the number of steps (as well as the order of the steps) of the New |
Specifying the name and location of the project
In the first panel of the New Project Wizard (see figure 1), you’re asked to specify
the name of the project and the folder where the project file will be created.
The name of the project file will be the name of the project plus the .ipr extension,
so if you have specific requirements concerning spaces (or lack there of) in
your filenames, take appropriate action. There is no requirement that your
project file be located anywhere in particular, but you must consider several factors
when choosing a location:
Figure 1: Step 1 of the New Project Wizard involves choosing a project name and a location to store
the IPR file.
- You’ll have fewer problems sharing or relocating your project if you keep
all of your project’s file and components at the same level or below the
project file itself. - If you’ll be maintaining your project file in a source control system, it’s
convenient to place the project file in the root of your source control
project. - Keeping all of your project files in the same folder makes it easier to find
and access them; likewise for your module files. However, doing so may be
less convenient with regard to your source code control system.
If the folder you specify for the project doesn’t exist, the wizard asks you if you
wish to create it. Remember that you’re specifying the folder where the project
file will be created, not the name of the project file itself (this is created automatically
and is always the same as your project name). When you’re ready, click Next
to continue.
Selecting a project JDK
In the second step of the New Project Wizard (see figure 2), you’re asked to
specify which JDK to use for the project. Make your selection, and click Next.
Figure 2: Step 2 of the New Project Wizard involves selecting a target JDK for the project.
Your choice of JDK determines which compiler and API library are used to build
your project, unless a module specifically overrides this setting.
Selecting a single or multi-module project
At this point, the New Project Wizard has everything it needs to create a new
project. However, without any modules, a project is just an empty shell and not
very useful. This step lets you create your initial module (through the wizard) by
selecting the first option Create single-module project and clicking Next (see
figure 3).
If you want to create a multi-module project, or if you want an empty project to
which you’ll add modules later, select the second option Create/configure multimodule
project; doing so changes the Next button to a Finish button. Click Finish to exit the wizard. The project is created, and you’re taken to the Add Module
Wizard to create or import modules.
Managing project settings
Along with its global settings and preferences, IDEA maintains a collection of settings
specific to each project you define. These settings define not only the
project’s contents and behavior but also related information such as your source
code control settings and compiler behavior. You can access these settings by
Figure 3: Step 3 of the New Project Wizard lets you choose between a single-module and a multimodule
project.
selecting File | Settings, by using the shortcut Ctrl+Alt+S, or by clicking the Settings icon in the main toolbar (the wrench and machine nut). Doing so brings up
the Settings control panel, shown in figure 4. The project-specific categories for
the currently active project are shown at the top of the panel; global IDE settings
are listed in the lower half.
The project-specific options that you can specify include defining the list of
modules involved in the project, the compiler to use for building the project (and
some of its options), the version control system configuration for the project, the
code style to which the project adheres, and a few options controlling the behavior
of the IDEA GUI Designer.
Configuring paths
The Paths settings panel is the main control panel for configuring your project.
However, as you can see in figure 5, this control panel is sparse when no modules
are present (you’ll see another screenshot of this window shortly with modules
in it, for comparison). This is because the modules, not the project, manage
the development artifacts like source folders, dependent libraries, and so forth.
The module list lets you add and remove modules from the project; it’s discussed
in the next section.
Figure 4: The Settings window lets you control project-specific settings as
well as general IDE settings. The two categories are separated for
convenience, as shown here.
Figure 5: The Paths window lets you control a few project-specific
settings, as shown here, but it’s principally used to configure the
paths of the project’s included modules.
The first option allows you to select between absolute and relative paths. If a module,
library, or other referenced file is located outside the project file’s directory,
IDEA uses either the absolute path or the relative path (relative to the project file)
to reference them, depending on which option you’ve selected in the project’s
path options. For files within the project directory, relative paths are always used
to make project files as portable as possible between machines. This setting is also
available on a module-by-module basis, should that become necessary.
The other setting lets you to configure the language level for the project. The
default language level is 1.3, but you can use 1.4 or 1.5. Language level 1.4
enables the assert keyword, introduced in JDK 1.4. By default, Java compilers disable
this keyword, because it wasn’t a reserved keyword prior to JDK 1.4 and may
cause conflicts with older source code. Similarly, language level 1.5 enables some
of the new JDK 1.5 features, such as the enum keyword and autoboxing. If you alter
the language level, you must restart IDEA in order for the option to take affect.
Configuring compiler settings
The Compiler settings panel lets you control build-related options such as
whether to generate warnings, passing additional parameters to the compiler,
and so forth.
Configuring version control settings
The Version Control settings panel lets you integrate your version control system
with your project. Because this is a project-level setting, you’re free to use different
source code control systems for different project or tweak the behavior of each
from project to project. Note that IDEA also maintains a set of global settings for
version control shared from project to project, such as the location of your source
code repository. These settings are also accessed via this panel.
Configuring project code style settings
The Project Code Style settings panel lets you override the code layout and formatting
styles used for this project. IDEA maintains a detailed set of code formatting
options that are shared between projects unless overridden through this
panel. The Code Style settings let you specify everything from the size of your
indents to the spacing around method calls and operator symbols.
Configuring GUI Designer settings
The options in the GUI Designer settings panel pertain to IDEA’s GUI Designer
tool, which makes building user interfaces in Swing relatively painless.
Configuring project template defaults
When you create a new project, the initial settings are based on project template
settings maintained by IDEA. To edit these settings, select the File | Template
Project Settings command to reveal a subset of the Settings panel (figure 6).
IDEA lets you specify default settings for the compiler, version control, code style,
and GUI designer options. Once set, all new projects begin with these default settings.
Saving your project settings
IDEA automatically saves project settings, so there is no need (or opportunity) to
explicitly save your project. The newly defined settings are applied immediately,
so you don’t need to restart IDEA or close and reopen project to make
them effective.
Reopening a project
When you start IDEA, it automatically reopens the last project you worked on,
unless you’ve disabled the Reopen last project on startup option under IDEA’s
General settings. You can open an existing project by selecting the File | Open
Project command and selecting the IPR file corresponding to your project. Or, if
Figure 6: The Template Project Settings window lets you specify defaults for most project-specific
settings. These defaults are applied to all new projects you create.
you’ve used the project recently, it’s listed in the File | Reopen submenu, which
maintains a list of the most recently used projects.
Working with multiple projects
When you attempt to work on a project while one is already open, IDEA asks if you
wish to open the project in a new frame. If you want to work on multiple projects
simultaneously, click Yes. Otherwise, click No to close your existing project and
open the new one.
When you open multiple projects, each is loaded in its own IDEA frame. The
two projects are completely independent; other than letting you cut and paste
between the application windows, they can’t share data. To close a project, select
the File | Close Project menu option.
WARNING |
---|
Regardless of how many projects you have open at once, only a single |
Working with project files
IDEA stores the configuration data for projects and their components in plaintext
XML files, making it easy to manage, edit, and share project configuration data
with others. IDEA creates three different types of files: the project file, the workspace
file, and the module file.
Project files have an .IPR extension and contain information core to the
project itself, such as the names and location of its component modules, compiler
settings, Ant configurations, and so forth. You can click an .IPR file to launch the
project in IDEA. By default, this file is created at the root of the project.
Along with each project file, IDEA creates an .IWS file to store your personal
workspace settings. This file remembers the placement and positions of your windows,
your VCS and History settings, your Run/Debug configuration targets, and
other data pertaining to the development environment. This file is always created
alongside your project file. If this file is deleted, it’s regenerated automatically,
unlike the project .IPR file.
Module files are created for each module you defined and have the .IML
extension. The module file stores all the path and dependency information associated
with the module. Its exact contents depend on what type of module it is. By
default, module files are located in the module’s content root folder.
Summary
Project configuration and management is an important aspect of software design,
and any worthy IDE provides functionality to address this need. Without it, an
engineer may as well be writing software in a plain text editor. The creators of IDEA
have invested much thought and effort into making the project-management feature
set within their IDE support the needs of their audience.
IDEA uses the concepts of projects, modules, and libraries to decompose the
traditional concept of a software project. Projects are the highest level; they
equate roughly to the products you’re trying to produce. Modules are wholly contained
subcomponents-individually buildable, runnable, and testable-that can
be assembled into a larger solution. Libraries are static modules that aren’t
dynamically built or altered but that can be leveraged within the context of a
module. By defining these layers and making them self-contained and modular
(as Java components are touted to be), software designers can begin to reuse code
in multiple projects and also manage that task with a minimum of effort.
About the Authors
Duane K. Fields is a software developer and manager. He co-authored Manning’s best-selling Web Development with JavaServer Pages.
Stephen Saunders is a software engineer with experience in knowledge management, financial services, and data management.
Eugene Belyaev is the cofounder, president, and chief technology officer of JetBrains, the company that created IDEA.
About the Book
IntelliJ IDEA in Action
By Duane K. Fields, Stephen Saunders, Eugene Belyaev
Published: March, 2006, Paperback: 450 pages
Published by Manning Publications
ISBN: 1932394443
Retail price: $44.95
This material is from Chapter 4 of the book.