How to add a file to a war with Maven - maven-2

I have developed a maven plugin that downloads the release notes from JIRA.
It's bound by default to the 'generate-sources' phase and creates a 'release.txt' file in the build folder (${project.build.directory}).
My question: how can I add this file in the 'WEB-INF' folder of the war file built by Maven ?
I know I can use the 'maven-war-plugin' to include additional external resources from the 'src' folder, but I don't want my 'release.txt' file generated there (=not commitable to svn).
Thanks for your help. I wish you a nice day!
Maxence

I think this can be done using this feature of that plugin:
Adding and Filtering External Web Resources:
http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
Which would allow you to generate your release.txt into a separate folder (not src) and have the plugin treat it as an extra resources folder.
Hope that helps.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}</directory>
<targetPath>WEB-INF</targetPath> <!-- introduced in plugin v 2.1 -->
<includes>
<include>release.txt</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>

Related

Maven : Excluding src/main/webapp/resources from the WAR is not working

I have src and src_backup directories that are identical except that I have created src_backup from src by renaming (cache busting purpose) all the .js and .css files in
src/main/webapp/resources
directory.
I am using maven-war-plugin 2.4. I want the WAR file to include renamed files from
src_backup/main/webapp/resources
and exclude original files from
src/main/webapp/resources
I am able to include from src_backup but cannot exclude from src. What I end up getting is the WAR file containing both versions of files (original ones as well as renamed ones). I have read more than 5 different sources (such as stackoverflow) and tried more than 20 different ways to do so but it does not work.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>targetWAR</warName>
<warSourceExcludes>/${baseDir}/src/main/webapp/resources</warSourceExcludes>
<webResources>
<resource>
<directory>src_backup/main/webapp/resources</directory>
<targetPath>resources</targetPath>
</resource>
<resource>
<directory>src_backup/main/webapp/WEB-INF/views</directory>
<targetPath>WEB-INF/views</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
I have also tried to use resources tag in the build tag but did not work.
<resources>
<resource>
<directory>src/main/webapp/resources</directory>
<excludes>
<exclude>*</exclude>
</excludes>
</resource>
</resources>
I am sure I am missing something. Any help would be deeply appreciated.
You could change warSourceDirectory, see warSourceDirectory in war mojo. For instance:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>${basedir}/src_backup/main/webapp/resources</warSourceDirectory>
</configuration>
</plugin>
See also my answer to rename files, and not use different directories: Renaming static files when building WAR file using Maven

Using maven using non standard directory layout

I am trying to apply maven to an existing project which already has a directory structure in place. All I can find from previous question is the following.
Maven directory structure
However, my requirement is more detailed. Please see below for the directory structure:
<root dir>
|
+--src-java
|
+--src-properties
|
+--WEB-INF
I know we could have something like
<build>
<sourceDirectory>src-java</sourceDirectory>
...
</build>
But sourceDirectory is for JAVA source code only, if I'm not mistaken.
For the above structure, how do I declare it in pom.xml? Moving the directory is my last option right now.
I guess you need to have something similar to below.
Seeing WEB-INF, I assume you want to build a war. Maven war plugin does this. You will need to configure this a bit since the folder structure is non-standard - for instance you may need to specify the location of web.xml using webXml property. These are documented in the usage page.
<build>
<sourceDirectory>src-java</sourceDirectory>
...
<resources>
<resource>
<directory>src-properties</directory>
</resource>
</resources>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>WEB-INF</warSourceDirectory>
...
</configuration>
</plugin>
</plugins>
</build>
You can change the default directory structure declared in the Super POM by overwriting them in your pom.
For your example, e.g.
<sourceDirectory>src-java</sourceDirectory>
<resources>
<resource>
<directory>src-properties</directory>
</resource>
</resources>
Maven will copy all resources to the jar file. If you want to include WEB-INF to the jar it would be best to move it into the specified resource directory. Otherwise you have to copy it by your own (with maven plugins) to the target directory - I suppose.
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
From here.

Maven: How to add files to the root inside an EAR with maven-ear-plugin?

I need to add 2 XML files inside an EAR generated with maven-ear-plugin.
Unfortunately, I haven't seen a way to add an arbitrary file to an EAR; the documentation of the plugin which reads "The EAR plugin supports the following artifacts: ejb, war, jar, ejb-client, rar, ejb3, par, sar, wsr and har". There's nothing for adding a regular file.
org.apache.maven.plugins
maven-ear-plugin
2.3.1
foo
foo
1.4
lib
${parent.groupId}
foo-web
/foo
org.richfaces.framework
richfaces-api
commons-lang
commons-lang
Many thanks in advance.
In maven-ear-plugin 2.4.2 you can use config elements earSourceDirectory, earSourceExcludes and earSourceIncludes to declare extra files to include in the EAR.
By default you simply place those files to ${basedir}/src/main/application folder.
I had the same issue. I had the ejb.properties file under ejbs/src/main/resources and had used earSourceDirectory and earSourceIncludes to pull the file from the ejbs directory to the ear. However, it did not reliably put it in the lib directory. The EJB does not find it in .; it looks for it in the lib directory.
To fix this, I created the src/main/application/lib directory and created a link to the ejb.properties file. I then removed the earSourceDirectory and Includes properties. Now when I do a mvn clean package, it automatically pulls the properties file and puts it in the lib directory of the ear.
Sorry to be late for #wishihadabettername but I had same issue recently, more I couldn't move the files to include because they were in other folder then earSourceDirectory. I read maven-ear-plugin ear:ear documentation and thought about how to move my release folder on the root of ear.
Binds by default to the lifecycle phase: package.
and
workDirectory - Directory that resources are copied to during the build.
Default value is: ${project.build.directory}/${project.build.finalName}.
Then my submodule pom.xml looks like this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>prepare-package</phase> <!-- before package phase -->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- Work Directory of Ear plugin -->
<outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
<resources>
<resource>
<!-- my resource folder -->
<directory>release</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Hope it help people from now on

How to exclude pom.xml from Maven generated war?

Using Maven war plugin, I generate WAR which includes following directory:
META-INF
-- maven
-- com.abc.def
-- myServlet
-- pom.xml
-- pom.properties
In release, I want to exclude this maven directory. How can I do that?
I tried latest maven-war-plugin (2.1-beta-1), it has configuration "packagingExcludes", but it doesn't work as I wish.
Any suggestions?
I'm not sure but I think that the Maven Archiver (which is mainly used by plugins to handle packaging) can be configured to achieve this.
About the <addMavenDescriptor> element, the Maven Archiver Reference says:
Whether the generated archive will contain these two Maven files:
The pom file, located in the archive in META-INF/maven/${groupId}/${artifactId}/pom.xml
A pom.properties file, located in the archive in META-INF/maven/${groupId}/${artifactId}/pom.properties
The default value is true.
So a pom configured like this should do the trick:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
Using the standard Maven packaging you can't omit the file to my knowledge. It is possible however to use the maven-assembly-plugin to construct the war, in this case you have much finer grained control over the contents of the artifact, and can omit the pom.xml.
However I have personally found it useful to keep the pom.xml for diagnostic purposes. It can be handy to know what was used to build and assemble the war when trying to figure out what is wrong with your app.
Update: in a bizarre bit of synchronicity to Pascal's answer, I've just been reading up on the Archiver reference and it appears that this can be done by setting the addMavenDescriptor property to false. Personally I would still avoid doing this for reasons given above. But you may want to change your acceptance to Pascal's answer.
Putting a META-INF folder in a resources directory or in the root of your source directory will destroy the META-INF content created by Maven. For WAR files, putting a META-INF in your web content directory will do the same.
Adding other content to that custom META-INF will override what maven would create.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<warSourceExcludes>pom.xml</warSourceExcludes>
</configuration>
</plugin>
or
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<warSourceExcludes>here/there/everywhere/a/pom.xml</warSourceExcludes>
</configuration>
</plugin>

maven javaee application client plugin

I am pretty new to maven.
Is there any plugin or packaging type suitable for building application client jar file ?
I want to add the application-client.xml file to the META-INF folder inside the jar.
The normal jar packaging doesn't include the file.
You should only need to define the project with jar packaging (and as it is the default you don't need to declare it).
If you define the application-client.xml in the src/main/resources/META-INF folder it will be included in the META-INF folder of the final jar.
To define additional information you need to configure the jar plugin as below.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.app.App</mainClass>
<addClasspath>true</addClasspath>
</manifest>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
Check out the guide to working with manifests for full details
I'm not very familiar with the JavaEE support in Maven, but it looks like the ejb plugin can generate a client jar as well if configured properly. Check this page out:
Maven EJB Plugin - Generating an EJB client