Tuesday, September 20, 2011

Recipe#6: Configure Maven2

When I started programming 8 years ago, ANT was the build tool of choice for Java programs. These days, you are better off using Apache Maven, which adds dependency management, build profiles, and automation for common tasks (like documentation and site generation, distribution, and test).

To install Maven:
  1. Download the package from http://maven.apache.org/download.html.
  2. Unzip the contents to a location that will be Maven's home directory.
  3. Set the "M2_HOME" environment variable to point to the home directory. Some plugins still require it, although it is no longer necessary for Maven itself.
  4. Add %M2_HOME%\bin to environment PATH so that you may run the mvn command from anywhere.
By default, Maven creates a local repository under %user_profile%\.m2. To control where artifacts and dependencies are stored, specify an %M2_REPO% environment variable that points to a directory where you want the local repository.

Because I develop in the Eclipse IDE, it is configured it with the m2eclipse Maven plugin, which allows me to void a DOS console when I build projects and do everything in Eclipse:
  1. In Eclipse, create an update site for m2eclipse [Help > Install New Software. Add m2eclipse=http://download.eclipse.org/technology/m2e/releases.
  2. Select "Maven Integration for Eclipse", click [Next]. Agree to license terms and [Install]. You will need to restart Eclipse.
Before you start using m2eclipse, it pays to look over the configuration. Among the things I changed: having the plugin use the Maven installation from earlier because the plugin references non-release or snapshot version of Maven. If you have other programs or scripts that use Maven outside Eclipse, this action promotes build consistency. Also consider the settings file, which you can now edit in Eclipse.

Some recommended reads on Maven2 can be found at http://maven.apache.org/articles.html. I like "Maven 2: Effective Implementation" because it also touches on Apache Archiva and Continuum. Otherwise "Apache Maven 3 Cookbook" will get you up-to-date on the latest usage of Maven.

Wednesday, September 07, 2011

Recipe#5: Configure Eclipse for SVN

With access to an SVN server, you need to configure your IDE to connect to available SVN repositories. In Eclipse, you'd need to install and configure the Subclipse SVN plugin:
  1. Open menu Help > Install new software ...
  2. Add the Tigris Subclipse update site http://subclipse.tigris.org/update_1.6.x
  3. Select the Core SVNKit library, JNA library, and Subclipse.
  4. After installation, you can further configure the plugin at Window > Preferences > Team > SVN.
To check in a project:  right-click project in Eclipse, select Team > Share Project.
To checkout a project: select menu File > Import, select SVN > Checkout Projects from SVN.
Thereafter, all version control operations (update, commit, add, etc) are available from the Team menu.

Tuesday, September 06, 2011

Recipe#4: Install an SVN Server

Even if you are the sole programmer on any project, it is imperative that you use version control for your source code and documentation. I use Apache Subversion (SVN) for, among other things:
  1. Be able to revert to a previous version of an application, either because my current working copy is corrupted or full of unanticipated bugs.
  2. Sharing the project with other developers for collaboration or contribution.
  3. Maintaining multiple branches or tagging code at specific milestones.
To use SVN in development, you need an SVN server and SVN client. I use Visual SVN for the server component, installed on a separate machine than what I use for development. VisualSVN is easy to manage and configure, but I mostly like its ability to authenticate via Windows users or your own creations, and access to the checked-in artifacts over HTTP(s) (browser and clients).

On why you have no excuse for not using source versioning:
http://betterexplained.com/articles/a-visual-guide-to-version-control/
http://www.relisoft.com/co_op/whyvcs.html
http://www.mactech.com/articles/mactech/Vol.14/14.06/VersionControlAndTheDeveloper/index.html
http://jamesmckay.net/2009/06/why-would-anyone-not-use-source-control/
http://www.soundsoftware.ac.uk/why-version-control

Friday, September 02, 2011

Recipe#3: Install the Eclipse IDE

No serious Java developer I know writes code in a text editor; you need an Integrated Development Environment (IDE). The benefits of using an IDE can't be emphasized enough, and with many free options out there, you have no excuse. (See a comparison of the major Java IDEs). My IDE of choice is Eclipse.
  1. Download Eclipse (http://www.eclipse.org/downloads/). Because I mostly develop web applications, I use the "Eclipse IDE for Java EE Developers" flavor.
  2. The platform version you get depends on what JVM you have installed. If the JVM is 32-bit, get the 32-bit version of the IDE.
  3. Extract the downloaded .zip to a location that will be Eclipse's installation directory.
  4. Run %install_dir%\eclipse.exe. Should open the IDE and prompt you to configure workspaces. My practice is to specify a workspace for each major project I work on, most of which will have many related Eclipse projects.
  5. Since Eclipse is not installed like other Windows applications, create a shortcut on your desktop to the Eclipse executable.
You'll get more use out of the IDE if you learn to use it properly. Invest in a book on this topic or Google for tutorials (check out http://www.vogella.de/articles/Eclipse/article.html for a start).

Thursday, September 01, 2011

Recipe#2: To Start Developing In Java

To start any Java project, you need to install a Java Development Kit (JDK) on your computer. The kit contains the virtual machine (JVM), the language spec, and a number of other utilities. Only three things need be done to properly install a JDK:
  1. Download the latest J2SE from http://www.oracle.com/technetwork/java/javase/downloads/index.html and run its installer.
  2. Configure the environment variables so that your installation is available on the system classpath. In either PATH or CLASSPATH, add %install_directory%\bin. At the commandline, you should be able to run java -version and see version information.
  3. Create a JAVA_HOME system environment variable and point it to the directory where the JDK is installed. Plenty of Java tools, plugins, applications, and utilities expect this variable to exist.
At this point, you can start developing Java code (using any text editor and the shell/commandline). A word of caution: although Java SE7 has some great new features, some have observed a few drawbacks. Ref: "Java SE 7 'Buggy,' Causing Crashes Says Apache Lucene".