简体   繁体   English

如何使用Maven2将多个模块和第三方JAR捆绑在ZIP中?

[英]How do I bundle multiple modules and 3rd-party JARs in a ZIP using Maven2?

I'm developing my application as a set of OSGi bundles using Maven 2. Some common functionality, such as logging, web server etc. is used from other OSGi bundles, for example, I'm using the OSGi version of Jetty. 我正在使用Maven 2将我的应用程序开发为一组OSGi包。一些常见的功能,例如日志,Web服务器等,可以从其他OSGi包中使用,例如,我使用的是JetG的OSGi版本。

I want to ship my application with all third-party bundles and an OSGi container included. 我想发送我的应用程序包含所有第三方包和一个OSGi容器。 I chose Apache Felix as my default container. 我选择Apache Felix作为我的默认容器。

I'm quite new to Maven and I don't know how to write the POM file that does this and couldn't find a similar example in the Maven book . 我是Maven的新手,我不知道如何编写POM文件来做到这一点,并且在Maven书中找不到类似的例子。 The core point seems to be that a multi-module project doesn't create an artifact of its own. 核心观点似乎是多模块项目不会创建自己的工件。

It does build and package my bundles into OSGI-compatible JAR files as it should (using the maven-bundle-plugin ). 它确实构建了我的bundle并将其打包成OSGI兼容的JAR文件(使用maven-bundle-plugin )。 Now I want it to wrap up the other stuff as well (which doesn't need to be built or anything, just pulled in and put into the package) and produce a ZIP file like this: 现在我想要它包装其他东西(不需要构建或任何东西,只需拉入并放入包中)并生成这样的ZIP文件:

+-Archive Root
 |
 +- /bundles
  |
  +- my.bundle1.jar
  +- my.bundle2.jar
  +- 3rd.party.bundle1.jar
  +- 3rd.party.bundle2.jar
 +- /conf
  |
  +- ... some config files ...
 +- felix.jar

That way, my user can download the ZIP file, unpack it to a directory, say "MyApp", and then go 这样,我的用户可以下载ZIP文件,将其解压缩到目录,说“MyApp”,然后去

# > java -jar /path/to/MyApp/felix.jar

Some notes on details, if they matter: 关于细节的一些注释,如果重要的话:

  • Each bundle is a child of a common parent project that has no own source code and a packaging set to "pom", as taken from the book's examples. 每个包都是一个共同父项目的子项,它没有自己的源代码,并且packaging设置为“pom”,如本书的示例所示。
  • I have decided against enbedding Felix into a central bundle ("hosted framework" approach) 我决定不反对将Felix嵌入到一个中心包中(“托管框架”方法)
  • I might offer an alternative shipping that just includes my own bundles for customers running OSGi containers. 我可能会为运行OSGi容器的客户提供另一种包含我自己的捆绑包的运输。 This might be a starting point? 这可能是一个起点?

In the doc you reference, there is a section titled Embedding dependencies that describes how the plugin resolves the Maven project dependencies and add them to the classpath and resources. 在您引用的文档中,有一个标题为嵌入依赖项的部分描述了插件如何解析Maven项目依赖项并将它们添加到类路径和资源中。

The plugin uses the instruction to transform the project dependencies into and clauses, which are then appended to the current set of instructions and passed onto BND. 该插件使用该指令将项目依赖项转换为和子句,然后将其附加到当前指令集并传递给BND。 If you want the embedded dependencies to be at the start or middle of or then you can use {maven-dependencies}, which will automatically expand to the relevant clauses. 如果您希望嵌入式依赖项位于开头或中间,或者您可以使用{maven-dependencies},它将自动扩展到相关子句。

... ...

The plugin uses the instruction to transform the project dependencies into and clauses, which are then appended to the current set of instructions and passed onto BND. 该插件使用该指令将项目依赖项转换为和子句,然后将其附加到当前指令集并传递给BND。 If you want the embedded dependencies to be at the start or middle of or then you can use {maven-dependencies}, which will automatically expand to the relevant clauses. 如果您希望嵌入式依赖项位于开头或中间,或者您可以使用{maven-dependencies},它将自动扩展到相关子句。

In the example below the {maven-dependencies} placeholder will be expanded to include the project dependencies that are scope runtime or compile into the Include-Resource and Bundle-Classpath elements. 在下面的示例中,将扩展{maven-dependencies}占位符以包括作为作用域运行时的项目依赖项,或者编译为Include-Resource和Bundle-Classpath元素。

<!-- embed all compile and runtime scope dependencies -->
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>

<Include-Resource>
  {maven-resources},{maven-dependencies}
</Include-Resource>

<Bundle-ClassPath>.,{maven-dependencies},some.jar</Bundle-ClassPath>

Normally the plugin only checks direct dependencies, but this can be changed to include the complete set of transitive dependencies with the following option: 通常,插件仅检查直接依赖项,但可以使用以下选项更改为包含完整的传递依赖项集:

<Embed-Transitive>true</Embed-Transitive>

If you want a dependency inlined instead of embedded add the inline=true. 如果您想要内联依赖项而不是嵌入式,请添加inline = true。 For example to inline all compile and runtime scoped dependencies use: 例如,内联所有编译和运行时范围的依赖项使用:

<Embed-Dependency>*;scope=compile|runtime;inline=true</Embed-Dependency>

It took me a while (about half a day of doc reading) to realize that the rather simple answer I was looking for was: 我花了一段时间(大约半天的文档阅读)才意识到我正在寻找的相当简单的答案是:

Use Maven Assemblies . 使用Maven程序集

They actually let you put together all kinds of archives from your project's artifacts and dependencies. 它们实际上允许您将项目的工件和依赖项中的各种存档组合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM