Maven exclude directories

On the sony binding, I have a webapp that works fine. However, the default maven build task will include all the develop jars (and especially node_modules) in the final target build.

I am not a maven guru in the least bit - how do I tell maven (via my child pom.xml) to exclude certain directories from the final jar?

The WAR packaging of Maven projects is controlled through maven-war-plugin.

If your dependencies are not required to be embedded in final WAR mark it with <scope>provided</scope>. It might be also a good idea to create a separate module where only nodejs stuff will be packaged and linked through webResources in plugin configuration.

Ok - alot of what you said went ‘whoosh’ above my head :wink: But after googline, here is my structure and below and what I tried (unsuccessfully) to exclude:

src/main
--- feature
--- java
--- resources
------ esh-inf
------ web
--------- sonyapp
------------- dist
------------- node_modules
------------- public
------------- src

All I want to do is include “web\sonyapp\dist” into the final jar without the other three directories. In my pom.xml I tried:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
          <webResources>
            <excludes>
              <exclude>**/node_modules</exclude>
            </excludes>
          </webResources>
        </configuration>
      </plugin>
      ...

But the node_modules was still included (I’ll add public/src to that once I get a template correct for excluding node_modules).

I’ve tried various ways of excluding ("**\node_modules\**" and “**/src/main/resources/web/sonyapp/node_modules/**”).

Are you sure we are using the war plugin? In the output I don’t see that - I see the maven-jar-plugin plugin but no war plugin:

[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ org.openhab.binding.sony ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ org.openhab.binding.sony ---
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ org.openhab.binding.sony ---
[INFO] Building jar: C:\Users\troberts\Development\openhab2-addons\bundles\org.openhab.binding.sony\target\org.openhab.binding.sony-2.5.0-SNAPSHOT.jar

Thanks for reply Tim, indeed I misread your first post. You wrote that you have “webapp” which I translated into maven-war-plugin. Sorry about that. After checking output I figured out you have a standard ESH/openHAB binding, which means there is no WAR at all, I should have been asked about how you build the whole thing.

From the output it is clear that you have maven-jar-plugin. You can try to use a few things

Above ways are slightly different. The main difference between 2nd and 3rd is that build helper allows you to link a couple of plugins into a pipeline where one generates resources by ie. unpacking some archive and then dynamically bind it.

The includes/excludes in the first case is the simplest one. You can try to run Maven build with -X option to enable verbose output to see how your rules are applied to JAR contents.

Be aware that JAR might be still post-processed by bnd to generate an OSGi manifest.

Thanks - the “-X” helped me immensely. Below is the final solution (I had to hit both the maven-jar-plugin to exclude the source/node_modules directories and the maven-source-plugin to ignore the node_modules directory). Note: the last step is what compile the web app…

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>web/sonyapp/node_modules/**</exclude>
            <exclude>web/sonyapp/public/**</exclude>
            <exclude>web/sonyapp/src/**</exclude>
            <exclude>web/sonyapp/*</exclude>
          </excludes>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>web/sonyapp/node_modules/**</exclude>
          </excludes>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            <id>npm run build</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>npm</executable>
              <arguments>
                <argument>run</argument>
                <argument>build</argument>
              </arguments>
              <workingDirectory>${basedir}/src/main/resources/web/sonyapp</workingDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>