maven-jar-plugin 排除失败 [英] maven-jar-plugin Exclusions Failing

查看:104
本文介绍了maven-jar-plugin 排除失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试从同一个 pom 文件构建两个 jar(是的,我已经阅读了 这篇 Sonotype 博客文章 说不要)因为我们需要一个拥有我们所有资源的一个没有内部政治原因.我们已经使用我们认为应该可以工作的配置来配置 maven-jar-plugin,但资源总是包含在内.这是我们的 pom 文件的相关部分:

We are trying to build two jars from the same pom file (and yes, I have read this Sonotype blog post saying not to) because we need one with all our resources and one without for internal political reasons. We have configured the maven-jar-plugin with a configuration that we think should work, but the resources are always included. Here is the relevant part of our pom file:

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

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

When we build, we get OurProject.Jar and OurProject-consumer.jar as one would expect, but all the same resources are in each jar file.

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

We have tried <exclude>**/*</exclude> and <exclude>**/resources/*.*</exclude> instead of the list or specific extensions. No joy. I am hoping that we are missing something basic.

推荐答案

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

I recomend that you use maven-assembly-plugin for your consumer jar, but since you are determined to do it with maven-jar-plugin, let's fix your build.

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

The problem here is that you are confusing a setting that prevents resources from being filtered with the setting that actually excludes resources from the jar (both uses <exclude /> tags).

以下配置(在 内)会在打包阶段触发对 jar:jar 的第二次调用.它将从消费者 jar 中排除所需的资源(有效地做你想做的事):

The following configuration (inside <plugins />) triggers a second call to jar:jar during the package phase. It will exclude the desired resources from the consumer jar (effectively doing what you want):

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

虽然此配置(在 内)启用了 xml 和属性的过滤(即,在进程资源阶段使用 resource:resource 替换属性)档案;但不适用于将原样复制的图像和其他二进制文件.

While this configuration (inside <resources />) enables filtering (i.e., property replacing using resource:resource during process-resources phase) for xml and properties files; but not for images and other binary files which will copied unaltered.

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

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

With both configurations in place you will actually build two jars. The default with all resources (including filtered xml and properties files) and a secondary consumer jar with no resources.

这篇关于maven-jar-plugin 排除失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆