Maven war packaging, include classes ignores excluded resources - maven

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.

Related

How to Add JUnit Results to Tycho Built JAR

We are using Maven and sometimes Tycho, and I want to put the results of the JUnit tests into the resulting JARs.
With Maven, that's easy. I just added the following to the pom.xml:
<resources>
<resource>
<directory>target/</directory>
<includes>
<include>surefire-reports/*.*</include>
</includes>
</resource>
</resources>
The test phase is before the package phase, so all is well.
For Tycho however, tests are executed after the package phase (because they are executed in the verify phase for some reason). The phase cannot be changed either (see bug 440094).
So the only alternative is to build another JAR after the verify phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<phase>post-integration-test</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>verification-doc</classifier>
<includes>
<include>**/surefire-reports/*.*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
However, it does not work. Neither the folder nor the files are added. My guess was that it's because of the include tag points to the source folders, so I tried:
<include>${project.build.outputDirectory}/../surefire-reports/*.*</include>
<include>${project.build.directory}/surefire-reports/*.*</include>
Which did not work either?
So how do I add JUnit test results to the JAR of a Tycho build?
You can change the directory of the maven-bundle-plugin like this:
<configuration>
<classesDirectory>${project.build.directory}</classesDirectory>
<includes>
<include>surefire-reports/*</include>
</includes>
</configuration>

maven resource plugin or packageExclude in war plugin

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.

Include non Java files and resources files to test-jar using maven jar plugin

I would like to include scripts files as part of packaging test files using maven. I ma using the below plugin configuration however config and jython files the files are not package in the test jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<include>**/config/*</include>
<include>**/jython/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
is there anyway to include the files which are not in src/test/java and src/test/resources in test-jar?
Specify additional test resource folders using:
<build>
<testResources>
<testResource>
<directory>src/test</directory>
<includes>
<include>**/config/*</include>
<include>**/jython/*</include>
</includes>
</testResource>
</testResources>
</build>
You should then find that no config is required for the maven-jar-plugin.
The build-helper-maven-plugin may also be used. See: Maven - Add directory to classpath while executing tests

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.

Maven: how to get a war package with resources copied in WEB-INF?

when I create a war package with maven, files and directories under the directory "src/main/resources" are copied in /WEB-INF/classes instead of /WEB-INF. How can I get them copied in /WEB-INF?
thanks,
rand
UPDATE:
in my pom now I use this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>war</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>myapp/target/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
and I launch mvn with:
mvn -Dmaven.test.skip=true clean package resources:copy-resources
but I got:
[INFO] One or more required plugin parameters are invalid/missing for 'resources:copy-resources'
[0] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<outputDirectory>VALUE</outputDirectory>
</configuration>.
[1] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<resources>VALUE</resources>
</configuration>.
I'm using maven 2.2 and the snippet basically is the same of the documentation
any idea?
either configure the outputDirectory parameter of resources:resources plugin, or put your files under src/main/webapp/WEB-INF/ directory.
resource plugin
EDIT:
This configuration is working for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<id>default-copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
you can run a phase in the form somePhase or a goal somePlugin:someGoal. The phase invocations will invoke all plugins goals hooked on phases in interval [validate,phase] in order, so there's no need to explicitly call them.
Web resources are not the same as java resources, which should be placed in the classpath. Web resources are processed via the war plugin and should be placed into src\main\webapp\WEB-INF\. In this case, it will work automatically without any additional configuration in the pom.xml
This configuration is working add plugin pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<!--copy resource file location-->
<resource>
<directory>${project.build.directory}/classes</directory>
</resource>
</webResources>
<!--location for add file-->
<webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
</configuration>
</plugin>

Resources