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

时间:2017-08-18 09:46:16

标签: java maven

我必须使用maven构建一个war文件来有条件地包含jar, 每个jar都是由一个单独的maven项目创建的,该项目将jar部署到nexus(我们的组织远程)存储库

例如:我有像这些core.jar,reward.jar,payment.jar,domains.jar这样的罐子等等 我需要建立一个基于条件(environmnet)的最终战争,以包括上面的罐子

最后战争的组合(w1) w1.war:core.jar,domains.jar w1.war:core.jar,domains.jar,rewards.jar(如果奖励适用,可以指定包含此jar的任何方式)

2 个答案:

答案 0 :(得分:2)

Maven WAR Plugin允许您包含/排除JAR。例如:

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

您可以使用配置文件将包含/排除与条件相关联。例如,让WAR插件使用属性($ {excludedResources},$ {includedResources})...

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

...并通过配置文件定义这些属性的值:

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

因此,您可以使用Maven WAR Plugin的内置功能来调整WAR内容,并且可以使用Maven配置文件使这些调整成为条件。

答案 1 :(得分:1)

您可以尝试在maven中使用profiles功能。每个依赖关系可以包括在其自己的简档块中,例如仅当您通过domains个人资料切换此个人资料和服务时,才会包含services。 同时,您可以通过公共依赖块识别公共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>

激活的命令行可以是:

  1. 此命令行将包含core.jar和domains.jar

    mvn clean install -Pdomains

  2. 在这种情况下,war将包括core.jar和services.jar

    mvn clean install -Pservices

  3. 最后通过此命令行将包含所有jar

    mvn clean install -Pdomains,services