maven resource plugin or packageExclude in war plugin - maven

I have a (exploded) war file generated by maven. I want to exclude some directories from the packaging. So I tried it with the two following ways:
Way:
<resources>
<resource>
<directory>${basedir}/src/main/webapp/resources</directory>
<excludes>
<exclude>css/*.css</exclude>
</excludes>
</resource>
</resources>
This does not work as expected. The plugin will copy all other files but the excluded above from my resource-directory (src/main/webapp/resources/) into the war-folder src/main/resources. I do not want to copy all the other files from src/main/webapp/resources to src/main/resources. The resource-plugin should ONLY exclude the above files and should not additionally copy the other files from src/main/webapp/resources into src/main/resources. Is there a way to say that to the resource-plugin?
The second way, does also not work:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<packagingExcludes>${basedir}/src/main/webapp/resources/css/*.css</packagingExcludes>
</configuration>
</plugin>
The debug output shows, that the path is correct:
[DEBUG] (s) packagingExcludes = /Users/test/src/main/webapp/resources/css/*.css
However, looking in my exploded war-archive, the "css/*.css" are NOT excluded. What is wrong?

Okay, I found the solution:
I have to use this
<configuration>
<warSourceExcludes>
...
</warSourceExcludes>
</configuration>
to exclude resources in the exploded war file.

Related

How can I exclude certain overlay-derived directories from my war using maven?

We build a webapp using maven 3.5.0 and the maven-war-plugin. We have many overlays, many of which have a tests directory that contains JSP files used for testing certain things in a dev environment. We don't want these included in our war.
We tried the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<overlays>...</overlays>
<warSourceExcludes>tests</warSourceExcludes>
</configuration>
</plugin>
This didn't work, nor did a couple of configuration variations:
<packagingExcludes>tests</packagingExcludes>
and
<packagingExcludes>**/${project.artifactId}/tests/**</packagingExcludes>
Is this due to my naive misuse of configuration options, or does it have to do with how overlays are processed?
You should use <excludes>..</excludes> (see https://maven.apache.org/plugins/maven-war-plugin/overlays.html) instead of <packagingExcludes>..</packagingExclude>...
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<overlays>
<overlay>
<groupId>com.example.projects</groupId>
<artifactId>documentedprojectdependency</artifactId>
<excludes>
<exclude>WEB-INF/classes/images/sampleimage-dependency.jpg</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
...
The dependentWarExcludes element worked to eliminate the tests directories and their contents:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<overlays>...</overlays>
<dependentWarExcludes>tests/**</dependentWarExcludes>
</configuration>
</plugin>

Ignoring binary files in Maven webapp resource filtering

Using Maven I need a property replacement of ${project.version} in an HTML file, so I add this to my POM:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<webResources>
<webResource>
<directory>${basedir}/src/main/webapp</directory>
<filtering>true</filtering>
</webResource>
</webResources>
</configuration>
</plugin>
But now Maven corrupts some of my font-related files, e.g. MaterialIcons-Regular.woff.
Rather than excluding a path glob (which I assume would mean I would need to re-include it in a separate section without filtering), is there any way just to mark some file extensions as binary as you can with the maven-resources-plugin and <nonFilteredFileExtension>?
The solution is just as it is for maven-resources-pluging:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<webResources>
<webResource>
<directory>${basedir}/src/main/webapp</directory>
<filtering>true</filtering>
</webResource>
</webResources>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>eot</nonFilteredFileExtension>
<nonFilteredFileExtension>ttf</nonFilteredFileExtension>
<nonFilteredFileExtension>woff</nonFilteredFileExtension>
<nonFilteredFileExtension>woff2</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
(I should have RTM for maven-war-plugin war:war.)

exclude WEB-INF from a maven generated war

How do I prevent the WEB-INF directory from being included in the .war package?
This does not work.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<warSourceExcludes>WEB-INF/**</warSourceExcludes>
</configuration>
</plugin>
This does not work, either.
<warSourceExcludes>WEB-INF</warSourceExcludes>
I would be incline to say that WEB-INF is not part of the source directory but is considered as a resource.
Therefore you should try with the webResource property instead:
https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
Something like:
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>resource2</directory>
<!-- the list has a default value of ** -->
<excludes>
<exclude>WEB-INF</exclude>
</excludes>
</resource>
</webResources>
</configuration>
Since it's a static HTML site with only .js, .css, and .html, I switched to the rar package format that only generates a META-INF. Then I added this:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
</plugins>
</build>
...
</project>

Maven war packaging, include classes ignores excluded resources

I have the following pom
<project>
....
<packaging>war</packaging>
....
<build>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archiveClasses>true</archiveClasses>
<warSourceExcludes>WEB-INF/sass/**</warSourceExcludes>
<attachClasses>true</attachClasses>
<webResources>
<resource>
<directory>src/main/resources/config</directory>
<excludes>
<exclude>**/*</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
...
</project>
As you can see, I package a WAR while my .class files are not in my WEB-INF/classes folder - they are packaged into a JAR instead.
Now, I am desperately trying to exclude some resources from my JAR - but it does not work. When I run mvn jar:jar - the resources are excluded, however when I run mvn package the resources are there.
Please help.
It seems that #user944849 is right - indeed, the war plugin does not use the jar plugin in order to achieve the JAR packaging.
However, his answer gave me a wrong result still as it will simply create 2 jars - one will be with the resources and the other without. The WAR will still use the wrong one.
The correct answer is to use the new maven resources tag.
The one that corresponds to my configuration looks as follows
<build>
....
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
....
</build>
You do not have the jar:jar goal bound to a lifecycle phase. When you run jar:jar from the command line, the exclusions happen fine; when you run mvn clean package I suspect jar:jar is not executing. Try binding the goal to a phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals><goal>jar</goal></goals>
<phase>prepare-package</phase>
<configuration>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
FYI, the archiveClasses feature of the war:war plugin goal does something similar to what I think you're trying to achieve without requiring a separate plugin config.

exclude file from maven build

I've a web application with file src/main/webapp/META-INF/context.xml which contains some configuration for testing database. On production server this file is in $TOMCAT_HOME/conf/Catalina/localhost/ROOT.xml and I'm testing with embedded tomcat so I don't want to package this file. I'd like to exclude this file from maven build. I tried following:
<build>
...
<resources>
<resource>
<directory>src/main/webapp/META-INF</directory>
<filtering>true</filtering>
<excludes>
<exclude>context.xml</exclude>
</excludes>
</resource>
</resources>
</build>
and also following:
<build>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<resources>
<resource>
<directory>src/main/webapp/META-INF</directory>
<filtering>true</filtering>
<excludes>
<exclude>context.xml</exclude>
</excludes>
</resource>
</resources>
</configuration>
</plugin>
</build>
But the file still gets packaged in war and in build directory (eg. target/myapp-1.0-SNAPSHOT/META-INF/context.xml). What am I doing wrong?
You can try using the packagingExcludes parameter
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>META-INF/context.xml</packagingExcludes>
</configuration>
</plugin>
To exclude a resource from build, the first snippet in the question looks fine, except that the absolute path of the resource directory should be specified. For instance
<directory>${basedir}/src/main/webapp/META-INF</directory>
The others have answered the main question but one other detail I noticed from your original attempted solution -
<filtering>true</filtering>
In Maven, "resource filtering" doesn't mean what you probably think it means. It's not about including/excluding resources but rather whether they should be processed to fill in embedded variable references.
See http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
Recently I had a similar problem with persistence.xml. Try putting this into your POM:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>META-INF/context.xml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
If it does not help, try replace maven-jar-plugin with maven-war-plugin.

Resources