can you provide overwrite on a per resource basis using the maven-resources-plugin? - maven

So I'm having a problem with sharing a resources root because of IntelliJ, We have some resources we already add with the resource plugin, some of these resources I do not want to overwrite for performance reasons. In other cases I would like them overwritten So we don't forget we have to delete them if we change them. Can I just specify (mostly copy-pasta-ed from the official documentation
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<overwrite>true</overwrite> <!-- will this work -->
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
in the resources I want to overwrite? really this question is simple Can I put overwrite directly on the resource? Will it work?

Related

Rename directory while building war-file with Maven

Question
Overall Goal
I want to build a Java WebApp (war file) where folders containing static content have a version number append to it for cachability. Renaming should happen automatically during build.
Details
The WebApp is already structured in a way that all static content is grouped together inside folders:
/lib -> All JavaScript libraries, eg /lib/angular-1.0.3 (source at src/main/webapp)
/app -> My Own Code, at the moment /app/myApp (source at src/main/webapp)
/api -> all dynamic servlets (source at src/main/java)
index.jsp -> host page that bootstraps the WebApp
Everything starting with /api and index.jsp is dynamic and not cachable at all.
Everything starting with /lib is static and versioned, thus we can set an expires header of 'access + 12 month'. Content changes require a new version number. This is easy with libraries, as they don't change much an the version number is hardcoded.
Since my own app (built with AngularJS) is also static, I want to set its expires header to 'access + 12 month', too. To do so, I need to add the current version number from Maven to the end of the folder name, thus turning /app/myApp from the source directory to /app/myApp-1.2.3 in the target directory / final war file.
POM File
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>jslint</goal>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeResources>true</excludeResources>
<removeIncluded>true</removeIncluded>
<suffix>.min</suffix>
<excludes>
<exclude>**/*.xml</exclude>
<exclude>**/*.properties</exclude>
<exclude>**/lib/**/*.js</exclude>
<exclude>**/lib/**/*.css</exclude>
</excludes>
<aggregations>
<aggregation>
<insertNewLine>true</insertNewLine>
<output>${project.build.directory}/${project.build.finalName}/app/myApp/js/all.min.js</output>
<includes>
<include>${project.basedir}/src/main/webapp/app/myApp/js/header.txt</include>
<include>**/*.min.js</include>
</includes>
</aggregation>
</aggregations>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.1</version>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreMissingFile>false</ignoreMissingFile>
<file>${project.build.directory}/myApp/index.jsp</file>
<replacements>
<replacement>
<token>PROJECT_VERSION</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>
I am looking for a way to copy all ressources to the target-directory, run yuicompressor, update the links and rename app/myApp to app/myApp-x.y.z
Compressing and updating the links already, but I can't find a way to rename my own app at build time.
Thanks
Solution
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>jslint</goal>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeResources>true</excludeResources>
<removeIncluded>true</removeIncluded>
<suffix>.min</suffix>
<failOnWarning>true</failOnWarning>
<excludes>
<exclude>**/*.xml</exclude>
<exclude>**/*.properties</exclude>
<exclude>**/lib/**/*.js</exclude>
<exclude>**/lib/**/*.css</exclude>
</excludes>
<sourceDirectory>${project.basedir}/src/main/webapp/app/myApp</sourceDirectory>
<outputDirectory>${project.build.directory}/${project.build.finalName}/app/myApp-${project.version}</outputDirectory>
<aggregations>
<aggregation>
<insertNewLine>true</insertNewLine>
<output>${project.build.directory}/${project.build.finalName}/app/myApp-${project.version}/js/all.min.js</output>
<includes>
<include>${project.basedir}/src/main/webapp/app/myApp/js/header.txt</include>
<include>**/*.min.js</include>
</includes>
</aggregation>
</aggregations>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
<configuration>
<useCache>true</useCache>
<!-- this exclude the directory to rename from the default copy resources procedure -->
<warSourceExcludes>app/myApp/**,.idea/**</warSourceExcludes>
<!-- this will do the renaming : i.e. we specify the source directory relative to pom.xml and the target directory relative to root -->
<webResources>
<resource>
<directory>src/main/webapp/app/myApp</directory>
<excludes>
<exclude>**/*.js</exclude>
</excludes>
<targetPath>/app/myApp-${project.version}</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreMissingFile>false</ignoreMissingFile>
<file>${project.build.directory}/myApp/index.jsp</file>
<replacements>
<replacement>
<token>%PROJECT_VERSION%</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>
</plugins>
I think you can do it by configuring a <webResources> element in your maven-war-plugin config.
Doing something like this should work
<configuration>
<!-- this exclude the directory to rename from the default copy resources procedure -->
<warSourceExcludes>app/myApp/**</warSourceExcludes>
<!-- this will do the renaming :
i.e. we specify the source directory relative to pom.xml
and the target directory relative to root -->
<webResources>
<resource>
<directory>src/main/webapp/app/myApp</directory>
<!-- override the destination directory for this resource -->
<targetPath>app/myApp-${project.version}</targetPath>
</resource>
</webResources>
</configuration>
EDIT
I'm not used to work with yucompressor plugin but it seems that you can easily change the output directory through <outputDirectory> element:
<outputDirectory>${project.build.directory}/${project.build.finalName}/app/myApp-${project.version}/js/all.min.js</outputDirectory>
If all your code is in all.min.js (I think that's what the yucompressor do, but I'm not sure) : then you don't need the <webResources> section in the war plugin, but you have to keep <warSourceExcludes>app/myApp/**</warSourceExcludes> to avoid duplication.
EDIT 2
As an answer to your comment.
Try the following:
use yucompressor for *.css and *.js as indicated in previous EDIT
exclude /myapp from usual war-plugin copy resources procedure using <warSourceExcludes>app/myApp/**</warSourceExcludes>
use a webResources section to include all you non *.js and non *.css to myApp-${project.version}:
<webResources>
<resource>
<directory>src/main/webapp/app/myApp</directory>
<excludes>
<exclude>**/*.js</exclude>
<exclude>**/*.css</exclude>
</excludes>
<!-- override the destination directory for this resource -->
<targetPath>app/myApp-${project.version}</targetPath>
</resource>
</webResources>

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.

How to use <includes> / <excludes> in maven-remote-resources-plugin

I am trying to use the maven-remote-resources-plugin as per this example to selectively share common resources between multiple maven modules and I'm having a lot of difficulty getting the selective import of the resources to work.
I am trying to use <includes> and <excludes> elements as per below. I haven't seen these mentioned in doco for the plugin anywhere but eclipse provides them as valid options in the command completion and I don't get any errors when I run the pom. So far I haven't been able to get <includes> or <excludes> to have any effect at all on the imported resources
The relevant sections of my pom are;
Shared resources
<build>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
Resource consumer
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.3</version>
<configuration>
<resourceBunldes>
<resourceBundle>myApp:myApp_sharedresources:${project.version}</resourceBundle>
</resourceBundles>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<includes>
<include>theOnlyResourceIWant.properties</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<phase>generate-resources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>myApp</groupId>
<artifactId>myApp_sharedresources</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
I've tried many combinations of <includes> and <excludes> but all so far have had no impact.
So, are
<includes></includes>
and
<excludes></excludes>
valid elements for a maven-remote-resources-plugin configuration, and how do I use them?
I can reasonably seperate the resources out into seperate maven modules, but that could create a large number of single file maven modules and add a lot of extra xml so I'd like to avoid it if possible.
I'd really rather not start pawing through the plugin source code, but that is the next step.
I use a temp directory for the shared resources I'm importing and filter that.
Remote resource plugin configuration below. This copies all the shared resources into a temp directory in your project. Setting attached to false means they are not included in your final project artifact, which gives you the opportunity to select the ones you want to include using Maven's normal resource processing.
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<configuration>
<resourceBundles>
<resourceBundle>myApp:myApp_sharedresources:${project.version}</resourceBundle>
</resourceBundles>
<attached>false</attached>
<outputDirectory>${project.build.directory}/shared-resources</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<phase>generate-resources</phase>
</execution>
</executions>
</plugin>
Resource definition. When the maven-resource-plugin runs (it is bound to the lifecycle by default for jars/wars/ears), it will use the shared resource directory as well as the normal src/main/resources dir. You need to define both. (You may also enable resource filtering if you want.)
<resources>
<resource>
<directory>${project.build.directory}/shared-resources</directory>
<includes>
<include>theOnlyResourceIWant.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
I recommend making the shared directory be a subdirectory of ${project.build.directory}, so the clean lifecycle works without changes.
To enable the filter delimiters for the format '#{expr}' (Ruby-style), add the following to your plugin configuration:
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>process-remote-resources</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<filterDelimiters>
<filterDelimiter>#{*}</filterDelimiter>
</filterDelimiters>
[...]
</configuration>
</execution>
</executions>
</plugin>
Check this link for reference

mvn clean package, I want to copy the created jar to the current directory

I want to update my pom.xml so that when someone uses: mvn clean package, the generated jar file is copied to the current directory. I'm looking at the maven plugin copy-resources, but i'm not sure how to specify the current directory, is there an operating system agnostic way to do this? Would like it to also work on windows, if possible
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}</outputDirectory>
<resources>
<resource>
<directory>target/Test-0.0.1-SNAPSHOT.jar</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Thanks
If by current directory you mean ${project.basedir} then yes you can do this easily. Just make sure you use ${build.finalName}.jar as the file name part as it will properly get the main artifact of a project with <packaging> set to jar (the default).
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}</outputDirectory>
<resources>
<resource>
<!-- Get main artifact -->
<directory>target/${build.finalName}.jar</directory>
<!-- Don't filter binary files -->
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
If you wanted to do the current working directory instead you should be able to do it using ${user.dir} as the outputDirectory

Setting pom properties from Maven Plugin for resource filtering

I have a conf file in a java app that contains an IP address parameter. I want to be able to put in this parameter the local ip address automatically at build time. I used maven resources-plugin as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/home/user/config</outputDirectory>
<resources>
<resource>
<directory>config</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
Next, I've created a property that contains the parameter
<properties>
<gateway.ip>${local.ip}</gateway.ip>
</properties>
Then, I've created a Maven plugin that gets the local ip address and sets the above parameter:
final Properties props = mavenProject.getProperties();
props.put("local.ip", resultAddress.getHostAddress());
lastly, I define my custom plugin in the pom.xml:
<plugin>
<groupId>com.company</groupId>
<artifactId>get-local-ip-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<executions>
<execution>
<id>get-local-ip</id>
<phase>validate</phase>
<goals>
<goal>get-local-ip</goal>
</goals>
<configuration>
<localIp>${local.ip}</localIp>
</configuration>
</execution>
</executions>
</plugin>
The problem is that this doesn't work and I get ${local.ip} in the resulting file instead of xxx.xxx.xxx.xxx ip address.
Any suggestions?
Answered in comments:
found my bug :) - I bound both plugins to the validate phase (the good
old copy-paste) – rperez Jun 22 '11

Resources