maven-jar-plugin 排除失败

cne*_*eff 4 maven maven-jar-plugin

我们正在尝试从同一个 pom 文件构建两个 jar(是的,我读过这篇 Sonotype 博客文章,说不要这样做),因为我们需要一个拥有所有资源,而另一个由于内部政治原因而没有资源。我们已经使用我们认为应该有效的配置配置了 maven-jar-plugin ,但资源始终包含在内。这是我们的 pom 文件的相关部分:

<plugins>
    <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
            <execution>
                <id>package-consumer</id>
                <phase>package</phase>
                <configuration>
                    <classifier>consumer</classifier>
                    <resources>
                        <resource>
                            <directory>src/main/resources</directory>
                            <filtering>true</filtering>
                            <excludes>
                                <exclude>**/*.bmp</exclude>
                                <exclude>**/*.jpg</exclude>
                                <exclude>**/*.jpeg</exclude>
                                <exclude>**/*.gif</exclude>
                                <exclude>**/*.xml</exclude>
                                <exclude>**/*.sql</exclude>
                                <exclude>**/*.log4j</exclude>
                                <exclude>**/*.properties</exclude>
                                <exclude>**/*.sh</exclude>
                            </excludes>
                        </resource>
                    </resources>
                </configuration>
                <goals>
                    <goal>jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>
Run Code Online (Sandbox Code Playgroud)

当我们构建时,我们会得到 OurProject.Jar 和 OurProject-consumer.jar 正如人们所期望的,但所有相同的资源都在每个 jar 文件中。

我们已经尝试过<exclude>**/*</exclude><exclude>**/resources/*.*</exclude>而不是列表或特定扩展。没有喜悦。我希望我们缺少一些基本的东西。

Ant*_*oly 5

我建议您为您的消费者 jar 使用maven- assembly-plugin,但既然您决定使用 maven-jar-plugin 来完成它,那么让我们修复您的构建。

这里的问题是,您混淆了阻止资源被过滤的设置与实际从 jar 中排除资源的设置(两者都使用<exclude />标签)。

以下配置(内部)在打包阶段<plugins />触发第二次调用。jar:jar它将从消费者 jar 中排除所需的资源(有效地执行您想要的操作):

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <executions>
      <execution>
        <id>package-consumer</id>
        <phase>package</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        <configuration>
          <classifier>consumer</classifier>
          <excludes>
            <exclude>**/*.bmp</exclude>
            <exclude>**/*.jpg</exclude>
            <exclude>**/*.jpeg</exclude>
            <exclude>**/*.gif</exclude>
            <exclude>**/*.xml</exclude>
            <exclude>**/*.sql</exclude>
            <exclude>**/*.log4j</exclude>
            <exclude>**/*.properties</exclude>
            <exclude>**/*.sh</exclude>
           </excludes>
        </configuration>
      </execution>
    </executions>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

虽然此配置(在 内部)启用了对 xml 和属性文件的过滤(即,在流程资源阶段<resources />使用属性替换);resource:resource但不适用于将不加更改地复制的图像和其他二进制文件。

  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <includes>
      <include>**/*.xml</include>
      <include>**/*.properties</include> 
    </includes>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>false</filtering>
    <excludes>
      <exclude>**/*.xml</exclude>
      <exclude>**/*.properties</exclude>  
    </excludes>
  </resource>
Run Code Online (Sandbox Code Playgroud)

两种配置都到位后,您实际上将构建两个 jar。默认包含所有资源(包括过滤的 xml 和属性文件)和不包含资源的辅助消费者 jar。