Showing posts with label Maven. Show all posts
Showing posts with label Maven. 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.