【问题标题】:How to build a jar from a module of maven with dependencies from other module?如何从具有来自其他模块的依赖项的 maven 模块构建 jar?
【发布时间】:2016-12-21 20:53:35
【问题描述】:

我在 maven 中有一些模块,我想构建一个模块的 jar,该模块仅依赖于其他模块。

如何在 jar 中包含两个模块?

编辑:

我有以下模块:

  myproject-core/
    myproject-api/
    myproject-dependencie/
    myproject-api-web/

我想用 myproject-dependenci 在 myproject-api 中构建一个 jar。我在 myproject-api 中有更多的依赖项,我只需要在这个 jar 中拥有 myproject-api 和 myproject-dependencie。

谢谢!

【问题讨论】:

  • 不清楚你想在这里做什么,你能澄清一下你有什么,你想要什么吗?您能否发布一些示例 POM 以提供帮助?
  • 我编辑了更多细节,谢谢

标签: maven


【解决方案1】:

我猜 maven-shade-plugin 会成为你的朋友:

https://maven.apache.org/plugins/maven-shade-plugin/

在你的插件部分类似:

...

    <properties>
        <shade-main-class>{your-main-class}</shade-main-class>
    </properties>

...

...

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
             <includes>
                <include>com/yourcompany/**</include>
              </includes>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>${shade-main-class}</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
...

【讨论】:

  • 嗨 thelINtoy,使用这段代码,我想我会得到一个包含所有依赖项的 jar,但我不想要所有依赖项,我只想要其中一个。
  • 我想我可以使用 exclude 标签来排除其余的依赖项......但我很安静,我正在寻找其他方式......
  • 在您编辑问题后,我更新了答案以显示您可以使用包含过滤器
猜你喜欢
  • 2013-09-20
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2019-12-13
  • 2014-11-18
  • 2015-01-23
  • 2014-10-18
相关资源
最近更新 更多