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

时间:2022-08-15 15:20:05

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

9 个解决方案

#1


63  

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 here under "scope"

在“范围”下查看有关maven范围的更多信息

Edit If 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

在这里查看更多

#2


5  

I fixed it.

我修好了它。

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

我更加小心地重读了引用,我发现elementExcludes元素应该是warSourceExcludes。

#3


5  

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 ...

#4


2  

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.

我没有可能修改依赖war文件。我需要从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>
        ....

#5


2  

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具有本地依赖关系,而该关联依赖于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依赖项。

#6


1  

See my answer here. The packaging-excludes entry 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.

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

#7


1  

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

正确的解决方案是使用 配置,因为 提供的 解决方案是一个hack。

Consider the following multi-module project:

考虑以下多模块项目:

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

In this multi-module project, module A requires modules {B, C, D}, but not {e, f, g}. However, modules B and D do require {e, f g}, and C requires {g}.

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


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

首先,让我们尝试使用 提供的 方法来解决这个问题:

To exclude {e, f, g} from A, the <scope>provided</scope> spec must be present in D's POM. But wait, B requires {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 D must be been pulled into B. Similarly, since C depends on {g}, the dependency declarations for {g} must be present in C's POM.

要从A中排除{e,f,g}, 提供的 规范必须存在于D的POM中。但等等,B需要{e,f,g}。所以要解决这个问题,{e,f,g}的依赖声明也必须出现在B的POM中( 提供 )。这意味着必须将D中的依赖项规范复杂性拉入B.同样,由于C依赖于{g},因此{G}的依赖性声明必须存在于C的POM中。

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

使用 提供的 解决方案,模块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:

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

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

要从A中排除{e,f,g},必须在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, C and D from having to care about excluding transitive dependencies for the sake of A.

使用此解决方案,“必须从A中排除的内容”的复杂性仅包含在A的POM中。这使得模块B,C和D不必为了A而考虑排除传递依赖性。

This solution upholds the encapsulation principle.

该解决方案坚持封装原则。

#8


0  

When compiling with maven 3 (I used 3.0.2) with provided scope you 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包含所指定的内容(仅提供未依赖的内容)

#9


0  

You can do this by specifying 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 cards and regular expressions too. Refer the documentation for more info.

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

#1


63  

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 here under "scope"

在“范围”下查看有关maven范围的更多信息

Edit If 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

在这里查看更多

#2


5  

I fixed it.

我修好了它。

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

我更加小心地重读了引用,我发现elementExcludes元素应该是warSourceExcludes。

#3


5  

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 ...

#4


2  

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.

我没有可能修改依赖war文件。我需要从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>
        ....

#5


2  

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具有本地依赖关系,而该关联依赖于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依赖项。

#6


1  

See my answer here. The packaging-excludes entry 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.

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

#7


1  

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

正确的解决方案是使用 配置,因为 提供的 解决方案是一个hack。

Consider the following multi-module project:

考虑以下多模块项目:

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

In this multi-module project, module A requires modules {B, C, D}, but not {e, f, g}. However, modules B and D do require {e, f g}, and C requires {g}.

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


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

首先,让我们尝试使用 提供的 方法来解决这个问题:

To exclude {e, f, g} from A, the <scope>provided</scope> spec must be present in D's POM. But wait, B requires {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 D must be been pulled into B. Similarly, since C depends on {g}, the dependency declarations for {g} must be present in C's POM.

要从A中排除{e,f,g}, 提供的 规范必须存在于D的POM中。但等等,B需要{e,f,g}。所以要解决这个问题,{e,f,g}的依赖声明也必须出现在B的POM中( 提供 )。这意味着必须将D中的依赖项规范复杂性拉入B.同样,由于C依赖于{g},因此{G}的依赖性声明必须存在于C的POM中。

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

使用 提供的 解决方案,模块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:

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

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

要从A中排除{e,f,g},必须在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, C and D from having to care about excluding transitive dependencies for the sake of A.

使用此解决方案,“必须从A中排除的内容”的复杂性仅包含在A的POM中。这使得模块B,C和D不必为了A而考虑排除传递依赖性。

This solution upholds the encapsulation principle.

该解决方案坚持封装原则。

#8


0  

When compiling with maven 3 (I used 3.0.2) with provided scope you 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包含所指定的内容(仅提供未依赖的内容)

#9


0  

You can do this by specifying 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 cards and regular expressions too. Refer the documentation for more info.

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