0

I've repackage some non OSGi maven jars as bundle using the maven-bundle-plugin. Repackaging is based on the Embed-Dependency feature of that bundle, but until today I haven't found a way to include also sources of that bundles. Is there a way to obtain a source jar (aggregation) of the all emdedded dependency jars?

Obviously only if source jar is available on the maven central.

Edit Thank @balazsz for the hook. My final solution comes from suggestion in this response

<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>com.example</groupId>
    <artifactId>com.example.com.j256.simplejmx</artifactId>
    <version>1.12</version>
    <packaging>bundle</packaging>
    <name>SimpleJMX</name>
    <properties>
        <maven.compiler.target>1.6</maven.compiler.target>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.build.timestamp.format>yyyyMMddHHmm</maven.build.timestamp.format>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <bundle.version>${project.version}.${maven.build.timestamp}</bundle.version>
    </properties>
    <licenses>
        <license>
            <name>ISC</name>
            <url>https://www.isc.org/downloads/software-support-policy/isc-license/</url>
            <distribution>repo</distribution>
        </license>
    </licenses>
    <scm>
        <connection>scm:git:ssh://git@github.com/j256/simplejmx.git</connection>
        <developerConnection>scm:git:ssh://git@github.com/j256/simplejmx.git</developerConnection>
        <url>https://github.com/j256/simplejmx</url>
    </scm>
    <dependencies>
        <dependency>
            <groupId>com.j256.simplejmx</groupId>
            <artifactId>simplejmx</artifactId>
            <version>${project.version}</version>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <profiles>
        <profile>
            <id>disable-java8-doclint</id>
            <activation>
                <jdk>[1.8,)</jdk>
            </activation>
            <properties>
                <additionalparam>-Xdoclint:none</additionalparam>
            </properties>
        </profile>
    </profiles>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>3.0.1</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <!-- includes all packages listed in Private-Package and exports those 
                            listed in Export-Package -->
                        <Bundle-Version>${bundle.version}</Bundle-Version>
                        <Export-Package>com.j256.simplejmx.server;version=${project.version},
                            com.j256.simplejmx.common;version=${project.version},
                            com.j256.simplejmx.client;version=${project.version}
                        </Export-Package>
                        <Private-Package>!com.j256.simplejmx.spring.*,
                            !com.j256.simplejmx.web.*
                        </Private-Package>
                        <Import-Package>!com.j256.simplejmx.*,*</Import-Package>
                    </instructions>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>unpack-sources</id>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.j256.simplejmx</groupId>
                                    <artifactId>simplejmx</artifactId>
                                    <version>${project.version}</version>
                                    <classifier>sources</classifier>
                                    <outputDirectory>${project.build.directory}/sources</outputDirectory>
                                    <excludes>**/simplejmx/web/**,**/simplejmx/spring/**</excludes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.4</version>
                <executions>
                    <execution>
                        <id>javadoc</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sourcepath>${project.build.directory}/sources</sourcepath>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

src.xml file

<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>
Community
  • 1
  • 1
nfalco
  • 1
  • 3
  • Is it a problem for you if you need to list those dependencies that you want the sources for? I can show an example for that, but not if you want to get these jars automatically (and transitively). – Balazs Zsoldos Jun 13 '16 at 10:57
  • @balazs I edit the main question with the main pom.xml. I would that maven proces create also the source jar with sources of com.j256.simplejmx – nfalco Jun 15 '16 at 17:02
  • I think I misunderstood you. So you would like to have a project, and merge the sources of another project into yours. Would it be a problem if your project re-compiled the original project java files, too? I think I know another solution, where you use maven-dependency-plugin to unpack the other artifact and maven-buildhelper-plugin to add the unpacked sources to your project. All this in the generate-sources phase. After that, everything works as normal, you do not need to use the Embed-Dependency instruction. – Balazs Zsoldos Jun 16 '16 at 13:32
  • Maybe recompiling entail some license issues. In this way I ensure the third party library is untouched, only repackage it (as springsource, servicemix or orbit does) with a OSGi manifest. Sources are needed during a maven release so they are deployed to our internal artifacts repository for debug/developing purpose (the same for javadoc) – nfalco Jun 20 '16 at 19:05

1 Answers1

0

You can always add the sources of a dependency to the project by specifying its classifier. E.g.:

<dependency>
    <groupId>com.j256.simplejmx</groupId>
    <artifactId>simplejmx</artifactId>
    <version>${project.version}</version>
    <classifier>sources</classifier>
</dependency>

As the sources jar does not have OSGi Manifest entries, the maven-bundle-plugin will not be interested what's inside. As it is a dependency of your project, you can embed the sources artifact into the generated bundle with the Embed-Dependency instruction. E.g.:

<configuration>
  <instructions>
    <Embed-Dependency>*;classifier=sources</Embed-Dependency>
  </instructions>
</configuration>
Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31
  • My goal was produce a source bundle during the build so I Can release it. I will try to add the maven-source-plugin to publish sources on maven release. Thanks for the possibility – nfalco Jun 15 '16 at 23:25