Maven how to aggregate source from project depedencies

I use maven-bundle-plugin to create osgi plugin from non osgi depedency and I want to include the source from this depedency into the projet build.

This is an example I create an OSGI bundle from jfreechart and when I publish it I want to include jfreechart sources.

<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>org.jfree.chart</groupId>
    <artifactId>com.netappsid.org.jfree.chart</artifactId>
    <version>1.0.13</version>
    <name>JFreeChart OSGI</name>
    <packaging>bundle</packaging>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Export-Package>org.jfree.chart.*;org.jfree.data.*</Export-Package>
                        <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
                        <Embed-Dependency>jfreechart;inline=true</Embed-Dependency>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.0.13</version>
                    <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>com.springsource.org.jfree</artifactId>
            <version>1.0.12</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>com.springsource.javax.servlet</artifactId>
            <version>2.5.0</version>
        </dependency>
    </dependencies>

</project>

Answers


I had the same issue. Here's what I ended up doing:

  1. unpack the sources using the maven-dependency-plugin

    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>unpack-sources</id>
          <goals>
            <goal>unpack</goal>
          </goals>
          <configuration>
            <outputDirectory>${project.build.directory}/sources</outputDirectory>
            <artifactItems>
              <artifactItem>
                <groupId>commons-httpclient</groupId>
                <artifactId>commons-httpclient</artifactId>
                <version>3.1</version>
                <classifier>sources</classifier>
              </artifactItem>
            </artifactItems>
          </configuration>
        </execution>
      </executions>
    </plugin>
    
  2. attach a sources artifact built with the maven-assembly-plugin

    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <executions>
        <execution>
          <id>source-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <descriptors>
              <descriptor>src.xml</descriptor>
            </descriptors>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    with the following descriptor (note that the descriptor id is used as the classifier ; the artifact is attached by default):

    <?xml version="1.0"?>
    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
      <id>sources</id>
      <includeBaseDirectory>false</includeBaseDirectory>
      <formats>
        <format>jar</format>
      </formats>
      <fileSets>
        <fileSet>
          <directory>${project.build.directory}/sources</directory>
          <outputDirectory>/</outputDirectory>
          <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
      </fileSets>
    </assembly>
    

If I understand you correctly...

I've had to package many osgi-less JARs myself for use in an OSGi application. When using the maven-bundle-plugin, if you are using Export-Package in the manifest (or osgi.bnd file) then its classes will be included in the created bundle.

Example:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: ACME PostgreSQL Driver Bundle
Bundle-SymbolicName: com.acme.org.postgresql
Bundle-Version: 9.0.801.jdbc4

# NB: I've imported a little too much for what is required.
Import-Package: org.postgresql*;version="9.0-801.jdbc4", \
    javax*, \
    org.w3c.dom, \
    !org.ietf.jgss, \
    !org.dom.xml.views

Export-Package: org.postgresql*;version="9.0-801.jdbc4"

Private-Package: org.w3c.dom*, org.xml*, javax*

Here, the exported packages will be included in the JAR from my Maven dependencies in the POM.

If you also want to include the dependency JAR, then you can use Embed-Dependency:

Embed-Dependency: org.postgresql*;version="9.0-801.jdbc4"
Embed-Transitive: true

If this what you were looking for?

Tony


Need Your Help