Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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>

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top