0

我已经使用 maven-bundle-plugin 将一些非 OSGi maven jar 重新打包为 bundle。重新打包基于该捆绑包的嵌入依赖功能,但直到今天我还没有找到一种方法来包含该捆绑包的源。有没有办法获取所有嵌入的依赖 jar 的源 jar(聚合)?

显然,只有在 Maven 中心提供源 jar 时。

编辑感谢@balazsz 的钩子。我的最终解决方案来自此回复中的建议

<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 文件

<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>
4

1 回答 1

0

您始终可以通过指定其分类器将依赖项的源添加到项目中。例如:

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

由于源 jar 没有 OSGi Manifest 条目,因此 maven-bundle-plugin 不会对里面的内容感兴趣。由于它是您项目的依赖项,因此您可以使用 Embed-Dependency 指令将源工件嵌入到生成的包中。例如:

<configuration>
  <instructions>
    <Embed-Dependency>*;classifier=sources</Embed-Dependency>
  </instructions>
</configuration>
于 2016-06-15T20:40:35.117 回答