简体   繁体   English

Maven构建的WAR中有条件地包含JAR

[英]Conditional inclusion of JARs in a WAR built by Maven

I have to build a war file using maven to include jars conditionally, each jar is created by a seperate maven project which deploys jar to nexus(our organisations remote) repository 我必须使用maven构建一个战争文件以有条件地包含jar,每个jar由一个单独的maven项目创建,该项目将jar部署到nexus(我们的组织远程)存储库

Eg : I have jars like these core.jar,reward.jar,payment.jar,domains.jar so on I need to build a final war based on conditions(environmnet) to include above jars 例如:我有像这些core.jar,reward.jar,payment.jar,domains.jar这样的jar,因此我需要根据条件(环境)建立最终的战争,以包括上述jar

Combination of final war(w1) w1.war : core.jar,domains.jar w1.war : core.jar,domains.jar,rewards.jar(Any way to specify to include this jar if rewards is applicable) 最终战争(w1)的组合w1.war:core.jar,domains.jar w1.war:core.jar,domains.jar,rewards.jar(如果奖励适用,可以指定包含此jar的任何方式)

The Maven WAR Plugin allows you to include/exclude JARs. Maven WAR插件允许您包括/排除JAR。 For example: 例如:

  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
      <packagingExcludes>
        WEB-INF/lib/excluded.jar
      </packagingExcludes>
      <packagingIncludes>
        WEB-INF/lib/included.jar
      </packagingIncludes>
    </configuration>
  </plugin> 

You can associate the inclusions/exclusions with a condition by using profiles. 您可以使用配置文件将包含/排除与条件相关联。 For example, let the WAR plugin use properties (${excludedResources}, ${includedResources}) ... 例如,让WAR插件使用属性($ {excludedResources},$ {includedResources})...

  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
      <packagingExcludes>
        ${excludedResources}
      </packagingExcludes>
      <packagingIncludes>
        ${includedResources}
      </packagingIncludes>
    </configuration>
  </plugin> 

... and define values for those properties via profiles: ...并通过配置文件为这些属性定义值:

  <profiles>
    <profile>
      <id>prod</id>
      <properties>
        <excludedResources>WEB-INF/lib/a.jar,WEB-INF/lib/b.jar</excludedResources>
        <includedResources>WEB-INF/lib/c.jar</includedResources>
      </properties>
    </profile>
    <profile>
      <id>tst</id>
      <properties>
        <excludedResources>WEB-INF/lib/x.jar,WEB-INF/lib/y.jar</excludedResources>
        <includedResources>WEB-INF/lib/z.jar</includedResources>
      </properties>
    </profile>
  </profiles>

So, you can use the Maven WAR Plugin's built-in ability to tweak the WAR contents and you can make these tweaks conditional by using Maven profiles. 因此,您可以使用Maven WAR插件的内置功能来调整WAR内容,并且可以使用Maven配置文件使这些调整成为条件。

You can try to use profiles capabilities in maven. 您可以尝试在Maven中使用profiles功能。 Each dependency can be included into its own profile block eg domains will be included only if you switch this profile on and services - by services profile. 每个依赖项都可以包含在其自己的配置文件块中,例如,仅当您打开此配置文件和服务时才包含domains -通过services配置文件。 At the same time you can identify common jars through the common dependency block (in our case core.jar will be common) 同时,您可以通过公共依赖项块识别公共jar(在我们的例子中, core.jar将是公共的)

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stackoverflow</groupId>
    <artifactId>conditional-war</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>War Which Includes Jar By Conditions</name>

    <!-- Common dependency block which will be always included -->
    <dependencies>
        <dependency>
            <groupId>com.stackoverflow</groupId>
            <artifactId>core</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

    <profiles>
        <!-- Profile for domains jars. Will be included by
             profile\condition "domains" -->
        <profile>
            <id>domains</id>
            <activation>
                <property>
                    <name>domains</name>
                </property>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>com.stackoverflow</groupId>
                    <artifactId>domains</artifactId>
                    <version>${project.version}</version>
                </dependency>
            </dependencies>
        </profile>

        <!-- Profile for domains jars. Will be included by
             profile\condition "services" -->
        <profile>
            <id>services</id>
            <activation>
                <property>
                    <name>services</name>
                </property>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>com.stackoverflow</groupId>
                    <artifactId>services</artifactId>
                    <version>${project.version}</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
</project>

Command line for the activation can be following: 用于激活的命令行如下:

  1. By this command line will be included core.jar and domains.jar 通过此命令行将包含core.jar和domains.jar

    mvn clean install -Pdomains

  2. In such case war will include core.jar and services.jar 在这种情况下,战争将包括core.jar和services.jar

    mvn clean install -Pservices

  3. And finally by this command line will be included all the jars 最后,通过此命令行将包括所有罐子

    mvn clean install -Pdomains,services

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

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