Managing Projects with IntelliJ IDEA

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,
roughly categorized into basic/standard Java (J2SE) modules, J2EE modules, and
J2ME 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
Plugin modules. A J2ME module is a module suited for working on micro applications
(such as for mobile technologies), and an IntelliJ Plugin module provides
you with a correctly configured module for developing your own IDEA extensions.

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
Project creation wizard may not match those described here for version 4.5.

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.