Java 如何使用 maven 在 WAR、WEB-INF/lib 目录中包含特定的 jar

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

How to Include specific jars in WAR, WEB-INF/lib directory with maven

javamavenmaven-plugin

提问by naamadheya

This is my pom.xml, trying to create a WAR file with specific 3 libraries in WEB-INF/lib directory.

这是我的 pom.xml,试图在 WEB-INF/lib 目录中创建一个包含特定 3 个库的 WAR 文件。

I include them in <packagingIncludes>tag and the they are packaged in lib dir, but all .classfiles are ignored.

我将它们包含在<packagingIncludes>标签中,并将它们打包在 lib 目录中,但所有.class文件都被忽略。

I cannot use <packagingExcludes>because the dependent project has many 3rd party jars and out of my control.

我无法使用,<packagingExcludes>因为依赖项目有许多 3rd 方 jar 并且超出了我的控制。

what is wrong here or is there a way i can ignore all jars except 3 specific jars?

这里有什么问题,或者有什么方法可以忽略除 3 个特定罐子之外的所有罐子?

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>1.0</modelVersion>
<parent>
    <groupId>frmId</groupId>
    <artifactId>frm</artifactId>
    <version>1.5.9</version>
</parent>
<artifactId>frmw</artifactId>
<packaging>war</packaging>
<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <warSourceDirectory>src/main/webapp</warSourceDirectory>
                <packagingIncludes>WEB-INF/lib/frm-fl*.jar,WEB-INF/lib/frm-bean*.jar,WEB-INF/lib/frm-facade-${ffm.fpi.version}.jar</packagingIncludes>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.fortify.ps.maven.plugin</groupId>
            <artifactId>sca-maven-plugin</artifactId>
             <version>${maven-sca-plugin.version}</version>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.fi.ps</groupId>
        <artifactId>frmc-fl</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

采纳答案by fabballe

You just have to put your 3 specific libraries as dependencies of the project and maven will put it on WEB-INF/lib during package phase

您只需要将 3 个特定库作为项目的依赖项,maven 将在打包阶段将其放在 WEB-INF/lib 上

Update:

更新:

To exclude JAR comming from dependencies you have to use this kind of syntax

要排除来自依赖项的 JAR,您必须使用这种语法

<dependency>
  <groupId>sample.ProjectA</groupId>
  <artifactId>Project-A</artifactId>
  <version>1.0</version>
  <exclusions>
    <exclusion>  <!-- declare the exclusion here -->
      <groupId>sample.ProjectB</groupId>
      <artifactId>Project-B</artifactId>
    </exclusion>
  </exclusions> 
</dependency>

And to exclude all sub dependencies (maven 3 only):

并排除所有子依赖项(仅限 Maven 3):

<exclusions>
    <exclusion>
        <groupId>*</groupId>
        <artifactId>*</artifactId>
    </exclusion>
</exclusions>

Update 2

更新 2

with maven-war-plugin you should add WEB-INF/classes/** to packagingIncludes

使用 maven-war-plugin,您应该将 WEB-INF/classes/** 添加到 PackagingIncludes

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <warSourceDirectory>src/main/webapp</warSourceDirectory>
        <packagingIncludes>WEB-INF/lib/frm-fl*.jar,WEB-INF/lib/frm-bean*.jar,WEB-INF/lib/frm-facade-${ffm.fpi.version}.jar,WEB-INF/classes/**></packagingIncludes>
         <failOnMissingWebXml>false</failOnMissingWebXml>
     </configuration>
</plugin>