How to exclude files from being copied, by maven, into the exploded war - maven

I have a Java web app, in which I have some folders within the standard webapp source directory (src/main/webapp) that I don't want to get copied over into the war (exploded or packaged).
One of the reasons I don't want these files copying over is that we run the YUI JS & CSS minimizer & compressor on .js and .css files within the exploded war. The files that I want to exclude produce errors during the compression phase. The other reason I don't want them adding to the war is that they support testing a single page JS app that lives within the webapp (they are client side JS test scripts that rely on node / angular.js).
Below are the relevant sections from the POM.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<id>parent-resources</id>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<overlays>
</overlays>
<webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
</configuration>
<phase>generate-sources</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
</plugin>
I have tried, unsuccessfully, to use warSourceExcludes to exclude certain paths, but to no avail. An example of my usage is shown below, where client/ is a folder directly beneath src/main/webapp:
<configuration>
...
<warSourceExcludes>
<excludes>
<exclude>
client/
</exclude>
</excludes>
</warSourceExcludes>
...
</configuration>
What is the correct way to exclude certain paths, and or individual files, within the web app source directory from being included in the exploded war?
UPDATE
Following on from the suggestion from #maba I updated the configuration as follows:
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<overlays>
</overlays>
<webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
<warSourceExcludes>client/</warSourceExcludes>
</configuration>
The folder, client/, still is getting copied across. Any ideas?

Thanks to #alexksandr & #maba for their answers - which though correct didn't fully resolve my issue.
The solution seems to be - though I am not sure exactly why this is the case - that the configuration section is not picked up on when it is placed within the execution section.
Taking my original pom.xml and re-factoring it to make it work gives:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<overlays>
</overlays>
<webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
<warSourceExcludes>client/</warSourceExcludes>
</configuration>
<executions>
<execution>
<id>parent-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
</plugin>
The important detail seems to be that the configuration should be at the top level of the plugin and not within the execution section - though clearly the xml in my first attempt to use warSourceExcludes was way off target (see original question prior to the update section).

I could find no configuration of <warSourceExcludes> which worked, however <packagingExcludes> did work:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<packagingExcludes>excludeMe/**</packagingExcludes>
</configuration>
</plugin>

In order to exclude all files from folder use wildcards like that client/**
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<overlays>
</overlays>
<webappDirectory>${project.build.directory}/${project.build.finalName}-work</webappDirectory>
<warSourceExcludes>client/**</warSourceExcludes>
</configuration>

Your warSourceExcludes is wrong. Here is what the maven-war-plugin says regarding warSourceExludes:
The comma separated list of tokens to exclude when copying the content of the warSourceDirectory.
So in your case this is what your configuration should look like:
<configuration>
...
<warSourceExcludes>client/</warSourceExcludes>
...
</configuration>
This means also that if you want to add some more excludes, just add them separated with a comma:
<configuration>
...
<warSourceExcludes>client/,otherDir/,evenMoreDir/</warSourceExcludes>
...
</configuration>

Related

minify-maven-plugin (com.samaxes.maven v1.7.6) is not replacing the minified files with the original files

I want to minify JS and CSS files in maven project. I have used minify-maven-plugin (com.samaxes.maven v1.7.6). As per the documentation (https://samaxes.github.io/minify-maven-plugin/minify-mojo.html), I have set <nosuffix> and <skipMerge> as true because I want to maintain the file structure and replace the minified files with original files. I have also set the <phase>package</phase>.
After generating and deploying the WAR file, the JS and CSS files are not minified, they stay the same as before.
I also referred to some stackoverflow answers and set the <warSourceExcludes> option as per the suggestion provided at https://stackoverflow.com/questions/22117824/using-samaxes-minify-nosuffix-to-overwrite-original-files
After using the <warSourceExcludes> option, when I deploy the WAR file on the server, the JS and CSS files are not available and the application is showing 404 errors for the same. Please refer to my pom.xml configuration:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
</configuration>
</plugin>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.6</version>
<executions>
<execution>
<id>default-minify</id>
<phase>package</phase>
<configuration>
<charset>UTF-8</charset>
<webappSourceDir>${project.basedir}/WebContent</webappSourceDir>
<cssSourceDir>./</cssSourceDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssSourceExcludes>
<cssSourceExclude>**/*.min.css</cssSourceExclude>
</cssSourceExcludes>
<jsSourceDir>./</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>**/*.js</jsSourceInclude>
</jsSourceIncludes>
<jsSourceExcludes>
<jsSourceExclude>**/*.min.js</jsSourceExclude>
</jsSourceExcludes>
<jsEngine>CLOSURE</jsEngine>
<nosuffix>true</nosuffix>
<skipMerge>true</skipMerge>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
Please suggest a proper solution. Thanks in advance !
The answer given by #Kristof Neirynck for the question How to get maven to build a war with minified files using yuicompressor-maven-plugin works for the issue mentioned.
I changed the <phase> property as prepare-package and added
<webappTargetDir>${project.build.directory}/minify</webappTargetDir>
in minify-maven-plugin after <webappSourceDir> and in the maven-war-plugin I set the following configuration:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<webResources>
<resource>
<directory>${project.build.directory}/minify</directory>
</resource>
</webResources>
</configuration>
</plugin>
After using this configuration, when I deploy the WAR file in the Tomcat the original files are replaced with the minified files.

How do I add a file to the artifact (jar or war) created by maven?

I have an antrun task (goal run on phase prepare-package) set up that creates a file and saves it in /target/foo.bar. How do I add that to the artifact that gets created by maven (depending on module, it could be a jar or a war file)?
I have tried it with resources, with the builder-helper plugin, and the jar plugin - no luck:
<resources>
<resource>
<directory>target</directory>
<includes>
<include>**/foo.bar</include>
</includes>
</resource>
</resources>
That doesn't seem to do anything.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/foo.bar</file>
<type>bar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
According to debug output, this seems to install something extra in the repo, but doesn't add foo.bar to the artefact.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<includes>
<include>../foo.bar</include>
</includes>
</configuration>
</plugin>
No observable effect, either. :(
(I assume this might work for the war file, but I'd prefer one way to do it for both artifact types, if possible - plus I really need this to work for the jars, too...)
Is there a way to do what I want to do?
(I'd prefer not saving my file in src/main/resources first; I thought the packaging processes would pick files up from the build directory after they were placed there anyway, but I suppose I misunderstood something...)
I had my file saved to project.build.directory. Changing that to project.build.outputDirectory means the jar plugin is picking it up, without the need of any other plugins. Unfortunately, that doesn't sort the war issue out... :(
I had a similar problem and stored the resulting artifact in
target/${project.name}-${project.version}/foo.war
To be more precise, I created the war from a directory using the ant target
<war warfile="target/${project.name}-${project.version}/foo.war" basedir="somedir" />
Then it was correctly added to the surrounding ear.

Place minified files into jar

I will use the maven dependency com.samaxes.maven#minify-maven-plugin to minify my frontend project. This frontend- project is packaged into a jar (java archive) - so I have no war file because I use Spring boot.
With the configuration at the bottom *.min.js and *.min.css files are generated but they are not pacakged into jar file. I have read some threads and I have tried something but with no success: in the JAR file there are still the unminified css and js files.
Does anyone have any hint what I can do. I have a lot of css and js files in my project in different folder structures and the minified files should be placed in the same place like the unminified files are.
<build>
<plugins>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.4</version>
<executions>
<execution>
<id>default-minify</id>
<phase>package</phase>
<configuration>
<charset>UTF-8</charset>
<skipMerge>true</skipMerge>
<nosuffix>true</nosuffix>
<closureCompilationLevel>WHITESPACE_ONLY</closureCompilationLevel>
<webappSourceDir>src/main/resources</webappSourceDir>
<cssSourceDir>./</cssSourceDir>
<cssTargetDir>./minified</cssTargetDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssSourceExcludes>
<cssSourceExclude>**/*.min.css</cssSourceExclude>
</cssSourceExcludes>
<jsSourceIncludes>
<jsSourceInclude>**/*.js</jsSourceInclude>
</jsSourceIncludes>
<jsSourceExcludes>
<jsSourceExclude>**/*.min.js</jsSourceExclude>
</jsSourceExcludes>
<jsEngine>CLOSURE</jsEngine>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
[EDIT]
I have edit the Maven plugin in this way.
I really have to use src/main/recoures - it a specification at our project.
My problem now is that the files in the different folders unter the under the main folder path public/app are minified BUT the folders with the minified files are at same level than my public folder is.
So I would need something like this:
<webappTargetDir>**public/app/**${project.build.outputDirectory}</webappTargetDir>
Is there a possibility to do it like this?
<build>
<plugins>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.4</version>
<executions>
<execution>
<id>default-minify</id>
<phase>prepare-package</phase><!-- When omitted defaults to 'process-resources' -->
<configuration>
<charset>UTF-8</charset>
<skipMerge>true</skipMerge>
<closureCompilationLevel>WHITESPACE_ONLY</closureCompilationLevel>
<webappSourceDir>src/main/resources/public/app</webappSourceDir>
<webappTargetDir>${project.build.outputDirectory}</webappTargetDir>
<cssSourceDir>./</cssSourceDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<jsSourceDir>./</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>**/*.js</jsSourceInclude>
</jsSourceIncludes>
<jsEngine>CLOSURE</jsEngine>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
With the minify-maven-plugin configuration above I get the following folder structure at the left side but I would need the folder structure at the rigth side. Is there any possibility to do that?
The files are copied relatively from webappSourceDir to webappTargetDir. So if you want the structure to match, you should use similar patterns for those. Meaning, it should be
<webappSourceDir>src/main/resources</webappSourceDir>
<webappTargetDir>${project.build.outputDirectory}</webappTargetDir>
or
<webappSourceDir>src/main/resources/public/app</webappSourceDir>
<webappTargetDir>${project.build.outputDirectory}/public/app</webappTargetDir>
I've commited the working example here

Configuring Javadoc aggregation in Maven

I'm trying to create an aggregate Javadoc site for all the modules in my project, but I can't seem to configure the plugin in a way that is satisfactory. Mainly, I can't seem to get it to aggregate the javadocs all the while detecting links and excluding certain packages. Essentially, it appears the configuration of the plugin is ignored entirely.
I have a root pom.xml that refers to a bunch of submodules and contains the following configuration:
<modules>
<module>foo</module>
<module>bar</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.maven.apache.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>aggregate</id>
<phase>site</phase>
<goals>
<goal>aggregate</goal>
</goals>
<configuration>
<links>
<link>http://docs.oracle.com/javase/6/docs/api</link>
<link>http://static.netty.io/3.5/api</link>
<link>http://google-guice.googlecode.com/git/javadoc</link>
<link>http://docs.guava-libraries.googlecode.com/git-history/release/javadoc</link>
<link>http://fasterxml.github.com/jackson-databind/javadoc/2.0.4</link>
<link>https://developers.google.com/protocol-buffers/docs/reference/java</link>
</links>
<bootclasspath>${sun.boot.class.path}</bootclasspath>
<additionalJOption>-J-Xmx1024m</additionalJOption>
<detectJavaApiLink>true</detectJavaApiLink>
<detectLinks>true</detectLinks>
<excludePackageNames>*.testing.*</excludePackageNames>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
But when I run mvn javadoc:aggregate with this setup, I end up with a javadoc site that has no links to any of the referenced libraries and still includes all the testing classes.
I don't even see the plugin attempting to download the package-list for each declared link source.
On the other hand, generating the javadoc for each individual module works well and as expected.
What am I getting wrong?
Plugin configurations can be placed on two levels; inside the execution tag or outside of it ("global").
When the configuration is inside the execution tag it belongs to that particular execution. In your case you will have to run mvn site for it to execute since it is bound to that phase.
When the mvn javadoc:aggregate command is used it looks for the "global" configuration. In your pom there is no such configuration and thus it uses the default configuration.
Change your plugin configuration to this instead:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<configuration>
<links>
<link>http://docs.oracle.com/javase/7/docs/api</link>
<link>http://static.netty.io/3.5/api</link>
<link>http://google-guice.googlecode.com/git/javadoc</link>
<link>http://docs.guava-libraries.googlecode.com/git-history/release/javadoc</link>
<link>http://fasterxml.github.com/jackson-databind/javadoc/2.0.4</link>
<link>https://developers.google.com/protocol-buffers/docs/reference/java</link>
</links>
<bootclasspath>${sun.boot.class.path}</bootclasspath>
<additionalJOption>-J-Xmx1024m</additionalJOption>
<detectJavaApiLink>true</detectJavaApiLink>
<detectLinks>true</detectLinks>
<excludePackageNames>*.testing.*</excludePackageNames>
</configuration>
<executions>
<execution>
<id>aggregate</id>
<phase>site</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
You can place a configuration inside the execution part to override and specialize the configuration for that execution.
BTW The <groupId> is wrong in your pom. It should be
<groupId>org.apache.maven.plugins</groupId>
and not
<groupId>org.maven.apache.plugins</groupId>

How to include external source files in maven-jar-plugin

We are building a jar file from external(to the project) classes.
That works fine but we have not been able to figure out how to also include the external source files. I have tried using the "< includes >" tag but only end up with a manifest file in the final jar when used. I have looked at using the maven-resources-plugin but either I used it wrong or it doesn't work in my case. Here is a copy of our of code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<!-- <phase>generate-resources</phase> -->
<phase>clean</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${itendant.path}/web/rocket/WEB-INF/classes</classesDirectory>
<finalName>${itendant.jar.name}</finalName>
<outputDirectory>${itendant.jar.path}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Sources? Do you mean external dependencies? These should also be managed with maven, using mvn deploy:deploy-file as described on http://maven.apache.org/plugins/maven-deploy-plugin/usage.html , and imported in your pom.xml.
If you really mean external resources, then a proper resources declaration would be:
<project>
...
<build>
...
<resources>
<resource>
<directory> [your folder here] </directory>
</resource>
</resources>
...
</build>
...
</project>
You can have multiple tags if you have multiple resource directories, of course. Also note that building anything during clean is questionable, as clean is not run every build - package would be a better option.

Resources