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.
Related
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
I am having a problem about placing weblogic.xml under WEB-INF folder after mvn install. My weblogic.xml file is under src/main/resources/weblogic.xml and I want it to be placed under WEB-INF after install.(packaging is "war" by the way)
I tried this:
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>../resources</targetPath>
<excludes><exclude>web.xml</exclude><exclude>weblogic.xml</exclude></excludes>
</resource>
<resource>
<directory>src/main/resources/config</directory>
<targetPath>..</targetPath>
<includes><include>weblogic.xml</include></includes>
</resource>
</resources>
It is working with install but when I want a classpath using eclipse:eclipse, It gives the error :
Description Resource Path Location Type Cannot nest output folder 'ResponseManager/target/WEB-INF/resources' inside output folder
'ResponseManager/target/WEB-INF' ResponseManager Build path Build
Path Problem
because of this conf in classpath:
<classpathentry kind="src" path="src/main/resources" output="target/WEB-INF/resources" excluding="web.xml|weblogic.xml|**/*.java"/>
<classpathentry kind="src" path="src/main/resources/config" output="target/WEB-INF" including="weblogic.xml" excluding="**/*.java"/>
Any ideas?
Normally files under src/main/resources get packaged along the compiled class files, in a webapp they would be placed in WEB-INF/classes. Is there any reason why you can't put these under the standard path src/main/webapp?
If you need to package additional files which are not in the src/main/webapp folder then it would be better to configure these resources in the war plugin like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<webResources>
<resource>
<directory>src/main/config</directory>
<targetPath>WEB-INF</targetPath>
<includes>
<include>weblogic.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
It should be possible to specify the targetPath as above but I think it would be cleaner to reproduce the wanted directory structure inside your source folder.
How to exclude files from src/main/resources, for ex : I have a folder named "map" in there, which I wanna keep and I want to delete everything from war(or not to package it inside at firstplace).
Or alternative but same result, exclude all *.resources files from src/main/resources and put in war everything else?
Thank you
You may configure your resources like this:
<build>
<resources>
<resource>
<directory>src/main/resources/map</directory>
</resource>
</resources>
</build>
or this:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.log</exclude>
</excludes>
</resource>
</resources>
</build>
For more details, click here.
If you don't want some resources to be copied into target/classes, you can define includes or excludes in the resource element as documented in Including and excluding files and directories. For example:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/map/*.*</exclude>
</excludes>
</resource>
</resources>
</build>
If you want resources to be still copied into target/classes but for some reason don't want them to be packaged in the final artifact, then configure the maven war plugin to use packagingExcludes.
The official documentation for the maven resources plugin describes how you can perform includes and excludes.
http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
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>
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>