How to start a project successfully is often overlooked, but is always important. Projects often consist of many components that are built separately and then combined together to create a larger system. Because of this, we will look at how to create and share libraries so we can benefit from modularity and a more decentralized system. This will also help in configuring your environment to work with, for example, checking project folders into and out of version control systems.
Due to the decentralized nature of software engineering, we will take a look at how to import projects from different sources. This will give us a greater opportunity to work with various teams that use different sets of tools while still benefiting from NetBeans's capabilities.
Let's start with a Java project. There is not much configuration to be done with this kind of project, but this will help in familiarizing with the IDE. It will also will give some insight on how to create other, more advanced projects since the flow is almost identical.
If you do not have NetBeans installed on your machine yet, please visit:
It is also necessary to have Java installed; please visit: http://www.oracle.com/technetwork/java/javase/downloads/index.html
Let's create a New Project. This can be achieved by either clicking File and then New Project, or by pressing Ctrl+Shift+N.
In the New Project window, two panes will be shown. Choose Java on the Categories pane and Java Application on the Proejcts pane, then click Next.
At the Name and Location step, Project Name should be filled with
At Create Main Class, append com to the beginning of
Leave all the other options marked and click Finish.
The configuration window should look like the following screenshot:
While the project location can be specified, since we did not change the folder, NetBeans automatically adds the project to NetBeansProjects, which is the NetBeans default project folder.
Use dedicated Folder for Storing Libraries is marked since we would like to have a specific project folder where all of the libraries will be stored, making packaging and distribution easier in the future. Libraries are packaged as JAR files. A JAR file is an example of a library that contains Java sources, which enable the developer to extend the functionality of applications.
NetBeans will create the package structure, which will consist of Source Packages and Test Packages. The code templates provided by the IDE will correctly place the files in their respective folders. For example, if one JUnit test is created, the file will be placed under the Test Packages folder automatically.
One node containing libraries will also be shown in the projects view. This is where NetBeans will copy the necessary libraries. In the Java project, for example, NetBeans copies and configures the JUnit libraries into our newly-created project by default. Different projects will have different dependencies.
We will create a library folder upon project creation. For this recipe, we will create a Java EE project.
A library consists of a set of files that are packaged inside a compressed file. Many files can exist inside a library but usually, in Java, libraries contain only class, JARs, and configuration files. Libraries in Java can have different extensions.
Libraries in Java can have different extensions:
EAR: The Enterprise Archive usually encompasses all the other files combined. Normally it contains other WARs (and will also hold the same file types), configuration types, and other JARs. This file is better suited to compact into a single file, many different modules of an application.
Both WAR and EAR are built on top of JAR. These files will be explored in future recipes so for now we will only work with normal JAR files.
Please refer to the Getting ready section of the previous recipe for necessary tools and configurations.
Once that is complete, it is possible to continue to the next section.
Create a New Project by either clicking File and then New Project, or pressing Ctrl+Shift+N (shortcut).
In the New Project window, choose Java Web on the Categories side and Web Application on the Projects, then click Next.
At Name and Location, click on Use Dedicated Folder for Storing Libraries.
Now, either type the folder path or select one by clicking on Browse.
After choosing the folder enter Project Name as
CreatingLibrariesand, proceed by clicking Next and then Finish.
Since we are creating libraries, a new project is going to be needed for this purpose. Projects can be packaged as libraries and reused in other projects to extend their functionality. Let's not worry about changing any of the default values that are set by the IDE for Project Name and web servers, since it is not a part of the scope of this recipe.
When selecting Use Dedicated Folder for Storing Libraries, it should be noted that this is where the libraries required for compilation will be stored.
NetBeans will perform the heavy lifting of copying all the necessary libraries to the specified folder. After following these steps, a new project will be created with the shared libraries folder at the chosen location.
After creating a library directory, the next step is to actually share the project's library.
When projects become mature enough, or just for testing purposes, the developer can package the entire project into compressed Java Archive Files (JAR). These compressed files might be enterprise (EAR) or web (WAR) format, or simply JARs.
For this recipe, we will be using the previously created project and creating the WAR file from it.
If no Project is currently available at the Projects tab, follow the Creating libraries recipe to ensure that this recipe will work.
Click on the Projects tab, or press Ctrl+1.
Right-click on the project's name and click on build.
Click on Files, or press Ctrl+2, to navigate to the File structure of the project.
Expand the project node by double-clicking on the project's name.
Select Run Target.
On Run Target's sub menu, click on dist.
The Output panel will show the path in which the WAR file is placed.
This script is incorporated into NetBeans menus while on the Projects panel, as seen by the build menu. By clicking on build, NetBeans will call the Ant script and initiate the process of bundling all the necessary files of this project into the WAR file.
The second step-by-step is given as an example on how to access the dist target through the Files panel.
Unsure which way to execute? And why is it good to have a separate
build.xml generated by NetBeans?
The second method, though longer, is very complete in the sense that other targets can be attained, in case the dist is not necessarily what we want.
The advantage with NetBeans
build.xml is that it can be used outside the IDE as well; very useful if you are deploying the application in a machine that does not have access to a Graphical Interface. It also lets the project be packaged without the necessity of using NetBeans.
For example, sometimes the developer needs to connect remotely to some server that does not provide a graphical window manager to solve issues/bugs/introduce features. In this case, it is easy to make necessary changes, package the solution, and deploy to the application server.
This functionality will import one or more projects created by the Eclipse IDE simply by specifying the workspace in which they are housed.
A valid Eclipse project, with sources and dependencies, must be used in order to continue with this recipe.
Click on File and then on Import Project and Eclipse Project....
In the Import Eclipse Project window, select Import Eclipse Project.
Click on the Browse... button. An Open dialog will pop-up for workspace selection.
After selecting a valid workspace project, click the Next > button.
In the Projects to Import section, select the projects you want to import.
Select Store NetBeans project data inside Eclipse project folders.
Click the Finish button.
By selecting the workspace directory, NetBeans will then analyze and convert the metadata created by Eclipse.
The project structure, along with the dependencies, will be available for NetBeans usage. It is important to notice that NetBeans will not change the way the Eclipse project behaves.
On Store NetBeans project data inside Eclipse project folders, NetBeans will create its own structure inside the Eclipse folder structure. Select this option if you want to distribute the NetBeans project directory in a Version Control System. This ensures that libraries and configuration files used by Eclipse and NetBeans are the same.
It is also possible to use NetBeans without placing its configuration files inside Eclipse's workspace. If this is the desired outcome, then select Create imported NetBeans projects in a separate location and then click on Browse... to select the folder where NetBeans configurations will exist.
The imported Eclipse projects will then be placed on the Projects tab on the right-hand side.
In the There's more… section, there is a list of some errors that might arise when importing an Eclipse project.
Now let's talk about some other options, as well as some pieces of general information that are relevant to this task.
With multiple developers working on a project, it is common that changes to a project happen from time to time. When this happens, NetBeans can resynchronize the projects by the following:
Import Eclipse Project
Resynchronize Eclipse Projects
After following these steps, the classpaths of all the projects imported to Eclipse will be in sync. Changes to your local project will not be synchronized back, however. This way, NetBeans ensures that the local configurations will not damage the parent project.
While importing Eclipse projects, some importing errors might come up.
Many of these errors are not specific to our recipe but the following notes might come in handy while developing future projects.
Some of these errors are:
Resolve Missing Server Problem: Just right-click on the project node and browse to the folder where the server is installed.
Resolve Reference Problem: Occurs when libraries are missing from the project class path. Solving this is very similar to the missing server problem. Right-click on the project node and select Resolve Reference Problem, and then select the folder where the library is.
Eclipse platform for Project Name cannot be used. It is a JRE and the NetBeans project requires a JDK. NetBeans will use the default platform: This is due to the Eclipse project being configured with a JRE instead of JDK. To solve this, click on the toolbar in Tools, and then select Java Platforms. The Java Platform Manager will be shown, click on Add Platform... and from the options, select the correct Java Platform the application is being developed on.
With NetBeans, Maven support is already integrated to the IDE. And with the latest release, 7.0, Maven 3.0 is integrated.
There is no requirement to download or configure plugins. It is as simple as that.
Note that even if Maven is not in the System Path, NetBeans already has an embedded version for ease of use. This is very useful if there is no specific Maven version to be used.
However, it is a good practice to install and configure Maven properly.
For more information, visit: http://maven.apache.org/.
An existing Maven project should exist in order to continue with this recipe.
Click on File and then Open Project or press Ctrl+Shift+O.
The Open Project dialog will be shown.
Select the folder where the Maven project is placed by clicking on Look In (A Maven project will have the following icon):
Once the File name changes to the correct folder, click Open Project.
After clicking Open Project, the Projects tab will show the Maven structure.
Maven, like Ant, helps developers package and maintain projects. Developers can perform certain tasks by invoking Maven goals, such as packaging and executing test cases.