maven-jar-plugin Exclusions Failing

2019-07-02 10:09发布

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>

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.

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.

2条回答
来,给爷笑一个
2楼-- · 2019-07-02 10:26

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.

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

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>

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>

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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-02 10:44

I would suggest to make it like this:

 <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>
查看更多
登录 后发表回答