从构建中排除 Maven 依赖

Exclude maven dependency from build

我有两个 Maven 项目。项目 A 和项目 B。B 使用 A 作为依赖项。当我将 B 构建为 war 时,Maven 导出 A 及其依赖项。如何告诉 Maven 在导出时不要单独包含这个依赖树?我在 Maven war 配置中找不到排除标签。

注意:我不能在依赖项中使用提供的范围,因为我需要它来测试使用嵌入式 tomcat。

项目 B Pom 的片段

<dependencies>
<!-- Project A -->
<dependency>
    <groupId>test</groupId>
    <artifactId>test-core</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
            <encoding>UTF-8</encoding>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
    <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat7-maven-plugin</artifactId>
      <version>2.2</version>
      <configuration>
        <path>/</path>
        <port>80</port>
      </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.2</version>
        <configuration>
            <!-- Exclude Tag is not available in configuration -->
        </configuration>
    </plugin>  
  </plugins>

您需要创建配置文件,您可以创建两个配置文件,一个用于开发(嵌入 tomcat,有 dep A),另一个用于生产(没有 dep)

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <!-- dep A -->
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>prod</id>
        <dependencies>
            <dependency>
                <!-- dep A -->
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

我终于决定在 git 中克隆一个分支并将其称为发布分支。它现在有一个不同的 Pom,即,将提供 B 中项目 A 的范围。在 Dev 分支中进行一系列更改后,我会将其挑选到发布分支中并构建 jars。我也相信这对 Jenkins 应该有好处(还没有测试,但那是下一个)