Maven YUI Compressor Plugin Causing process-resources phase to not run? - maven

I have a maven project that builds a war file.
Including the yui compressor in my maven build file is causing files, found in src/main/resources/ unrelated to any js files, processed during the process-resources to be empty when they are copied into the target directory. Very weird, indeed. Once the yuicompressor plugin is removed from the cycle, the other resources are processed just fine.
Anyone ever seen that (please, say yes ;-) )?
Here's my config:
The YUI Compressor config:
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
<phase>process-resources</phase>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/extjs*/**/*.js</exclude>
<exclude>**/extjs*/**/*.css</exclude>
</excludes>
<nosuffix>true</nosuffix>
</configuration>
</plugin>
And the Resources config, containing the files that are empty when copied into the target dir:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
To repeat: the files (log4j.xml, etc) in the resources directory are being copied over to the target directory, but they are empty.
Thanks for your help!

What is happening is the YUI Compressor plugin has the resources directory as one of the locations to include for compressing implicitly. It was being run after the resources plugin executed, overwriting the xml and .properties files in the resources dir with empty files (because xml and .properties files don't contain javascript). My fix was to add new excludes to the plugin's config:
<excludes>
<exclude>**/*.xml</exclude> <!-- <-- this one here -->
<exclude>**/*.properties</exclude> <!-- <-- and this one -->
<exclude>**/extjs*/**/*.js</exclude>
<exclude>**/extjs*/**/*.css</exclude>
</excludes>
This is still less than ideal, however, because any resources without an xml or .properties suffix will still be parsed by the yui compressor; I'm back to the original problem.
I tried this exclude, but it didn't work:
<exclude>**/resources/*.*</exclude>
does anyone have any idea why the above wouldn't work, or have an idea how to tell the yui plugin not to process anything in resources?

Related

How do I add a file to the artifact (jar or war) created by maven?

I have an antrun task (goal run on phase prepare-package) set up that creates a file and saves it in /target/foo.bar. How do I add that to the artifact that gets created by maven (depending on module, it could be a jar or a war file)?
I have tried it with resources, with the builder-helper plugin, and the jar plugin - no luck:
<resources>
<resource>
<directory>target</directory>
<includes>
<include>**/foo.bar</include>
</includes>
</resource>
</resources>
That doesn't seem to do anything.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/foo.bar</file>
<type>bar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
According to debug output, this seems to install something extra in the repo, but doesn't add foo.bar to the artefact.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<includes>
<include>../foo.bar</include>
</includes>
</configuration>
</plugin>
No observable effect, either. :(
(I assume this might work for the war file, but I'd prefer one way to do it for both artifact types, if possible - plus I really need this to work for the jars, too...)
Is there a way to do what I want to do?
(I'd prefer not saving my file in src/main/resources first; I thought the packaging processes would pick files up from the build directory after they were placed there anyway, but I suppose I misunderstood something...)
I had my file saved to project.build.directory. Changing that to project.build.outputDirectory means the jar plugin is picking it up, without the need of any other plugins. Unfortunately, that doesn't sort the war issue out... :(
I had a similar problem and stored the resulting artifact in
target/${project.name}-${project.version}/foo.war
To be more precise, I created the war from a directory using the ant target
<war warfile="target/${project.name}-${project.version}/foo.war" basedir="somedir" />
Then it was correctly added to the surrounding ear.

How to exclude certain resources from the maven war plugin war file?

We've been using war:exploded for a while now and I'm trying to do away with it. I've added the following to my pom:
<resource>
<directory>src/main/webapp</directory>
</resource>
This copies files such as the in src/main/webapp/keystore into target/classes/keystore so my local UI launcher works and sees everything. Score!
However, by adding to the resources list, this means that the same files also show up in the war file as keystore (correct) and WEB-INF/classes/keystore (wrong). It also means there there is a WEB-INF/classes/WEB-INF directory (blah). I'm trying to exclude the resource files from src/main/webapp resource since src/main/webapp/WEB-INF is already a resource.
I'm trying not to specifically exclude keystore and other files since we add/delete from that list semi-often. I've tried to add the following (and a number of other variants) to the war plugin configuration without results:
<webResources>
<resource>
<directory>src/main/webapp</directory>
<excludes>
<exclude>**</exclude>
</excludes>
</resource>
</webResources>
I've also read a number of other SO questions and I've spent at least an hour reading docs on from the maven war plugin page and tried other configs without success.
Any idea what magic I need to do here? Thanks in advance.
You need
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<packagingExcludes>**/keystore</packagingExcludes>
</configuration>
</plugin>
Also See
Including and Excluding Files From the WAR
warSourceExcludes worked for me. for example.To exclude js folder and scss folder from my war i tried...
<configuration>
<warSourceExcludes>js/*.*,scss/*.*</warSourceExcludes>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
<includes>
<include>WEB-INF/web.xml</include>
</includes>
</resource>
</webResources>
</configuration>
However, by adding to the resources list, this means that the same files also show up in the war file as keystore (correct) and WEB-INF/classes/keystore (wrong).
I was not able to get the war-plugin to properly exclude the webapp directory while at the same time allowing the webapp/WEB-INF directory. Instead of adding the following to the pom:
<!-- bad idea, this didn't work -->
<resource>
<directory>src/main/webapp</directory>
</resource>
I switched to using the resources-plugin to be able to copy certain resources over during the validate phase:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/webapp</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I initially copied the webapp directory into target/classes but that also seemed to make it into the resulting war file which was what I was trying to avoid. I have the feeling that I could have solved this if necessary.
Instead I decided to copy the webapp files into the target/webapp directory and change my program to look for the resources there:
String RESOURCE_BASE = "target/webapp";
System.setProperty("server.jetty.webapp.resourceBase", RESOURCE_BASE + "/");
System.setProperty("server.jetty.ssl.keystore", RESOURCE_BASE + "/keystore");
System.setProperty("server.jetty.ssl.truststore", RESOURCE_BASE + "/keystore");
...
This seems to be working well. Testing locally now works (without war-exploded!!) and the files are not doubly included in the resulting war file. In addition, I added a symlink at runtime from target/webapp/views/cms to point to our CMS hierarchy which is in another project.

Configure maven-shade-plugin to include src/main/webapp

I'm working with a simple project, with the webapp directory in the standard location (src/main/webapp). For some reason, it looks like the maven-shade-plugin doesn't include that in the final generated jar - it will only include artifacts src/main/resources by default.
I have tried to use the following configuration, with no luck:
<configuration>
<artifactSet>
<includes>
<include>*</include>
<include>*:*</include>
</includes>
</artifactSet>
</configuration>
Any suggestions on how to do that without having to move src/main/webapp into src/main/resources/webapp?
Thank you.
Eugen.
Have you tried to update your build section with your resource path ?
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
</build>
EDIT
As already said, shade purpose is not to war packages.
You need to put it in src/main/resources/webapp or specify the appropriate location explicitly for it to be caught up properly. Annoying, but it works.
For those asking why this kind of thing makes sense to do, it's a typical setup for a jar with an embedded web server like Jetty.
I use the resource plugin to move the webapp to the classes folder.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/webapp</outputDirectory>
<resources>
<resource>
<directory>src/webapp/dist</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This way I can place the folder with my webapp anywhere, define the destination directory and it is included in my shaded jar.
It sounds you would like to package sources into a package. There exists a maven-source-plugin which will create a jar which contain the source files. Furthermore this is usually done during a release cycle. As already mentioned the maven-share-plugin intention is to package jar files together (their contents; keyword: ueberjar).
I would use the war plugin http://maven.apache.org/plugins/maven-war-plugin/faq.html#attached to create a jar of a webapp project. You have already set <packaging>war</packaging>?
What's the point in adding your webapp resources into a JAR? That's WAR-like.
Change your packaking type to war and it will work.

how to assemble external artifacts into one global directory with the maven-assembly-plugin (or otherwise)

I am experimenting with Maven and I am trying to mavenize a project originally build with shell scripts.
With the Maven rule-of-thumb: one project, one artifact, I created the following structure:
<PROJECT>
<MODULE-1>
<MODULE-2>
<MODULE-3>
..
<MODULE-N>
<RESOURCES>
<DISTRIB>
The RESOURCES module is structured as follows:
<RESOURCES>/src/main/resources/<MODULE-1>/bin
<RESOURCES>/src/main/resources/<MODULE-1>/lib
<RESOURCES>/src/main/resources/<MODULE-1>/doc
<RESOURCES>/src/main/resources/<MODULE-2>/bin
<RESOURCES>/src/main/resources/<MODULE-2>/lib
<RESOURCES>/src/main/resources/<MODULE-2>/doc
...
<RESOURCES>/src/main/resources/<MODULE-N>/bin
<RESOURCES>/src/main/resources/<MODULE-N>/lib
<RESOURCES>/src/main/resources/<MODULE-N>/doc
The reason for doing it this way was that the resources above are needed at runtime, not compile-time and they are mostly property files, config files and shell scripts to invoke the various jar-files. For the final resources step, I wanted to combine the subdirectories into one global bin/lib/doc directory. However, I do not see an option in the assembly descriptor to strip of the prefix of the modules to get to what I want:
<RESOURCES>/target/resources/bin
<RESOURCES>/target/resources/lib
<RESOURCES>/target/resources/doc
where bin would contain all the files found in the /src/main/resources//bin directory, /src/main/resources//bin directory etc. Similarly for lib, doc.
My question: should I have a:
<MODULE-1>/src/external/resources/bin
<MODULE-1>/src/external/resources/lib
<MODULE-1>/src/external/resources/doc
...
<MODULE-N>/src/external/resources/bin
<MODULE-N>/src/external/resources/lib
<MODULE-N>/src/external/resources/doc
structure instead, that I would then access through a dependency-set? These files should not be part of the jar-file of the various modules and therefore cannot be stored in src/main/resources of their respective projects. Or is what I want to achieve doable by using some other maven plugin instead?
In the DISTRIB module I would combine the output of the RESOURCES module with the JAR-files and dependencies to create a directory structure that would then be used with a packaging tool (Solaris package and WiX installer).
Any help would be appreciated!
In the pom fort he resources module set the source directory to something other than /src/main/resources/ so that it does not copy them to the target folder. Then use the Maven Resources Plugin to copy the resource files to target/bin and target lib etc.
e.g.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/bin/</outputDirectory>
<resources>
<resource>
<directory>src/external/resources/bin</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/lib/</outputDirectory>
<resources>
<resource>
<directory>src/external/resources/lib</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>`

In Maven how to exclude resources from the generated jar?

When I create an executable jar with dependencies (using this guide), all properties files are packaged into that jar too. How to stop it from happening? Thanks.
UPDATE: I tried to exclude them using the Maven resources plugin, but then my application won't find the properties files when I run it in Eclipse (right click on the module -> Run As -> Java Application)
UPDATE: Thanks for your useful answers. I think I'd better spend time to learn Maven, for now I just choose the simplest solution.
To exclude any file from a jar / target directory you can use the <excludes> tag in your pom.xml file.
In the next example, all files with .properties extension will not be included:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
</build>
By convention, the directory src/main/resources contains the resources that will be used by the application. So Maven will include them in the final JAR.
Thus in your application, you will access them using the getResourceAsStream() method, as the resources are loaded in the classpath.
If you need to have them outside your application, do not store them in src/main/resources as they will be bundled by Maven. Of course, you can exclude them (using the link given by chkal) but it is better to create another directory (for example src/main/external-resources) in order to keep the conventions regarding the src/main/resources directory.
In the latter case, you will have to deliver the resources independently as your JAR file (this can be achieved by using the Assembly plugin). If you need to access them in your Eclipse environment, go to the Properties of your project, then in Java Build Path in Sources tab, add the folder (for example src/main/external-resources). Eclipse will then add this directory in the classpath.
This calls exactly for the using the Maven JAR Plugin
For example, if you want to exclude everything under src/test/resources/ from the final jar, put this:
<build>
<plugins>
<!-- configure JAR build -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<excludes>
<exclude>src/test/resources/**</exclude>
</excludes>
</configuration>
</plugin>
...
Files under src/test/resources/ will still be available on class-path, they just won't be in resulting JAR.
Put those properties files in src/test/resources. Files in src/test/resources are available within Eclipse automatically via eclipse:eclipse but will not be included in the packaged JAR by Maven.
Exclude specific pattern of file during creation of maven jar using maven-jar-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.exe</exclude>
<exclude>**/*.java</exclude>
<exclude>**/*.xls</exclude>
</excludes>
</configuration>
</plugin>
Do you mean to property files located in src/main/resources? Then you should exclude them using the maven-resource-plugin. See the following page for details:
http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
Another possibility is to use the Maven Shade Plugin, e.g. to exclude a logging properties file used only locally in your IDE:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin-version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>log4j2.xml</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
This will however exclude the files from every artifact, so it might not be feasible in every situation.
When I create an executable jar with dependencies (using this guide), all properties files are packaged into that jar too. How to stop it from happening? Thanks.
Properties files from where? Your main jar? Dependencies?
In the former case, putting resources under src/test/resources as suggested is probably the most straight forward and simplest option.
In the later case, you'll have to create a custom assembly descriptor with special excludes/exclude in the unpackOptions.
here is another solution to exclude all files in resources folder, the final configuration looks like:
<build>
<!-- exclude all files in resources-->
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/**</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<!-- other configurations/plugins in the pom.xml-->
</build>
or we can use includes to only package some file or folder.
But this method has a side effect. IDE will also exclude the resource files in target/classes folder. maven-jar-plugin only affect jar file.
I found a better solution to execludes resourses folder using maven-jar-plugin, here we use includes:
<build>
<plugins>
<!-- configure JAR build -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
</configuration>
</plugin>
...

Resources