Developers, Developers, Developers! Maksim Sorokin IT Blog

20Jul/11Off

Maven + Apache Felix: Best Practices

This is another post in Maven + Apache Felix + CXF + DOSGi series. As was shown in Maven + Apache Felix: Easy Development and Debugging With Eclipse post, it is easy to develop projects with Maven and Apache Felix. But how about really big projects. How to maintain configuration and easily manage the bundles?

The answer is simple -- use the power of Maven. If you have really big projects, try to split them into different parents and modules. It is import during this splitting part to preserve some sort of naming convention. For example, each child preserves parent name as a prefix. In this case it is easier to identify and operate the projects. The structure may look like this, assuming that there is a generator project:

-- generator
 |-- generator.parser
   |-- generator.parser.full
     |-- generator.parser.full.noboms
     |-- generator.parser.full.universal
   |-- generator.parser.simple
   |-- generator.parser.utils
 |-- generator.security
   |-- generator.security.basic
   |-- generator.security.ldap
 |-- generator.utility

Also, do not forget that you can exploit pluginManagement and dependencyManagement in Maven. Using it you can predefine build process routine in your parent (typically the top one) and not care about it in your parents. Example would be something like:

<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>dk.sorokin.maksim</groupId>
  <artifactId>generator</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>Generator Parent</name>

  <modules>
    <module>generator.parser</module>
    <module>generator.security</module>
    <module>generator.utility</module>
  </modules>

  <properties>
    <org.apache.felix.framework.version>3.2.2</org.apache.felix.framework.version>
    <org.apache.servicemix.specs.jsr311-api-1.0>1.8.0</org.apache.servicemix.specs.jsr311-api-1.0>
    <org.osgi.core.version>1.4.0</org.osgi.core.version>
  </properties>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <configuration>
          <archive>
            <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
          </archive>
        </configuration>
      </plugin>
    </plugins>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.felix</groupId>
          <artifactId>maven-bundle-plugin</artifactId>
          <version>2.3.5</version>
          <executions>
            <execution>
              <id>generate-resources</id>
              <goals>
                <goal>manifest</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
      </dependency>

      <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.framework</artifactId>
        <version>${org.apache.felix.framework.version}</version>
      </dependency>

      <dependency>
        <groupId>org.apache.servicemix.specs</groupId>
        <artifactId>org.apache.servicemix.specs.jsr311-api-1.0</artifactId>
        <version>${org.apache.servicemix.specs.jsr311-api-1.0}</version>
      </dependency>

      <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.osgi.core</artifactId>
        <version>${org.osgi.core.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

Also, carefully maintain Felix config.properties file. Especially, if you share it with several developers. In order not to have hardcoded path in it, you may use Maven Filtering to filtering it. Instead of paths, you have variables, which are stored in Maven Settings.

Finally, always make sure, that the whole project and Felix launcher (if you use it) can be checked, built and run immediately on the fresh machine.

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.