【问题标题】:Maven how to aggregate source from project depedenciesMaven如何从项目依赖中聚合源代码
【发布时间】:2011-11-07 16:21:26
【问题描述】:

我使用 maven-bundle-plugin 从非 osgi 依赖创建 osgi 插件,我想将此依赖的源代码包含到 projet 构建中。

这是一个示例,我从 jfreechart 创建了一个 OSGI 包,当我发布它时,我想包含 jfreechart 源代码。

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

【问题讨论】:

  • 您使用的是 IDE 吗?当我针对这个 POM 运行 Maven 时,我得到了预期的 JAR 文件,其中包含“META-INF\...”和“org\jfree\...”。由于您也使用了“嵌入依赖”,我猜您还希望包含 jfreechart JAR,在这种情况下删除“;inline=true”语句,这将起作用。
  • 这个 pom 工作,但我想在发布时将带有 maven-source-plugin 的 jfreechart 源添加到 com.netappsid.org.jfree.chart-1.0.13-sources.jar 中
  • 对不起,我没有意识到您想要实际的源文件。我没有在任何 Maven 存储库上看到 jfreechart 源文件,但您可以直接从他们的网站下载它们并使用:&lt;scope&gt;system&lt;/scope&gt;&lt;systemPath&gt;${project.basedir}/lib/jfree-sources.jar&lt;/systemPath&gt; 以及 maven-source-plugin 的依赖项。我以前没有使用过它,所以不会有任何帮助。很抱歉造成误解。

标签: maven osgi


【解决方案1】:

我遇到了同样的问题。这是我最终做的:

  1. 使用 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. 附加使用 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>
    

    带有以下描述符(注意描述符id用作分类器;工件默认附加):

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

【讨论】:

    【解决方案2】:

    如果我理解正确的话......

    我不得不自己打包许多无 osgi 的 JAR,以便在 OSGi 应用程序中使用。使用maven-bundle-plugin 时,如果您在清单(或osgi.bnd 文件)中使用Export-Package,则其类将包含在创建的包中。

    例子:

    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*
    

    在这里,导出的包将包含在 POM 中我的 Maven 依赖项的 JAR 中。

    如果你还想包含依赖JAR,那么你可以使用Embed-Dependency:

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

    如果这是您要找的东西?

    托尼

    【讨论】:

    • 我已经使用了 maven-bundle-plugin,但是当我发布项目时,它没有将 指令的源代码包含到 myprojet-1.0.0-sources.jar 中
    • 您的所有其他字段是否都包含在您的发布包中,即 Bundle-SymbolicName?您能否将 POM 的这一部分添加到您的问题中,以便我们查看?
    猜你喜欢
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多