在常规 Maven 构建中使用来自 Eclipse p2 存储库的依赖项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6682028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:36:29  来源:igfitidea点击:

Use dependencies from Eclipse p2 repository in a regular Maven build?

javaeclipsemavenmaven-3dependency-management

提问by otto.poellath

I'd like to use dependencies from a remote Eclipse p2 repository in a "regular" Maven 3 build (e.g. JAR or WAR packaging) - all without converting the p2 repo to a local Maven repo (which is what osgi-to-maven2 and m4e seem to do).

我想在“常规”Maven 3 构建(例如 JAR 或 WAR 打包)中使用来自远程 Eclipse p2 存储库的依赖项 - 所有这些都不需要将 p2 存储库转换为本地 Maven 存储库(这就是 osgi-to-maven2 和m4e 似乎可以)。

Ideally, I'd just use http://maven.eclipse.org/nexus/, but that doesn't (yet?) contain many bundles.

理想情况下,我只使用http://maven.eclipse.org/nexus/,但这并不(还?)包含许多包。

Using Maven's systemPath doesn't count!

使用 Maven 的 systemPath 不算数!

采纳答案by otto.poellath

Looks like I'm answering another of my own questions..

看起来我正在回答我自己的另一个问题..

First of all, you can use Tycho to add a dependency on a bundle from a P2 repository:

首先,您可以使用 Tycho 添加对来自 P2 存储库的包的依赖:

  • Configure the build to use tycho-maven-plugin.
  • Specify a P2 repository.
  • Set packaging to "eclipse-plugin".
  • Create a manifest for your build and use Require-Bundle to state the dependency (e.g. org.eclipse.xsd). Also set Bundle-Version to the same version we used for our build in pom.xml.
  • 配置构建以使用 tycho-maven-plugin。
  • 指定 P2 存储库。
  • 将包装设置为“eclipse-plugin”。
  • 为您的构建创建一个清单并使用 Require-Bundle 来声明依赖项(例如 org.eclipse.xsd)。还将 Bundle-Version 设置为我们在 pom.xml 中用于构建的相同版本。

Let's give that a try:

让我们试一试:

$ mvn dependency:tree
[INFO] com.example:org.eclipse.xsd:eclipse-plugin:0.0.1
[INFO] +- p2.eclipse-plugin:org.eclipse.xsd:jar:2.6.0.v20100914-1218:system
[INFO] ...
[INFO] \- p2.eclipse-plugin:org.eclipse.core.filesystem:jar:1.3.1.R36x_v20100727-0745:system

Our dependency has sucessfully been resolved from the P2 repository. Unfortunately, we're not done yet. The dependency has been added with system scope, which means that the artifacts won't be included if we create a webapp that depends on our build. To work around that, we'll first unpack all classes contained in the dependency to some directory, and then re-package that directory as a jar and use it as the final artifact of our build.

我们的依赖已经从 P2 存储库中成功解决。不幸的是,我们还没有完成。该依赖项已添加到系统范围内,这意味着如果我们创建一个依赖于我们的构建的 web 应用程序,则不会包含这些工件。为了解决这个问题,我们首先将依赖项中包含的所有类解压缩到某个目录,然后将该目录重新打包为 jar 并将其用作我们构建的最终工件。

For the first part (unpacking), we add the maven-dependency-plugin to our build and configure it to run its unpack-dependencies goal during the package phase. For the second part (re-packaging), we add the maven-assembly-plugin to our build and configure it to run its single goal during the package phase. We also need to create and configure a custom assembly descriptor.

对于第一部分(解包),我们将 maven-dependency-plugin 添加到我们的构建中,并将其配置为在打包阶段运行其解包依赖项目标。对于第二部分(重新打包),我们将 maven-assembly-plugin 添加到我们的构建中,并将其配置为在打包阶段运行其单一目标。我们还需要创建和配置自定义程序集描述符。

Our build now consists of 3 files: The build file in pom.xml:

我们的构建现在由 3 个文件组成: pom.xml 中的构建文件:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>org.eclipse.xsd</artifactId>
    <version>0.0.1</version>
    <packaging>eclipse-plugin</packaging>
    <repositories>
        <repository>
            <id>helios</id>
            <layout>p2</layout>
            <url>http://download.eclipse.org/releases/helios</url>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-maven-plugin</artifactId>
                <version>0.12.0</version>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/dependency</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/repackaged.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The manifest in META-INF/MANIFEST.MF:

META-INF/MANIFEST.MF 中的清单:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Ignored
Bundle-SymbolicName: Ignored
Bundle-Version: 0.0.1
Require-Bundle: org.eclipse.xsd
Bundle-Vendor: Ignored

The assembly descriptor in src/main/assembly/repackaged.xml:

src/main/assembly/repackaged.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>repackaged</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
      <fileSet>
        <directory>${project.build.directory}/dependency/</directory>
        <outputDirectory>/</outputDirectory>
        <useDefaultExcludes>true</useDefaultExcludes>
      </fileSet>
    </fileSets>
</assembly>

Now mvn package will create a jar file that contains all the code from our P2 dependency, re-packaged as a proper Maven artifact, ready to be used as a dependency in another project.

现在 mvn package 将创建一个 jar 文件,其中包含来自我们 P2 依赖项的所有代码,重新打包为适当的 Maven 工件,准备用作另一个项目中的依赖项。

$ jar tf target/org.eclipse.xsd-0.0.1-repackaged.jar 
org/eclipse/xsd/ecore/XSDEcoreBuilder.class
org/eclipse/xsd/ecore/XSDEcoreBuilder.class
org/eclipse/xsd/ecore/XSDEcoreBuilder$Comparator.class
org/eclipse/xsd/ecore/XSDEcoreBuilder$EffectiveOccurrence.class
org/eclipse/xsd/ecore/XSDEcoreBuilder.class

回答by Eiswind

Another way is to use b3 aggregator to mirror the p2 repo in a maven compliant layout as described here:

另一种方法是使用 b3 聚合器在 maven 兼容布局中镜像 p2 存储库,如下所述:

Consuming emf bundles from p2 with maven : b3

使用 maven 从 p2 消耗电动势包:b3

then one can use the original jars as provided.

然后可以使用提供的原始罐子。