Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

Monday, October 03, 2011

Recipe#8: Setting Up Parent Maven Project In Eclipse

Whenever you create Maven projects, a best practice is to start with a parent project from which child modules will inherit. The parent POM defines global settings and doesn't itself produce a deliverable. Child modules would include the deliverables, project documentation (user guides and API docs), libraries (if this is a dependency for other projects), etc. It eases maintenance and extensibility tremendously.

This recipe assumes you have Java (see recipe#2), Maven2 (see recipe#6),  and Eclipse (see recipe#3) configured.

In Eclipse:
* File > New > Other. Select Maven | Maven Project [Next]. Use default workspace location [Next]. Select "maven-archetype-quickstart" [Next]. Provide archetype parameters [Finish].
* Update the POM, providing #properties, #description, #licenses, #organization and #developers information, and plugin information for the compiler and surefire (test) plugins. Notice that #packaging ought to be pom.


<?xml version="1.0" encoding="UTF-8"?>
<project ...>
    <modelVersion>4.0.0</modelVersion>
   
    <groupId>com.strive</groupId>
    <artifactId>geldzin2</artifactId>
    <packaging>pom</packaging>
    <version>0.1-SNAPSHOT</version>
   
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.6</jdk.version>
    </properties>
   
    <name>Geldzin2</name>
    <description>
        An online personal financial management application.
    </description>
   
    <licenses>
         <license>
              <name>The Apache Software License, Version 2.0</name>
              <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
              <distribution>repo</distribution>
        </license>
    </licenses>
   
    <organization>
        <name>Strive Consulting, LLC</name>
        <url>http://www.strive-ltd.com</url>
      </organization>

    <developers>
          <developer>
            <id>prideafrica</id>
            <name>Jubz Madagascar</name>
            <organization>Strive Consulting, LLC</organization>
            <organizationUrl>http://www.strive-ltd.com</organizationUrl>
            <roles>
                <role>Architect</role>
                <role>Developer</role>
            </roles>
          </developer>
    </developers>
   
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.9</version>
                </plugin>           
            </plugins>
        </pluginManagement>
    </build>
</project>

Saturday, October 01, 2011

Recipe#7: Simple Maven Webapp in Eclipse

How to create a webapp using Maven, and run it on Jetty in Eclipse. This recipe creates an initial webapp that I want to use as playground for ExtJS 4.x. Requires that you already have Eclipse and Maven configured. Everything is done in the Eclipse IDE.

* Menu File > New > Other.
* Select a wizard: Maven > Maven Project [Next].
* Select project name and location: use default workspace location. [Next].
* Select an archetype: maven-archetype-webapp. [Next].
* Specify archetype parameters: Group Id {com.strive}, Artifact Id {extjs4-projects}, Version {0.1-SNAPSHOT}, Package {com.strive.extjs4projects}. [Finish].
* Create the Java sources directory (src/main/java) and set it as a source folder (right-click: Build Path > Use as Source Folder).
* Open the POM and update #name:{ExtJS4 Sample Projects}, remove #dependencies, #build/finalName:{extjs4}.
* Specify properties and plugins (compiler, surefire, war, and jetty) in the POM:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jdk.version>1.6</jdk.version>
  </properties>
  <build>
    <finalName>extjs4</finalName>    
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>${jdk.version}</source>
          <target>${jdk.version}</target>
        </configuration>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.9</version>
      </plugin>
      <plugin>
          <groupId>org.mortbay.jetty</groupId>
          <artifactId>maven-jetty-plugin</artifactId>
          <version>6.1.26</version>
          <configuration>
            <contextPath>/${project.build.finalName}</contextPath>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.1.1</version>
      </plugin>
    </plugins>    
  </build>


* Create a Jetty launch configuration to run the webapp in Eclipse: Run > Run configurations. Maven Build | New. Name {JettyRun}, browse workspace and select current project, Goals {jetty:run}. [Apply] then [Run].

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.

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).

Saturday, May 26, 2007

Between Eclipse and NetBeans ...

I am using both IDEs (although Eclipse is really a 'platform') and it's hard to decide which one is better for my development work. In my experience, more people use Eclipse than use NetBeans. Most projects I've worked on lately have used an Eclipse base. The beauty of Eclipse is in how 'pluggable' and extensible it is. There's just a lot of plugins available for Eclipse - and very useful ones at that! People I've talked to say it's easier to develop plugins for Eclipse than for NetBeans.
The extensibility of Eclipse though may cause first-time users to find it tedious. The basic distribution of the platform does basic things. If you want to do web development, for example, you'd need to separately download JEM, EMF, and other dependencies to get it to work. Because of how detached this process is, the folks at Eclipse also have a version of Eclipse (WTP) that comes with all dependencies preconfigured.
In NetBeans, on the other hand, you only do one download of their web pack, and you got all you need. The preview version (6.0-M) is slow to start (up to 2 minutes starting up??), but works great. All the tools you need are well integrated in the base version. I'd say NetBeans is more straight-forward and user-friendly than Eclipse. A lot of useful features have been added to NetBeans IDE, though others have been removed (and why?). For example, in 5.5, you can refactor and choose access modifiers for the class attributes on one screen. In 6.o, you only do refactoring with no options to change access modifiers. So you must manually add "private" where necessary. Little inconvenience.
The other drawback to NetBeans is that it only supports SJSAS for Java EE development. Otherwise, you can also have JBoss and Tomcat for J2EE 1.4. Eclipse offers a wider range of options for servers, if you specify .server properties correctly.
It'd appear that Eclipse is more advanced, but I think NetBeans has gotten better too. The stigma that NetBeans is a Sun tool is slowly eroding, paving the way for wider acceptance. I'd recommend that a developer be able to use both IDEs - although you can essentially do the same tasks with either.
My strategy: I use NetBeans to do samples and prototypes when I work on an Eclipse-based project - and then move the ideas to Eclipse for production, and vice versa. Keeps me plugged in on both IDEs.