【问题标题】:Add an external jar as a dependency to maven POM for compiling, but exclude it while creating a fat jar将外部 jar 作为依赖项添加到 maven POM 进行编译,但在创建胖 jar 时将其排除
【发布时间】:2021-10-13 01:29:09
【问题描述】:

在运行 mvn install 时,我需要包含一个外部 jar(仅用于编译)。但是我也在创建一个胖/超级罐,因此,外部依赖被添加到胖罐中,我不想在胖罐中添加外部罐。 请提出出路?

我将外部依赖添加为:

<dependency>
            <groupId>a.b.c</groupId>
            <artifactId>xyz</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/library.jar</systemPath>
</dependency>

和创造脂肪:

<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>assemble-all</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
</plugins>

但是fat jar中不需要外部jar(library.jar)。

如何将它从 fat jar 中排除,但在执行 mvn install 时仍保留它? 请注意,在本地 repo 中添加 jar 对我来说不是一个选项,因为我是从 jenkins 进行此构建的。

【问题讨论】:

  • 如何创建一个胖罐子?你用哪个插件?你能从你的 pom.xml 中提供一个相关的 sn-p 吗?
  • 也许this 会回答?
  • @MarkBramnik 我添加了更多细节。
  • @g00se 我添加了更多细节
  • 范围:提供的是正确的。除了使事情变得比需要的更复杂之外,不推荐使用系统范围依赖性。最佳做法是将此类 jar 文件放入存储库管理器中,特别是如果您是从 Jenkins 构建的...此外,问题是您为什么使用 maven-assembly-plugin 和 maven-shade-plugin ?

标签: java maven pom.xml


【解决方案1】:

看起来 maven-shade-plugin 生成的工件会覆盖 maven-assembly-plugin 的输出,除非缺少某些东西 从 sn-p 中,您应该能够删除 maven-assembly-plugin。

要从阴影 JAR 中过滤特定 JAR,请添加:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        ...
        <configuration>
          <artifactSet>
            <excludes>
              <exclude>a.b.c:xyz</exclude>
            </excludes>
          </artifactSet>
        </configuration>
        ...
      </plugin>

到您的 maven-shade-plugin 配置。

【讨论】:

  • 非常感谢@Allen,这有帮助。竖起大拇指。
猜你喜欢
  • 2019-09-03
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
相关资源
最近更新 更多