john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

maven multi module project

PREREQUISITE:  maven3, java6, eclipse juno

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mvn archetype:create -DgroupId=net.kittyandbear -DartifactId=multimodule-example-parent -Dversion=0.1

rm -rf multimodule-example-parent/src/

# ensure the pom is correct
nano multimodule-example-parent/pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>net.kittyandbear</groupId>
  <artifactId>multimodule-example-parent</artifactId>
  <version>0.1</version>
  <packaging>pom</packaging>

  <name>multimodule-example-parent</name>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <java.vm.version>1.6</java.vm.version>
    <maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>
    <junit.version>4.10</junit.version>
  </properties>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

  <build>
    <plugins>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <source>${java.vm.version}</source>
                <target>${java.vm.version}</target>
            </configuration>
        </plugin>

    </plugins>

  </build>

</project>



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CREATE THE MODULES (in a tree hierarchy)

cd multimodule-example-parent

mvn archetype:create -DgroupId=net.kittyandbear -DartifactId=simple-weather -Dversion=0.1

MAVEN WILL UPDATE THE PARENT POM FOR YOU! (parent's pom.xml now contains)

        <modules>
                <module>simple-weather</module>
        </modules>


nano multimodule-example-parent/simple-weather/pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>net.kittyandbear</groupId>
    <artifactId>multimodule-example-parent</artifactId>
    <version>0.1</version>
  </parent>

  <groupId>net.kittyandbear</groupId>
  <artifactId>simple-weather</artifactId>
  <version>0.1</version>
  <name>simple-weather</name>

</project>


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BUILD THE PROJECT

cd multimodule-example-parent
mvn install

# if using eclipse

mvn eclipse:eclipse

Eclipse -> File -> New Project -> (other) Maven -> Import Existing (browse to the multimodule-example-parent)


IF YOU RECEIVE THE ERROR:

An internal error occurred during: "Updating Maven Project".
Unsupported IClasspathEntry kind=4

THIS IS BECAUSE pom.xml CONTAINS "var"

 <classpathentry kind="var" path="M2_REPO/junit/junit/4.10/junit-4.10.jar"/>

STRANGELY IF YOU RIGHT CLICK ON PROJECT simple-weather AND CHOOSE MAVEN UPDATE enough times the "error" goes away

THE RESOLUTION:
Right click on the Project (sub module) -> Maven -> Disable Maven Nature
mvn eclipse:clean       //removes the maven created eclipse project settings
Right click on the Project (sub module) -> Configure -> Convert to Maven Project



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
WRITE THE CODE

package net.kittyandbear;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class SimpleWeatherCLI
{
    private int connectionTimeoutInMilliseconds = 5000;
    private int readTimeoutInMilliseconds = 5000;
    private String response;

    SimpleWeatherCLI() throws IOException
    {
        // connect to the remote service with a valid API key
        String apiKey = "EXAMPLEKEYed412f750cd";
        String address = "http://api.wunderground.com/api/" + apiKey + "/conditions/q/CA/San_Francisco.json";
        URL url = new URL( address );
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setConnectTimeout( connectionTimeoutInMilliseconds );
        urlConnection.setReadTimeout( readTimeoutInMilliseconds );
        urlConnection.connect();

        // read the data
        StringBuilder strb = new StringBuilder();
        BufferedReader br = null;
        try
        {
            br = new BufferedReader( new InputStreamReader( urlConnection.getInputStream() ) );
            String line;
            while( ( line = br.readLine() ) != null )
            {
                strb.append( line + "\n" );
            }
        }catch( IOException e )
        {

        }finally
        {
            if( br != null )
            {
                br.close();
            }
        }
        this.response = strb.toString();
    }

    public static void main( String[] args ) throws IOException
    {
        System.out.println( "Connecting..." );
        SimpleWeatherCLI main = new SimpleWeatherCLI();
        System.out.println( main.response );
    }

} // end class


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FROM THE PARENT BUILD/INSTALL

mvn clean package install

NOTE: if you want a self running jar you'll need to add quite a bit...

  • « OxygenSpaceCLI pom.xml
  • django helloworld hi »

Published

Nov 29, 2012

Category

java

~357 words

Tags

  • java 252
  • maven 10
  • module 9
  • multi 8
  • project 4