Maven: exclude resource from war - eclipse

I am currently trying to exclude some resource from my built war.
I have read the documentation and the forums, and found a lot of informations.
Unfortunately nothing worked in my case...
I have an Eclipse Maven project, and if I'm right, maven-war-plugin is the default "war builder", so I have to override it in my pom.xml in order to exclude the resource from the buildt war.
I tried warSourceExcludes, packagingExcludes and webResources/excludes :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven.war.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warSourceExcludes>src/main/webapp/frontEndWorkspace</warSourceExcludes>
<packagingExcludes>src/main/webapp/frontEndWorkspace
</packagingExcludes>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/webapp</directory>
<!-- the list has a default value of ** -->
<excludes>
<exclude>**/frontEndWorkspace</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
Despite this configuration, I still have the frontEndWorkspace directory pushed in my Tomcat...
I wonder if it comes from the fact that I use it in my Eclipse environment?
Thanks in advance!

The parameters that you can use are packagingExcludes which is more generic (applies on the complete war structure) or warSourceExcludes if the files you want to exclude are specifically in the folder defined by the parameter warSourceDirectory (default being ${basedir}/src/main/webapp) (see here). It works easily when you know that it starts considering the folder structure of the war.
Example :
This will exclude all the files finishing by *.jsp contained in the folder WEB-INF of the folder defined by the parameter warSourceDirectory (default being ${basedir}/src/main/webapp) :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceExcludes>WEB-INF/**/*.jsp</warSourceExcludes>
</configuration>
</plugin>
This will exclude all the files contained in all the folders pouet contained in the war (but not the folder structure) :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<packagingExcludes>**/pouet/**/*.*</packagingExcludes>
</configuration>
</plugin>
The problem of your configuration <warSourceExcludes>src/main/webapp/frontEndWorkspace</warSourceExcludes> is that you start from the source folder which is wrong. You just have to remove src/main/webapp and add /** after frontEndWorkspace to have <warSourceExcludes>/frontEndWorkspace/**</warSourceExcludes> working (or <warSourceExcludes>frontEndWorkspace/**</warSourceExcludes>).

I am finding the same issue within Intellij IDEA which is using Maven 3.
The war file it generates contains the directory I am excluding.
UPD
Solution is to use syntax as below to eliminate the myFolder directory
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/resources</directory>
<excludes>
<exclude>**/myFolder/**</exclude>
</excludes>
</resource>
</webResources>

Related

Display maven-excluded directories in STS package explorer?

I use tags in my maven POM file to exclude a directory structure I do not want compiled:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<excludes>
<exclude>**/somedir/**</exclude>
</excludes>
</configuration>
</plugin>
However, I would like to have the directory displayed in the STS (3.9.2) Package Explorer. I've tried disabling all view filters, but the excluded directory doesn't display. Is it possible to have maven-excluded directories displayed?
The project itself was created using "Import existing Maven project."

How to tell Maven to update manifest's Bundle-Version?

I'm building Eclipse's plugins using Maven, and I'm looking for a way to tell Maven to update the Bundle-Version in the META-INF\MANIFEST.MF file.
The trick is, I need a clean way to do it. So that further colleagues (or even me in some months/years) may not break the final automated build.
Here is what I tried :
Using "Bundle-Version: ${project.version}" in the MANIFEST.MF
On a maven point-of-view, this is working.
BUT my Eclipse project is constantly displaying an error : The specified version does not have the correct format (major.minor.micro.qualifier)
Using the above but in another META-INF folder (named META-INF-MAVEN) that is used by maven instead of the original :
<resources>
<resource>
<directory>META-INF-MAVEN</directory>
<targetPath>META-INF</targetPath>
<filtering>true</filtering>
</resource>
</resources>
This is still "working", but the needed duplication of MANIFEST.MF file is far away from what I'm looking for. This is a large trap for future errors, like editing the original MANIFEST.MF file and forgetting the maven one, resulting in a bugged release.
Using org.apache.maven.plugins to add the Bundle-Version tag at compile-time from the pom file :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>target/classes/META-INF/MANIFEST.MF</manifestFile>
<manifestEntries>
<Bundle-Version>${project.version}</Bundle-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
This is the most promising way, BUT it only works if the original MANIFEST.MF does not have a Bundle-Version line.
The result is, again, an error within the Eclipse project.
And so, here I am, asking for any idea.
Thanks.
Following solution finds the Bundle-Version: line in META-INF/MANIFEST.MF and replaces it with the Maven project version. replacer maven plugin was used for that but there are certainly other maven plugins that can do the same work like find-and-replace-maven-plugin.
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>update-manifest-version</id>
<phase>process-resources</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<file>${basedir}/META-INF/MANIFEST.MF</file>
<regex>true</regex>
<replacements>
<replacement>
<token>Bundle-Version: .*</token>
<value>Bundle-Version: ${project.artifact.selectedVersion.majorVersion}.${project.artifact.selectedVersion.minorVersion}.${project.artifact.selectedVersion.incrementalVersion}.qualifier
</value>
</replacement>
</replacements>
</configuration>
</execution>
</executions>
</plugin>

Filtering Java classes in Maven WAR

I struggling with Java class filtering in a Maven WAR. The Maven compile a lot of Java classes and all of them appear in
target/classes
and I have packages like these:
a/A.class
a/b/B.class
a/b/C.class
Up to this point is OK. But what I need is to have a single Java class inside of
WEB-INF/classes
like this one:
a/b/B.class
This is the plugin's section from my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>${project.artifactId}</warName>
<webXml>${basedir}/src/main/application/WEB-INF/web.xml</webXml>
<packagingExcludes>WEB-INF/lib/*.jar,a/A.class,a/b/C.class</packagingExcludes>
<webResources>
<resource>
<directory>${basedir}/src/main/application/WEB-INF</directory>
<includes>
<include>jboss-web.xml</include>
</includes>
<targetPath>WEB-INF</targetPath>
</resource>
<resource>
<directory>${basedir}/src/main/application/META-INF</directory>
<includes>
<include>standard-jaxws-endpoint-config.xml</include>
</includes>
<targetPath>META-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
Unfortunately all the classes are packaged into my WAR.
Any hint?
Best regards,
SK
I believe this will filter the classes based on a relative location to the app that is built in the target directory. Which means your class files are stored in WEB-INF/classes at this stage.
Try changing this:
<packagingExcludes>WEB-INF/lib/*.jar,a/A.class,a/b/C.class
To
<packagingExcludes>WEB-INF/lib/*.jar,WEB-INF/classes/a/A.class,WEB-INF/classes/a/b/C.class

How to debug an OSGi Bundle inside eclipse using maven structured project

I have a maven project which library should also be an OSGi bundle with an
declarative service. I added the OSGI-INF folder with the service.xml inside
src/java/resources which will be added to the jar. But: When I start the project as equinox project, where I want to check if the service is loaded, I get the error that the OSGI-INF/service.xml can't be found. I guess eclipse won't add the resources folder to the classpath when starting.
BTW: The MANIFEST-MF is in the root folder and the pom.xml contains the following text:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
Has anbody a good idea how to tell eclipse where to find the metadata files? I'm using m2eclipse if this is relevant.
Thanks in advance
Hannes
I've the same issues but I've manually tweaked my POM.xml for copying the generate target/classes/META-INF/** stuff (MANIFEST.MF, property files, spring XMLs, ...) into the project ROOT folder (which Eclipse PDE expects):
<!--
We copy all stuff from target/classes/META-INF into META-INF/ in order
to keep Maven output with PDE.
-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>synch-pde-metadata-from-maven</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/META-INF</outputDirectory>
<resources>
<resource>
<directory>target/classes/META-INF</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--
We delete all stuff from the root bundle's META-INF
-->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<filesets>
<fileset>
<directory>META-INF</directory>
<includes>
<include>**/*</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
(I also clean up things when mvn clean is called) It is not really efficient but it works.
Ps: you may have to right-click on the project and make it a plug-in project in order for this to work.
The Problem is that Eclipse needs that OSGI-INF (and also META-INF) Folder in root.
I have found a link on how to probably solve the problem.
https://www.nuxeo.com/blog/working-with-osgi-and-maven-in-eclipse/
Basically they put the OSGI-INF and META-INF Folders into src/main/resources
Then they set the src/main/resources Folder as root of the OSGI project. So that Eclipse has both Folders at root.
To have Soure Files also available they added a Linked Resources to src/main/java by adding an entry to the .project File
<linkedResources>
<link>
<name>src</name>
<type>2</type>
<locationURI>PARENT-1-PROJECT_LOC/java</locationURI>
</link>
</linkedResources>
Now you just need to copy the .prject and .classpath Files into src/main/resources (your new root) and everything should be working.
By the time of this writing i didn't test this on my own but will do so in the near future.

Problem when importing Maven2 project in Eclipse

In my project, I have a resources directory (src/main/resources) that contains properties and XML files.
I want to filter only the properties files, but not any others kind of files (XML for example). Thus, I've set this in my pom.xml:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
</build>
This is working well when I run a Maven 2 package command, i.e. both XML and properties files are included in my final JAR, and only properties files have been filtered.
However, as I want to include this project in Eclipse, when I run the command mvn eclipse:eclipse, and import the project, then I have a problem with the source declared in my project properties.
In the "Java Build Path" option of Eclipse for my project, in tab "Source", I see the src/main/resources directory, but Eclipse also add filters which say to exclude all java files (Excluded: **/*.java) and include only properties files (Included: **/*.properties).
In the .classpath file generated, I get this line:
<classpathentry kind="src" path="src/main/resources" including="**/*.properties" excluding="**/*.java"/>
This way, the JAR built by Eclipse is not correct as all my XML files are not in the JAR.
How can I solve this problem?
Edit, regarding this page, I've added this in my pom.xml:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.6</version>
<configuration>
<sourceIncludes>
<sourceInclude>**/*.xml</sourceInclude>
</sourceIncludes>
</configuration>
</plugin>
</plugins>
However, the .classpath generated is not modified with the adequate information...
Edit again.
The addition in my previous edit works only for version 2.6.1+ of the Eclipse plugin, not for 2.6. So, I've tried with version 2.7. However, I don't know how to force the Eclipse plugin to not define the including attribute:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.7</version>
<configuration>
<sourceIncludes>
<sourceInclude>*</sourceInclude>
</sourceIncludes>
</configuration>
</plugin>
</plugins>
If I run the mvn eclipse:eclipse command, I get the following error:
Request to merge when 'filtering' is not identical. Original=resource src/main/resources: output=target/classes, include=[**/*.properties], exclude=[**/*.java], test=false, filtering=true, merging with=resource src/main/resources: output=target/classes, include=[], exclude=[**/*.properties|**/*.java], test=false, filtering=false
Well maven-eclipse-plugin is very bugy :( and we use version 2.5. The solution we found is copy resources to different folder. For regular maven bulid we use default filtering but for eclipse we have special profile. Here is maven-eclipse=plugin configuration in this profile:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources-step1</id>
<goals>
<goal>copy-resources</goal>
</goals>
<phase>initialize</phase>
<configuration>
<outputDirectory>${project.build.directory}/for-eclipse/resources</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resources-step2</id>
<goals>
<goal>copy-resources</goal>
</goals>
<phase>initialize</phase>
<configuration>
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/for-eclipse/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Of course you must modify include/exclude section for your needs.
I did some testing with the maven eclipse plugin 2.6, 2.5.1, 2.7 and 2.8-SNAPSHOT and none of them is indeed producing the expected result.
The only workaround I've been able to find is to use another directory for the resources you don't want to filter. Something like this:
...
<build>
<resources>
<resource>
<directory>src/main/resources1</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources2</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</build>
I agree that this is ugly. But at least, it produces a .classpath with the following entries:
<classpathentry kind="src" path="src/main/resources1" including="**/*.properties" excluding="**/*.java"/>
<classpathentry kind="src" path="src/main/resources2" including="**/*.xml" excluding="**/*.java"/>
That should allow you to deploy on Tomcat.
By the way, I wouldn't use the version 2.7 of the plugin because it's not working (see this thread). The version 2.6 may not produce the expected output but at least, it works.
Use properties to have Eclipse and Maven build into different directories. Now you can use Eclipse to quickly develop your code and use Maven to deploy it as a JAR.
I suggest that you either the mvn package goal as an external tool from Eclipse. I don't think that Eclipse can handle complex Maven workflows.
Even the m2eclipse plugin, which I use and highly recommend, has troubles with complex workflows.