4

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.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
cneff
  • 388
  • 6
  • 15

3 Answers3

5

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.

Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
  • Thanks @Anthony-Acioly. Just moving the tag out of fixed the problem. I'm not understanding the need of your block of tags, since our jars seem correct without them. – cneff Jul 10 '13 at 13:43
  • Another caveat that we ran in to: When we frist made the change, it properly excluded the POM file from the consumer jar, which meant no changes had been made to anything in the consuer jar, so it didn't rebuild it! Needless to say this caused us some confusion and frustration until we noticed the different timestamps on the two jar files. – cneff Jul 10 '13 at 13:45
  • The second setting is there to enable [filtering](http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html) for a set of file extensions (while copying the others resources unaltered). I've assumed that you needed filtering because of the original `true`. As for the pom.xml, if you want it back you may need to rethink the `**/*.xml` pattern. – Anthony Accioly Jul 10 '13 at 15:01
2

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>
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
0

Turning on debug logs for Maven(mvn -X), I see the entries for maven-jar-plugin is listed like this:

[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

I want to exclude src/resources/stubs, so I add it like this:

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

And it works!

Before I put src/resources/stubs/*/** but does not work. Now I think it is relative to classpath, not project dir.

WesternGun
  • 11,303
  • 6
  • 88
  • 157