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

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>

Related

How to copy file from Parent POM into Maven Project

I want to have a Parent Maven project that has two files.
src/license/LICENSE.txt
src/license/NOTICE.txt
When I build my main Project, that references it as a Parent POM, I want those files to be included in the produced JAR.
I know I can use...
<build>
<resources>
<resource>
<targetPath>META-INF</targetPath>
<directory>src/license</directory>
<includes>
<include>NOTICE.txt</include>
<include>LICENSE.txt</include>
</includes>
</resource>
</resources>
</build>
But this will only work when the files are located in main Project, not when they are in the Parent project.
I found a solution to this.
First, create a new repo to hold the items you want to be copied in.
The POM of this should contain this (GAV details are com.jeff:base-pom-license)...
<build>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*.txt</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Then in your Base pom add this...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<configuration>
<resourceBundles>
<resourceBundle>com.jeff:base-pom-license:${project.version}</resourceBundle>
</resourceBundles>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
This will cause the files you have in the new maven repo to be included in all the places that extend the base pom.

maven override/replace file into WEB-INF/classes

I want to override/replace my spring config xml file with specified location file while package as war.
And I do not want to use filter plugin (filter plugin must to use dolloar placeholder, it will run with error while local deploy), is there any plugin or setting I can use to do this?
project structure as follow:
ROOT
----config
----prd
----spring-servlet.xml
----web.xml
----src
----main
----java
----resources
----spring-servlet.xml
----webapp
....
My pom.xml is like as follow:
...
<profiles>
<profile>
<id>prd</id>
<properties>
<filterDir>prd</filterDir>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webXml>${basedir}/config/${filterDir}/web.xml</webXml>
<webResources>
<resource>
<directory>config/${filterDir}</directory>
<targetPath>WEB-INF/classes/</targetPath>
<includes>
<include>mvc-dispatcher-servlet.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
run maven use :mvn clean package -P prd.
Will work fine if not set tartgetPath, file copied to web root.
You can use copy-resources of maven to do this.
Example maven code should be like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-web.xml</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>your-target-directory</outputDirectory>
<resources>
<resource>
<directory>source-directory</directory>
<!--You can also mention files too-->
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Read more about copy-resources for correct implementation.

Maven Resource Plugin Copying Files

I currently have some Maven projects which when I install the project I need to copy all files from the conf folder to the target folder.
|-Project
|--src
|--conf <--FROM HERE -->
|--lib
|--target <--TO HERE-->
I have attempted this in the pom.xml to no avail. What am I doing wrong? My plugin part of the pom.xml is below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>${basedir}/conf</directory>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
Your problem is that you copy the resources in the phase install. In that phase your target archive is already built and copied to your local repository. See the Maven lifecycle. You'll propably want to do it in the process-resources phase.

Maven Profiles Jar issues

I have dev/XYZ.properties file, i am building a jar file using maven -P dev install. How can I place the property file in child.jar/META-INF/XYZ.properties file
You have to execute the maven-resources-plugin:copy-resources goal for your profile dev and bind it with the appropriate phase (process-resources seems the best phase to me). Here is how to define your profile dev :
<profile>
<id>dev</id>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/META-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/dev</directory>
<includes>
<include>XYZ.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

maven-ejb-plugin: include generated sources

I have an EJB-maven-Project that has some generated classes (generated by JAXB).
They are generated into: target/generated-sources/jaxb/
Now, with maven-ejb-plugin I want them (i.e. their compilated classes) to be included into the client-jar, something like that:
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<!-- Tell Maven we are using EJB 3.1 -->
<ejbVersion>3.1</ejbVersion>
<generateClient>true</generateClient>
<clientIncludes>
<clientInclude>com/bla/ch/authorization/client/**</clientInclude>
<clientInclude>target/generated-sources/jaxb/**</clientInclude>
</clientIncludes>
</configuration>
</plugin>
This does not work, the generated classes are not part of the ejb-client-jar. (Though they are in the ejb-jar).
How can I do this correctly?
Include sources in your jar is probably not a good solution.
You may add generated sources in you resources locations, and then use source-plugin, to generate the so called artifact-sources.jar
<resources>
<resource>
<directory>${basedir}/target/generated-sources</directory>
<filtering>true</filtering>
</resource>
</resources>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
This is a better way than producing a jar with source code.

Resources