Friday, 18 September 2009

Qi4j - Project setup with Maven and Eclipse

Qi4j LogoQi4j is an implementation of composite oriented programming paradigm written in pure Java. As I'm just taking my first steps with Qi4j I've decided to write down some notes about that. In this post I'll show you how to setup your first Qi4j project with maven and eclipse. So, let's go. Step 1. Create project skeleton. With maven's archetype plugin we can get the skeleton really quickly.
mvn archetype:generate
Use the default project archetype (number 15) Maven asks for the groupId and artifactId.
Define value for groupId: : com.blogspot.javasnippet.qi4j Define value for artifactId: : qi4j-example
The skeleton has been created with the following pom.xml file
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.blogspot.javasnippet.qi4j</groupId>
    <artifactId>qi4j-example</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>qi4j-example</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
Step 2. Add Qi4j maven repository. To download Qi4j artifacts we must configure appropriate repository in the pom.xml. To do that add the following section to your pom.xml file
<repositories>
    <repository>
       <id>ops4j-repository</id>
       <url>http://repository.ops4j.org/maven2</url>
    </repository>
</repositories>
Step 3. Add Qi4j dependecies. Qi4j is not a single heavyweight jar but rather a set of small libraries. Then we can take only these we really need.
<dependencies>
    <dependency>
        <groupId>org.qi4j.core</groupId>
        <artifactId>qi4j-core-api</artifactId>
        <version>${version.qi4j-core}</version>
    </dependency>
    <dependency>
        <groupId>org.qi4j.core</groupId>
        <artifactId>qi4j-core-spi</artifactId>
        <version>${version.qi4j-core}</version>
    </dependency>
    <dependency>
        <groupId>org.qi4j.core</groupId>
        <artifactId>qi4j-core-bootstrap</artifactId>
        <version>${version.qi4j-core}</version>
    </dependency>
    <dependency>
        <groupId>org.qi4j.core</groupId>
        <artifactId>qi4j-core-runtime</artifactId>
        <version>${version.qi4j-core}</version>
        <scope>runtime</scope>
    </dependency>
    ...
As you can see the Qi4j version number is set as a property so we must define it (when I'm writing this the latest version is 1.0-RC1).
<properties>
    <version.qi4j-core>1.0-RC1</version.qi4j-core>
</properties>
Busy people can find final pom.xml below.
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.blogspot.javasnippet.qi4j</groupId>
    <artifactId>qi4j-example</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>qi4j-example</name>
    <url>http://maven.apache.org</url>

    <properties>
        <version.qi4j-core>1.0-RC1</version.qi4j-core>
    </properties>

    <repositories>
        <repository>
            <id>ops4j-repository</id>
            <url>http://repository.ops4j.org/maven2</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.qi4j.core</groupId>
            <artifactId>qi4j-core-api</artifactId>
            <version>${version.qi4j-core}</version>
        </dependency>
        <dependency>
            <groupId>org.qi4j.core</groupId>
            <artifactId>qi4j-core-spi</artifactId>
            <version>${version.qi4j-core}</version>
        </dependency>
        <dependency>
            <groupId>org.qi4j.core</groupId>
            <artifactId>qi4j-core-bootstrap</artifactId>
            <version>${version.qi4j-core}</version>
        </dependency>
        <dependency>
            <groupId>org.qi4j.core</groupId>
            <artifactId>qi4j-core-runtime</artifactId>
            <version>${version.qi4j-core}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
Step 4. Build the application. Now it's time to download Qi4j dependencies and build the application for the first time.
mvn install
Step 5. Import project to the eclipse workspace. As Eclipse is my favorite IDE I call maven to generate eclipse project files.
mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs
Start Eclipse and import your fresh project into your workspace. Now you can remove generated files App.java and AppTest.java That's it. In the next post I'll show you how to start up your first Qi4j application.

No comments: