Maven resource plugin expose an image from a different path - maven

I have an immage accessible from /resources/gfx/loading.gif
I would like to have it accessible from /img/immage.gif
I tried with maven-resources-plugin with the following config, but actualy nothing happens.
I'm curretnly building the app from eclipse.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-loading-status</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/src/main/webapp/img</outputDirectory>
<resources>
<resource>
<directory>/src/main/resources/gfx</directory>
<includes>
<include>loading.gif</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Assuming you are building a WAR, you could configure the maven-war-plugin to include additional web resources:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource>
<directory>/src/main/resources/gfx</directory>
<includes>
<include>loading.gif</include>
</includes>
<targetPath>img</targetPath>
</resource>
</webResources>
</configuration>
</plugin>

Related

Maven: Filtering files from unpacked-dependencies

I have a script that is unpacked from a dependency that contains an undefined variable. My goal is to set a property in my maven project that defines the variable and filter the file so that the variable in the script is replaced by the property.
I have tried this:
<build>
<resources>
<resource>
<directory>${project.build.directory}/dir1</directory>
<filtering>true</filtering>
<includes>
<include>fileToBeFiltered.sh</include>
</includes>
</resource>
<resources>
</build>
However, this is called before the file is unpacked. So there is nothing to filter yet at the time this runs.
I also tried:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>filter script</id>
<phase>generate-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.build.directory}/dir1</directory>
<filtering>true</filtering>
<includes>
<include>fileToBeFiltered.sh</include>
</includes>
</resource>
<resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
However, its looking inside of src/main/resources instead of the target directory that I provided.
The only thing I can get KINDA working is this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>filter script</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dir2</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/dir1</directory>
<filtering>true</filtering>
<includes>
<include>fileToBeFiltered.sh</include>
</includes>
</resource>
<resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This DOES filter the file, but now the filtered file exists in a different directory than I want it to. I've tried making the outputDirectory the same as the directory it's copying from and just overwrite the original, but it doesn't work. I feel like there is a simple way of doing this that I'm not noticing.
I'm also constrained from modifying the artifact that the dependency is coming from.
I found solution, although it is a bit messy and not my favorite.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>filter script</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/filtered_files</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/dir1</directory>
<filtering>true</filtering>
<includes>
<include>fileToBeFiltered.sh</include>
</includes>
</resource>
<resources>
</configuration>
</execution>
</executions>
</plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>Create filter dir</id>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<mkdir dir="${project.build.directory}/filtered_files" />
</target>
</configuration>
</execution>
<execution>
<id>copy filtered files to original path</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<move todir="${project.build.directory}/dir1" >
<fileset dir="${project.build.directory}/filtered_files"/>
<include name="fileToBeFiltered.sh" />
</fileset>
</move>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This essentially creates a temp directory, uses the 3rd approach described in the original question, then moves the filtered files from the temp directory back to the original path, overwriting the unfiltered file.
Again, not my favorite approach, but it works.

Maven avoid certain files from being copied to test-classes

I'm trying to avoid that certain file types don't end copied in target/test-classes. The code I have right now in my pom file but it's not working is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/test-classes
</outputDirectory>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.nc</exclude>
</excludes>
</testResource>
</testResources>
</configuration>
</execution>
</executions>
</plugin>
I was finally able to do what I wanted with:
<execution>
<id>default-testResources</id>
<phase>test-compile</phase>
<goals>
<goal>testResources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/test/resources </directory>
<excludes>
<exclude>**/*.nc</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
With this code, which is very similar to the one I posted in the question, and by changing the maven version in my eclipse settings from 3.3.3 to 3.3.9 I'm able to avoid the files that I wanted to be copied to target/test-classes

Maven does not copy resource files

I have a maven web application project that follows the maven standard directory layout so I have my resource files placed in src/main/resources/.
As this follows the standard I was expecting maven to automatically add the resource files to /WEB-INF/classes of the web application during the build but no files are copied.
I have to add the following lines to the build section of the pom file to get maven to copy the files:
<resources>
<resource>
<directory>src/main/resources/</directory>
</resource>
</resources>
My question is: is this supposed to be necessary?
I was wondering if any of the build section plugins I use could somehow interfere with the copying of the resources. Are there any plugins that are known to do this?
The build section of my pom file is here:
<build>
<resources>
<resource>
<directory>src/main/resources/</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>${tests.to.include}</include>
</includes>
<excludes>
<exclude>${tests.to.exclude}</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/ITSelenium*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>add extra source directories</id>
<phase>generate-sources</phase>
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>${project.basedir}/generated/src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add extra test directories</id>
<phase>generate-test-sources</phase>
<goals><goal>add-test-source</goal></goals>
<configuration>
<sources>
<source>${project.basedir}/generated/src/test/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add integration test sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/it/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>res</warName>
<webResources>
<resource>
<filtering>true</filtering>
<directory>${project.basedir}/src/main/webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.4.0.905</version>
</plugin>
</plugins>
</build>
Any help or information will be much appreciated.

Move resource files to target and exclude from jar

I have a maven project set up like this:
\src
\--main
\--java
\--fu
\--bar
\--AppMain.java
\--resources
\--log4j.xml
With a pom using the maven-resources-plugin to move my resource files to a target directory like this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>my/target/dir/config</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
And the maven-jar-plugin to create the jar like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>fu.bar.AppMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
When I run mvn clean install, my target location includes the compiled jar and the \config\log4j.xml. However, the jar also includes log4j.xml.
How do I exclude log4j.xml from the jar and still move it to \my\target\dir\config?
Adding this to my pom was the trick:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>

How to unpack a war file copied from local directory using Maven Resources Plugin?

I am trying integrating CAS server with a Spring application. (I have configured CAS locally, in my Tomcat server and it is working properly). Using Maven Resources Plugin I copied the cas.war file to my Web app's target folder. But I need to unpack that cas.war file in order to overwrite some files that I have modified.
How can I unpack this war file which I have copied using Maven Resources Plugin`?
Here is my pom.xml
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>cas</warName>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${maven-jetty-plugin.version}</version>
<configuration>
<webApp>
<contextPath>/cas</contextPath>
</webApp>
</configuration>
</plugin>
<!--Copy plugin-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<!--copy cas.war file-->
<execution>
<id>copy-initial-cas.war</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/cas</outputDirectory>
<resources>
<resource>
<directory>${CAS.LOCATION}/target</directory>
<includes>
<include>cas.war</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<!--copying deployerConfigContext file-->
<execution>
<id>copy-deployerConfigContext</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/cas/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<includes>
<include>deployerConfigContext.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<!--copying web.xml file-->
<execution>
<id>copy-web.xml</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/cas/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<includes>
<include>web.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<!--copying casdatabase.properties file-->
<execution>
<id>copy-casdatabase.properties</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/cas/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<includes>
<include>casdatabase.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--unpacking plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>unzip-war</id>
<phase>install</phase>
<configuration>
<tasks>
<echo message="unpack cas.war" />
<unzip src="${basedir}/target/cas/cas.war" dest="${basedir}/target/cas" overwrite="true"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
If you wish to override resources inside a war, you should be using the maven overlay method.

Resources