【问题标题】:maven-jar-plugin Exclusions Failingmaven-jar-plugin 排除失败
【发布时间】:2013-07-07 13:55:17
【问题描述】:

我们正在尝试从同一个 pom 文件构建两个 jar(是的,我读过 this Sonotype blog post 说不这样做),因为出于内部政治原因,我们需要一个拥有所有资源的容器,而另一个没有。我们已经为 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>

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

我们尝试了&lt;exclude&gt;**/*&lt;/exclude&gt; 和&lt;exclude&gt;**/resources/*.*&lt;/exclude&gt;,而不是列表或特定扩展名。没有喜悦。我希望我们缺少一些基本的东西。

【问题讨论】:

    标签: maven maven-jar-plugin


    【解决方案1】:

    打开 Maven(mvn -X) 的调试日志,我看到 maven-jar-plugin 的条目是这样列出的:

    [INFO] Building jar: /home/ddddddddd/Code/dpt/app/app/target/app-0.0.1-SNAPSHOT.jar
    [DEBUG] adding directory META-INF/
    [DEBUG] adding entry META-INF/MANIFEST.MF
    [DEBUG] adding directory com/
    [DEBUG] adding directory com/company/
    [DEBUG] adding directory com/company/dpt/
    [DEBUG] adding directory com/company/dpt/app/
    [DEBUG] adding directory com/company/dpt/app/extensions/
    [DEBUG] adding directory stubs/
    [DEBUG] adding directory stubs/requests/
    [DEBUG] adding directory stubs/__files/
    [DEBUG] adding directory stubs/mappings/
    ...
    [DEBUG] adding entry com/company/dpt/app/extensions/MyClass.class
    [DEBUG] adding directory META-INF/maven/
    [DEBUG] adding directory META-INF/maven/com.company.dpt/
    [DEBUG] adding directory META-INF/maven/com.company.dpt/app/
    [DEBUG] adding entry META-INF/maven/com.company.dpt/app/pom.xml
    [DEBUG] adding entry META-INF/maven/com.company.dpt/app/pom.properties
    

    我想排除src/resources/stubs,所以我这样添加:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>stubs</exclude>
                            <exclude>stubs/*/**</exclude>
                        </excludes>
                    </configuration>
                </plugin>
    

    而且它有效!

    之前我把src/resources/stubs/*/** 放了但是不行。现在我认为它是相对于类路径,而不是项目目录。

    【讨论】:

      【解决方案2】:

      我建议您将 maven-assembly-plugin 用于您的消费者 jar,但由于您决定使用 maven-jar-plugin 来完成,所以让我们修复您的构建。

      这里的问题是,您将阻止来自 being filtered 的资源的设置与实际上从 jar 中排除资源的设置混淆了(两者都使用 &lt;exclude /&gt; 标记)。

      以下配置(在&lt;plugins /&gt; 内)在打包阶段触发对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>
      

      虽然此配置(在 &lt;resources /&gt; 内)启用了 xml 和属性文件的过滤(即,在进程资源阶段使用 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>
      

      使用这两种配置,您实际上将构建两个 jar。默认包含所有资源(包括过滤的 xml 和属性文件)和一个没有资源的二级消费者 jar。

      【讨论】:

      • 感谢@Anthony-Acioly。只需将 标记移出 即可解决问题。我不理解您需要 标签块,因为没有它们,我们的 jar 似乎是正确的。
      • 我们遇到的另一个警告:当我们第一次进行更改时,它正确地从消费者 jar 中排除了 POM 文件,这意味着没有对消费者 jar 中的任何内容进行任何更改,所以它没有重建!不用说,这让我们有些困惑和沮丧,直到我们注意到两个 jar 文件上的不同时间戳。
      • 第二个设置是为一组文件扩展名启用filtering(同时复制其他资源不变)。我假设您需要过滤是因为原来的&lt;filtering&gt;true&lt;/filtering&gt;。至于 pom.xml,如果你想要它回来,你可能需要重新考虑&lt;exclude&gt;**/*.xml&lt;/exclude&gt; 模式。
      【解决方案3】:

      我建议这样做:

       <build>
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-jar-plugin</artifactId>
              <executions>
                <execution>
                  <id>second-jar</id>
                  <goals>
                    <goal>jar</goal>
                  </goals>
                  <configuration>
                    <classifier>without</classifier>
                    <excludes>
                      <exclude>**/*</exclude>
                    </excludes>
                  </configuration>
                </execution>
              </executions>
            </plugin>
          </plugins>
        </build>
      

      【讨论】:

      • 当然要替换排除部分(否则您将创建一个空罐子)。
      • 感谢@khmarbaise 的快速回答!考虑到 Anthony 的编辑,该解决方案奏效了。
      猜你喜欢
      • 2015-06-13
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多