简体   繁体   English

从打包的war文件中排除本地WEB-INF / lib jar

[英]Exclude local WEB-INF/lib jars from packaged war file

I have mentioned the dependency for all the required JAR's in the pom.xml. 我已经在pom.xml中提到了所有必需JAR的依赖关系。 Now, Since the dependencies are defined in POM , the JAR's will be automatically packed in the lib folder. 现在,由于依赖项是在POM中定义的,因此JAR文件将自动打包在lib文件夹中。

I want to exclude all the JAR's in the lib,present in the project source. 我想排除项目源中存在的lib中的所有JAR。 The Dependency JAR's should be packaged inside the lib while building the WAR 在构建WAR时,应将Dependency JAR打包在lib中

I am using packaging excludes tag , To excludes the local jars. 我正在使用包装excludes标签 ,以排除本地jar。 But it excludes all the jars in the packaged war file. 但是它不包括打包的war文件中的所有jar。 I want to include only the maven dependencies. 我只想包含Maven依赖项。

                    <plugin>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.4</version>
                        <configuration>
                            <warSourceDirectory>WebContent</warSourceDirectory>
                            <warName>${war.name}</warName>
                            <webResources>
                                <resource>
                                    <directory>config</directory>
                                    <targetPath>WEB-INF/classes</targetPath>
                                    <directory>${maven.lib.dir}</directory>
                                    <targetPath>WEB-INF/lib</targetPath>                                
                                </resource>                                                                             
                            </webResources>
                            <archive>
                                <manifestEntries>
                                    <Built-Date>${maven.build.timestamp}</Built-Date>
                                </manifestEntries>
                            </archive>
                            <packagingExcludes>META-INF/*.xml</packagingExcludes>                           
                            <packagingExcludes>WEB-INF/*lib/</packagingExcludes>
                        </configuration>
                    </plugin>       

I can exclude few jars by specifying the names in the excludes tag. 我可以通过在excludes标签中指定名称来排除几个jar。 But i have too many jars to be explicitly written under the excludes tags 但是我有太多罐子无法在excludes标签下显式编写

<packagingExcludes>
        WEB-INF/lib/commons-logging-*.jar,
        %regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]
      </packagingExcludes>

I need some configuration, where in i can exclude the complete folder . 我需要一些配置,在其中我可以排除整个文件夹

I finally (after few edits) understand your problem. 我终于(经过几次编辑)终于明白了您的问题。 Unfortunately I think it would be extremely hard to do without something you would call a hack. 不幸的是,我认为如果没有所谓的hack很难做到。 This is hard because maven do not really support manually added jars. 这很困难,因为maven并不真正支持手动添加的jar。 It's considered bad practice but we try do something about it. 这被认为是不好的做法,但我们会尝试对此做一些事情。 Here is what you might do: 您可以执行以下操作:

  1. Copy maven dependencies to some temp directory 将Maven依赖项复制到一些临时目录
  2. Insert sub-directory into WEB-INF/lib 将子目录插入WEB-INF/lib
  3. Remove every *.jar from WEB-INF/lib , but not from sub-directory. WEB-INF/lib删除每个*.jar ,但不要从子目录中删除。

Here are more detailed steps: 以下是更详细的步骤:

First add copy-dependencies to some temp directory (in my case it is maven-deps) 首先将复制依赖项添加到一些临时目录中(在我的情况下是maven-deps)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/maven-deps</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>

Take in mind that in Eclipse IDE you get error in line with opening <execution> , but from tool-tip menu you can solve it by installing m2e connector from market (This plugin is not covered by default life-cycle. That's why you need connector). 请记住,在Eclipse IDE中,打开<execution>会出现错误,但是从工具提示菜单中,您可以通过从市场上安装m2e连接器来解决此问题(默认生命周期未涵盖此插件。因此,您需要连接器)。

After that we need tell maven to put this directory into war under WEB-INF/lib . 之后,我们需要告诉行家到这个目录下投入war下的WEB-INF/lib Do the following: 请执行下列操作:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>

        <webResources>
            <resource>
                <directory>target/maven-deps</directory>
                <includes>
                    <include>*.jar</include>
                </includes>
                <targetPath>WEB-INF/lib/maven-deps</targetPath>
            </resource>
        </webResources>
    </configuration>
</plugin>

This will insert directory maven-deps as sub-directory into WEB-INF/lib and exclude every *.jar from WEB-INF/lib but NOT from WEB-INF/lib/maven-deps 这会将目录maven-deps作为子目录插入WEB-INF/lib并从WEB-INF/lib排除每个*.jar ,但WEB-INF/lib/maven-deps排除

That way you will get only maven managed dependencies in WEB-INF/lib/maven-deps and this according to spec should be visible from war class loader, because class loader should load all jars from WEB-INF/lib and it's sub-directories. 这样,您将只在WEB-INF/lib/maven-deps获得maven管理的依赖项,并且根据规范,这应该在war类加载器中可见,因为类加载器应从WEB-INF/lib及其子目录加载所有jar。 。

I know this is kind of hack but at the moment I can't think any better solution to this. 我知道这是一种破解,但是目前我无法想到更好的解决方案。

I got a work around for this. 我为此进行了工作。 I am renaming the lib folder to ant-libs and then excluding it from packaging. 我将lib文件夹重命名为ant-libs,然后将其从包装中排除。 Once my war is created i am renaming ant-libs folder back to lib. 创建好战争之后,我将ant-libs文件夹重命名为lib。

Maven coderplus plugin: Maven coderplus插件:

              <plugin>
                <groupId>com.coderplus.maven.plugins</groupId>
                <artifactId>copy-rename-maven-plugin</artifactId>
                <version>1.0.1</version>
                <executions>
                  <execution>
                    <id>rename-file</id>
                    <phase>compile</phase>
                    <goals>
                      <goal>rename</goal>
                    </goals>
                    <configuration>
                      <sourceFile>${basedir}/WebContent/WEB-INF/lib</sourceFile>
                      <destinationFile>${basedir}/WebContent/WEB-INF/ant-libs</destinationFile>
                    </configuration>
                  </execution>
                  <execution>
                    <id>rename-file-back</id>
                    <phase>package</phase>
                    <goals>
                      <goal>rename</goal>
                    </goals>
                    <configuration>
                      <sourceFile>${basedir}/WebContent/WEB-INF/ant-libs</sourceFile>
                      <destinationFile>${basedir}/WebContent/WEB-INF/lib</destinationFile>
                    </configuration>
                  </execution>
                </executions>
              </plugin>

Maven war plugin: Maven War插件:

                 <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <warSourceDirectory>WebContent</warSourceDirectory>
                        <warName>${war.name}</warName>
                        <webResources>
                            <resource>
                                <directory>config</directory>
                                <targetPath>WEB-INF/classes</targetPath>                                    
                            </resource>
                            <resource>
                            <directory>${maven.lib.dir}</directory>
                            <targetPath>WEB-INF/lib</targetPath>
                            </resource> 
                        </webResources>
                        <archive>
                            <manifestEntries>
                                <Built-Date>${maven.build.timestamp}</Built-Date>
                            </manifestEntries>
                        </archive>
                        <packagingExcludes>WEB-INF/*ant-libs/</packagingExcludes>
                    </configuration>
                </plugin>

Just set <scope>provided</scope> for the dependency and it will not be packaged into war. 只需将<scope>provided</scope>为依赖项,它将不会打包到war中。 See more about maven scopes 进一步了解Maven范围

If you need to exclude certain files from the WAR file, use packagingExcludes . 如果需要从WAR文件中排除某些文件,请使用packagingExcludes Files will not be included to the final war but still be present in webappDirectory (the directory where the webapp is built). 文件不会包含在最终战争中,但仍会存在于webappDirectory (构建webapp的目录)中。

As stated in Maven WAR Plugin reference : Maven WAR插件参考中所述

Including and Excluding Files From the WAR 包含和排除WAR中的文件

It is possible to include or exclude certain files from the WAR file, by using the and configuration parameters. 使用和配置参数, 可以从WAR文件中包含或排除某些文件 They each take a comma-separated list of Ant file set patterns. 它们每个都以逗号分隔的Ant文件集模式列表。 You can use wildcards such as ** to indicate multiple directories and * to indicate an optional part of a file or directory name. 您可以使用通配符,例如**表示多个目录,并使用*表示文件或目录名称的可选部分。

Here is an example where we exclude all JAR files from WEB-INF/lib: 这是一个示例,其中我们从WEB-INF / lib中排除了所有JAR文件:

 <project> ... <build> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes> </configuration> </plugin> </plugins> </build> ... </project> 

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

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