Maven 如何从项目依赖性中聚合源代码

发布于 2024-12-14 06:35:42 字数 2535 浏览 1 评论 0原文

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

这是我从 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的呼吸 2024-12-21 06:35:42

我有同样的问题。这就是我最终所做的:

  1. 使用 maven-dependency-plugin 解压源代码

    <前><代码><插件>;
    maven-dependency-plugin;
    <处决>
    <执行>
    解包源
    <目标>
    <目标>解压

    <配置>
    ${project.build.directory}/sources
    <工件项目>
    <工件项目>
    commons-httpclient;
    commons-httpclient;
    <版本>3.1
    <分类器>来源




  2. 附加使用 maven-assemble-plugin 构建的源工件

    <前><代码><插件>;
    maven- assembly-plugin;
    <处决>
    <执行>
    源程序集
    <阶段>封装
    <目标>
    <目标>单个

    <配置>
    <描述符>
    <描述符>src.xml




    使用以下描述符(请注意,描述符 id 用作分类器;默认情况下会附加工件):

    
    <程序集 xmlns="http://maven.apache.org/plugins/maven-assemble-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">
      来源
      false;
      <格式>
        <格式>jar
      
      <文件集>
        <文件集>
          <目录>${project.build.directory}/sources
          <输出目录>/
          true
        
      
    
    

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>
    
梦罢 2024-12-21 06:35:42

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

我必须自己打包许多无 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

如果这就是您正在寻找的内容?

托尼

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文