Java 如何排除 maven war 插件生成的 jars?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1031695/
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-08-11 22:28:32  来源:igfitidea点击:

How to exclude jars generated by maven war plugin?

javamaven-2

提问by Jacques René Mesrine

Because of transitive dependencies, my wars are getting populated by xml-apis, xerces jars. I tried following the instructions on the reference page for maven-war-plugin but it is not working.

由于传递依赖关系,我的战争被 xml-apis、xerces jars 填充。我尝试按照 maven-war-plugin 参考页面上的说明进行操作,但它不起作用。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
      <packagingExcludes>WEB-INF/lib/xalan-2.6.0.jar,WEB-INF/lib/xercesImpl-2.6.2.jar,WEB-INF/lib/xml-apis-1.0.b2.jar,WEB-INF/lib/xmlParserAPIs-2.6.2.jar</packagingExcludes>
      <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
      <warName>project1</warName>
      <warSourceDirectory>src/main/webapp</warSourceDirectory>
    </configuration>
</plugin>

What am I doing wrong ? If it matters, I discovered that the maven-war-plugin I'm using is at version 2.1-alpha-1

我究竟做错了什么 ?如果重要的话,我发现我使用的 maven-war-plugin 版本是 2.1-alpha-1

采纳答案by David Rabinowitz

You can mark these dependencies as provided:

您可以将这些依赖项标记为已提供:

<dependency>
  <groupId>xerces</groupId>
  <artifactId>xerces</artifactId>
  <version>2.4.0</version>
  <scope>provided</scope>
</dependency>

This way the maven will add them to the compilation classpath, but will not package them. It is assumed they exist in your servlet container.

这样 maven 会将它们添加到编译类路径中,但不会打包它们。假设它们存在于您的 servlet 容器中。

See more about maven scopes hereunder "scope"

查看更多关于Maven范围在这里下的“范围”

EditIf you want to remove classes added via transitive dependencies you can exclude them from the dependency like this:

编辑如果你想删除通过传递依赖添加的类,你可以像这样从依赖中排除它们:

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
        <exclusions>
                <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                </exclusion>
        </exclusions>
</dependency>

(taken from this answer)

(取自这个答案

See more here

在这里查看更多

回答by Jacques René Mesrine

I fixed it.

我修好了它。

Rereading the reference with a bit more care, I discovered that the element packagingExcludesshould be warSourceExcludes.

仔细阅读参考资料,我发现元素PackagingExcludes应该是warSourceExcludes

回答by accreativos

About exclusion of transitive dependencies, i think don't work with transtive of transitive dependecies, and i think this could be the case.

关于排除传递依赖,我认为不适用于传递依赖的传递,我认为可能是这种情况。

For example, add dependency of hibernate-core -> dom4j -> xml-apis if add exclude of xml-apis near your hibernate-core, still add xml-apis...

例如,添加 hibernate-core -> dom4j -> xml-apis 的依赖,如果在你的 hibernate-core 附近添加排除 xml-apis,仍然添加 xml-apis ...

回答by SmithHCoder

I had no possibility to modify depending war file. I needed to avoid some old jar file from lib. This snippet of POM.xml configuration works well for me. I use Maven 2.2.

我无法修改依赖的战争文件。我需要避免使用 lib 中的一些旧 jar 文件。这个 POM.xml 配置片段对我来说效果很好。我使用 Maven 2.2。

<build>
    <plugins>
        <!-- put aside some unwanted jars from war...-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <overlays>
                    <overlay>
                        <groupId>com.company.app</groupId>
                        <artifactId>web_app</artifactId>
                        <excludes>
                            <exclude>WEB-INF/lib/json-lib-2.2.2-jdk13.jar</exclude>                                
                        </excludes>
                    </overlay>
                </overlays>
            </configuration>
        </plugin>
        ....

回答by greuze

When compiling with maven 3 (I used 3.0.2) with provided scopeyou have the problem with the transitive dependencies (you have a los of JARs inside the WAR). If you use older versions of maven (I used 2.2.1), the WAR contains what is spected (only dependencies not provided

使用提供的范围使用 maven 3(我使用 3.0.2)进行编译时,您会遇到传递依赖项的问题(您在 WAR 中丢失了 JAR)。如果您使用旧版本的 Maven(我使用的是 2.2.1),则 WAR 包含指定的内容(仅未提供依赖项)

回答by MattC

To add some clarification, you absolutely can exclude transitive dependencies. For example, if you have a local dependency on A, which has a dependency on M, which in turn has a dependency on X, then you can exclude X where you specify your A dependency.

要添加一些说明,您绝对可以排除传递依赖项。例如,如果您对 A 有本地依赖关系,而 A 对 M 有依赖关系,而 M 又对 X 有依赖关系,那么您可以在指定 A 依赖关系的地方排除 X。

For example, say you depend on 'myPackageA', which depends on Rampart, which depends on Xerces, then you can exclude Xerces in your myPackageA dependency:

例如,假设您依赖于 'myPackageA',后者依赖于 Rampart,后者依赖于 Xerces,那么您可以在 myPackageA 依赖项中排除 Xerces:

 <groupId>myPackageA.dependsOnRampart.whichDependsOnXerces</groupId>
 <artifactId>someArtifact</artifactId>
 <version>2.1</version>
 <exclusions>
   <exclusion>
     <groupId>org.apache.xerces</groupId>
     <artifactId>xmlParserAPIs</artifactId>
   </exclusion>
   <exclusion>
     <groupId>org.apache.xerces</groupId>
     <artifactId>xml-apis</artifactId>
   </exclusion>
   <exclusion>
     <groupId>org.apache.xerces</groupId>
     <artifactId>xercesImpl</artifactId>
   </exclusion>
   <exclusion>
     <groupId>org.apache.xerces</groupId>
     <artifactId>resolver</artifactId>
   </exclusion>
   <exclusion>
     <groupId>org.apache.xerces</groupId>
     <artifactId>serializer</artifactId>
   </exclusion>
 </exclusions>

To check your dependencies afterwards, run 'mvn dependency:tree' and hopefully you won't see any xerces dependencies.

之后要检查您的依赖项,请运行“mvn dependency:tree”,希望您不会看到任何 xerces 依赖项。

回答by wmorrison365

See my answer here. The packaging-excludesentry only works with maven-war-plugin version 2.1-alpha-1 and above. So, you may need to upgrade the version used or, you may prefer to use dependency-level injection as it's more precise exclusion of specific jars.

在这里看到我的答案。该packaging-excludes条目仅适用于 maven-war-plugin 版本2.1-alpha-1 及更高版本。因此,您可能需要升级使用的版本,或者您可能更喜欢使用依赖级别注入,因为它可以更精确地排除特定的 jar。

回答by Supun Wijerathne

You can do this by specifying inside <packagingExcludes></packagingExcludes>inside </configuration><configuration>.

您可以通过指定 inside <packagingExcludes></packagingExcludes>inside来做到这一点</configuration><configuration>

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <packagingExcludes>
            WEB-INF/lib/ex1-*.jar,
            WEB-INF/lib/ex2-logging-*.jar
          </packagingExcludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

You can specify by wild cardsand regular expressionstoo. Refer the documentationfor more info.

您也可以通过通配符正则表达式指定。有关更多信息,请参阅 文档

回答by Seva Safris

The correct solution is to use the <packagingExcludes>configuration, as the <scope>provided</scope>solution is a hack.

正确的解决方案是使用<packagingExcludes>配置,因为<scope>provided</scope>解决方案是 hack。

Consider the following multi-module project:

考虑以下多模块项目:

        A
      (war)
      /   \
     B     C
   (jar) (jar)
    /     /
   D     /
 (jar)  /
 / | \ /
e  f  g

In this multi-module project, module Arequires modules {B, C, D}, but not{e, f, g}. However, modules Band Ddorequire {e, fg}, and Crequires {g}.

在这个多模块项目中,moduleA需要模块 { B, C, D},但不需要{ e, f, g}。但是,模块BD确实需要 { efg} 和C需要 { g}。



First, let's try to solve this problem with the <scope>provided</scope>approach:

首先,让我们尝试用以下<scope>provided</scope>方法解决这个问题:

To exclude {e, f, g} from A, the <scope>provided</scope>spec must be present in D's POM. But wait, Brequires {e, f, g}. So to fix that, the dependency declarations for {e, f, g} must be present in B's POM as well (with <scope>provided</scope>). This means that the dependency spec complexity from Dmust be been pulled into B. Similarly, since Cdepends on {g}, the dependency declarations for {g} must be present in C's POM.

要从 中排除 { e, f, g} A<scope>provided</scope>规范必须存在于D的 POM 中。但是等等,B需要 { e, f, g}。所以为了解决这个问题, { e, f, g}的依赖声明也必须存在于B的 POM 中(with <scope>provided</scope>)。这意味着依赖规范的复杂性D必须被拉入B. 类似地,由于C依赖于 { g},因此{ } 的依赖声明g必须存在于C的 POM 中。

With the the <scope>provided</scope>solution, modules B, C, and Dmust all have knowledge of the fact that Acannot have {e, f, g}.

有了<scope>provided</scope>解决方案,模块B, C, 和D必须都知道A不能有 { e, f, g}的事实。

This approach breaks the encapsulation principle, and is not practical in multi-module projects that have complex dependency graphs.

这种方式打破了封装原则,在依赖图复杂的多模块项目中不实用。



Second, let's try to solve this problem with the <packagingExcludes>approach:

其次,让我们尝试用这种<packagingExcludes>方法解决这个问题:

To exclude {e, f, g} from A, one must provide the following in A's POM:

要从 中排除 { e, f, g} A,必须在A的 POM 中提供以下内容:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
      <packagingExcludes>a*.jar,b*.jar,c*.jar</packagingExcludes>
    </configuration>
</plugin>

With this solution, the complexity of "what must be excluded from A" is contained solely in A's POM. This frees modules B, Cand Dfrom having to care about excluding transitive dependencies for the sake of A.

使用此解决方案,“必须排除的内容”的复杂性A仅包含在A的 POM 中。这释放了模块BC并且D不必为了A.

This solution upholds the encapsulation principle.

本方案秉承封装原则。