Maven: include folder in resource folder in the war build - maven

I've a folder named extra-jars in the src/main/rescource, but how can I include these in the build? I want them to be put in the lib folder with the rest of the jars. I tried including them through , but that didnt work.

For jars that are not distributed by a Maven repository, the simplest way is place the extra jars in the src/main/webapp/WEB-INF/lib directory of your project. Maven will by convention, include everything under the src/main/webapp in the final war artifact.
An additional method is to use the Maven War Plugin. It has the ability to add additional files to the final war artifact though plugin configuration.
In the build section of the POM add something like the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>src/main/resource/extra-jars</directory>
<includes>
<include>*.jar</include>
</includes>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
The <configuration> section is the key to adding additional files.
The <directory> element defines the source location of the resource. The path is relative to pom.xml.
The <includes> element defines what files found in the above directory to include.
The <targetPath> element defines the destination directory in the WAR to which the files are copied.

These jars should be added as Maven dependencies, not by copying them into the lib folder. This is the sort of thing Maven is designed for.

Related

Why does maven create two copies of my filtered persistence.xml?

Below is the relevant section of the maven project I'm working on. I would like Maven to filter the persistence-context.xml file inside of the WEB-INF directory, and then place it into the WEB-INF directory inside of my war. When I run mvn clean package on this project I see the following two things.
The successfully filtered persistence-context.xml in the target/projectname/ folder. This is NOT the correct place. I want it one directory up in the WEB-INF.
The unfiltered persistence-context.xml in the target/projectname/WEB-INF/ folder. This is NOT what I want. I want the filtered one here.
I'm not even sure how two copies of this file are being generated! Any help would be much appreciated.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<includes>
<include>persistence-context.xml</include>
</includes>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
I realized I had another plugin entry for the war plugin which was interfering (possibly overwriting?) with this one. Once I removed that my above entry worked okay!

Maven war project copy website into webapp directory

I have a maven java web project and it build fine, and I have another website project (pure HTML/Javascript) which is outside the maven web project.
The directory structure is like:
project
|--backend
|--|--src
|--|--|--main
|--|--|--|--java
|--|--|--|--webapp
|--|--pom.xml
|--frontend
|--|--test
|--|--app
What I want to achieve is during the build in maven, copy the frontend/app directory into the webapp directory, before the WAR file is produced. How can I do that in maven?
Actually the maven war plugin support this directly.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>../website</directory>
<include>app/**/*</include>
</resource>
</webResources>
</configuration>
</plugin>
use a maven plugin such as maven resource plugin

maven-jar-plugin, include upper dir

Build part of POM
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<classesDirectory>./</classesDirectory>
<includes>
<include>*.wsdl</include>
<include>*.xsd</include>
<include>sources/</include>
<include>../configuration.doc</include>
</includes>
</configuration>
</plugin>
In target jar I have all wsdl from root dir, xsds from root dir and sources dir.
But no configuration.doc file in jar.
Any ideas?
You should move the configuration.doc into the appropriate directory like src/main/resources.
How about adding the relevant folder from where you want the .doc to be picked up to your project pom using maven resources plugin - specifically the <resources> configuration - there are examples here. This will make the contents available to the jar plugin.

maven resources plugin flat copy of resources

Given a folder 'database' containing JAR-connectors for different RDBMS. Each JAR is located in its own folder:
+---database
+---db2
+---db2.jar
+---derby
+---derby.jar
+---h2
+---h2.jar
+---mysql
+---mysql.jar
I need all of those JAR-files to be copied into WEB-INF\lib.
Here's my pom.xml:
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>../target/${project.artifactId}/classes/database</directory>
<targetPath>WEB-INF/lib</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
Problem is, that those JARs are copied with their directories:
+---WEB-INF/lib
+---db2
+---db2.jar
+---derby
+---derby.jar
+---h2
+---h2.jar
+---mysql
+---mysql.jar
This is how it should be:
+---WEB-INF/lib
+---db2.jar
+---derby.jar
+---h2.jar
+---mysql.jar
I have 20 connectors and I don't want to hard code them.
The most correct way of doing so will be to install your jar files into Maven repository and then use maven-dependency-plugin:copy goal. Or if you want to solve this roughly then use maven-antrun-plugin (copy rule).
You're having problems because you're trying to bend maven into something it's not supposed to do. Binary artifacts should be deployed into your artifacts repository (or local maven repository) and not included into your project. This way having them defined as dependencies in pom would ensure that they're copied into your WEB-INF/lib.

In Maven how to exclude resources from the generated jar?

When I create an executable jar with dependencies (using this guide), all properties files are packaged into that jar too. How to stop it from happening? Thanks.
UPDATE: I tried to exclude them using the Maven resources plugin, but then my application won't find the properties files when I run it in Eclipse (right click on the module -> Run As -> Java Application)
UPDATE: Thanks for your useful answers. I think I'd better spend time to learn Maven, for now I just choose the simplest solution.
To exclude any file from a jar / target directory you can use the <excludes> tag in your pom.xml file.
In the next example, all files with .properties extension will not be included:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
</build>
By convention, the directory src/main/resources contains the resources that will be used by the application. So Maven will include them in the final JAR.
Thus in your application, you will access them using the getResourceAsStream() method, as the resources are loaded in the classpath.
If you need to have them outside your application, do not store them in src/main/resources as they will be bundled by Maven. Of course, you can exclude them (using the link given by chkal) but it is better to create another directory (for example src/main/external-resources) in order to keep the conventions regarding the src/main/resources directory.
In the latter case, you will have to deliver the resources independently as your JAR file (this can be achieved by using the Assembly plugin). If you need to access them in your Eclipse environment, go to the Properties of your project, then in Java Build Path in Sources tab, add the folder (for example src/main/external-resources). Eclipse will then add this directory in the classpath.
This calls exactly for the using the Maven JAR Plugin
For example, if you want to exclude everything under src/test/resources/ from the final jar, put this:
<build>
<plugins>
<!-- configure JAR build -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<excludes>
<exclude>src/test/resources/**</exclude>
</excludes>
</configuration>
</plugin>
...
Files under src/test/resources/ will still be available on class-path, they just won't be in resulting JAR.
Put those properties files in src/test/resources. Files in src/test/resources are available within Eclipse automatically via eclipse:eclipse but will not be included in the packaged JAR by Maven.
Exclude specific pattern of file during creation of maven jar using maven-jar-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.exe</exclude>
<exclude>**/*.java</exclude>
<exclude>**/*.xls</exclude>
</excludes>
</configuration>
</plugin>
Do you mean to property files located in src/main/resources? Then you should exclude them using the maven-resource-plugin. See the following page for details:
http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
Another possibility is to use the Maven Shade Plugin, e.g. to exclude a logging properties file used only locally in your IDE:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin-version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>log4j2.xml</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
This will however exclude the files from every artifact, so it might not be feasible in every situation.
When I create an executable jar with dependencies (using this guide), all properties files are packaged into that jar too. How to stop it from happening? Thanks.
Properties files from where? Your main jar? Dependencies?
In the former case, putting resources under src/test/resources as suggested is probably the most straight forward and simplest option.
In the later case, you'll have to create a custom assembly descriptor with special excludes/exclude in the unpackOptions.
here is another solution to exclude all files in resources folder, the final configuration looks like:
<build>
<!-- exclude all files in resources-->
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/**</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<!-- other configurations/plugins in the pom.xml-->
</build>
or we can use includes to only package some file or folder.
But this method has a side effect. IDE will also exclude the resource files in target/classes folder. maven-jar-plugin only affect jar file.
I found a better solution to execludes resourses folder using maven-jar-plugin, here we use includes:
<build>
<plugins>
<!-- configure JAR build -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
</configuration>
</plugin>
...

Resources