How to tell YUI compressor maven plugin to output to WAR file and/or source directories? - maven

I'm trying to add YUI compressor to my JSF eclipse maven project. I'm not a maven professional, I just use the m2e maven plugin to resolve my dependencies and to right click my project und export it to a .WAR file. At this point, the YUI compressor plugin is working and compresses all css and javascript files as soon as I change and save a resource (i.e. a html file). My problem is, that it's neither putting the compressed files back to MyProject/src/main/webapp/resources/css|js nor into the resulting .WAR file. It puts them into MyProject/target/app-0.01-SNAPSHOT/resources/css|js which seems to be of no (?) use. How can I set it up to put the files into the .WAR file and into the original input-directories (so i can see and include them in my html files)? I have read a couple of threads here, but couldn't find the solution.
Here is the relevant part from my pom.xml:
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<!-- I was playing around with this. At this point, the following does the same as the default
setting, i.e. putting the files into the /target/ directory, which is not what i want/need. -->
<configuration>
<sourceDirectory>${project.basedir}/src/main/webapp/resources/js</sourceDirectory>
<outputDirectory>${project.build.directory}/${project.build.finalName}/resources/js</outputDirectory>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<versionRange>[1.3.0,)</versionRange>
<goals>
<goal>compress</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>true</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

Related

appengine-maven-plugin not auto deploying code after code changes

I've a appengine maven project which uses the newly recommended module structure. So I've a ear module which in turn contains 2 war sub modules. I'm using run mvn appengine:devserver from ear directory to run the code. I want maven to deploy any code change as soon as I save it so I can refresh the browser and see the changes but that doesn't seem to work. Here's my ear pom.
target/${project.artifactId}-${project.version}/*/WEB-INF/classes
org.apache.maven.plugins
maven-ear-plugin
2.8
5
lib
war
com.google.appengine
appengine-maven-plugin
${appengine.target.version}
2
<dependencies>
<dependency>
<groupId>com.blah.app</groupId>
<artifactId>A</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.blah.backend</groupId>
<artifactId>B</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
Following the recommendation on https://developers.google.com/appengine/docs/java/tools/maven I've added in the buildOuputput directory under build directive and also specified
<configuration>
<fullScanSeconds>2</fullScanSeconds>
</configuration>
under appengine-maven-plugin plugin. I've also enabled compile on save option in netbeans but maven doesn't seem to be scanning the classes folder and deploying the changes while devappserver is running.
Right now I'm stuck in clean build/deploy cycle for every small change. I'd really appreciate any help on this.
I managed to get it working in Eclipse by calling war:exploded from compile phase and adding a mapping in m2e configuration, so that it runs it in incremental builds in Eclipse. I am not sure how that would work in Netbeans, but maybe my solution for Eclipse will help you.
Here are the relevant portions of my pom:
This is the part that configures war:exploded execution:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<archiveClasses>true</archiveClasses>
<webResources>
<!-- in order to interpolate version from pom into appengine-web.xml -->
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
</plugin>
This is the part configures m2e (it goes in the build section of pom.xml):
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-war-plugin
</artifactId>
<versionRange>
[2.4,)
</versionRange>
<goals>
<goal>
exploded
</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>true</runOnIncremental>
<runOnConfiguration>true</runOnConfiguration>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>

Maven: use jar from URL as a source for resources

Dealing with a legacy project, I have the need to load text resources from a jar at an URL.
The text resources will be then filtered and included in the output; those resources come from a released artifact.
From resource-plugin I see it is only possible to give a number of directories; would it be possible to load resources as I need?
I want to do somthing like this, but using a remote jar instead of the oher project in the workspace:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
<resources>
<resource>
<directory>../<another project on the same workspace>/src/main/filtered-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Remote resource plugin, as suggested in one of the answer doesn't work because no file from the imported bundle ends up in target; there is no way I can produce the original bundle using remote resource plugin (it's a legacy projetc still in use and completely out of my control).
I think the Maven Remote Resources Plugin will suit your needs.
EDIT:
Snippet obtained from the usage page of the plugin. That XML fragment will attach the plugin to the generate-sources phase (choose a different one if it doesn't fit your needs), will download the apache-jar-resource-bundle artifact and uncompress its contents into ${project.build.directory}/maven-shared-archive-resources.
For better results is recommended that the resources artifact had been created using the bundle goal of the same plugin.
<!-- Turn this into a lifecycle -->
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>process-remote-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<resourceBundles>
<resourceBundle>org.apache:apache-jar-resource-bundle:1.0</resourceBundle>
</resourceBundles>
</configuration>
</execution>
</executions>
</plugin>
EDIT 2: Alternative Solution using AntRun
If your artifacts don't suit Maven needs and you need something more customized, then using AntRun plugin you could get it somehow:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>download-remote-resources</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<get src="URL of the resource" dest="${project.build.directory}" />
<unzip src="${project.build.directory}/filename.[jar|zip|war]" dest="${project.build.directory}/${project.build.finalName}" />
</target>
</configuration>
</execution>
</executions>
</plugin>

Jacoco Maven Plugin - Plugin execution not covered by lifecycle configuration

I'm new to Maven and want to use the Jacoco Maven Plugin to build my projects.
I've set up an example project with TestNG the only dependency.
Here is part of the pom.xml:
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.2.201302030002</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And i get this error:
Plugin execution not covered by lifecycle configuration: org.jacoco:jacoco-maven-
plugin:0.6.2.201302030002:prepare-agent (execution: default, phase: initialize)
What am I doing wrong ?
Cheers
You can ignore the plugin goal, adding something like this to your pom.xml
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only.
It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<versionRange>[0.5,)
</versionRange>
<goals>
<goal>prepare-agent</goal>
</goals>
</pluginExecutionFilter>
<action>
<!-- m2e doesn't know what to do with jacoco,
let's ignore it or annoying error markers appear
see http://wiki.eclipse.org/M2E_plugin_execution_not_covered
-->
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
As this is related to the Eclipse Maven plugin, alternatively this can be set locally in Eclipse's preferences. Moving the configuration out of the project's pom file helps the code simple and clean, free of IDE particulars.
Go to Eclipse --> Preferences --> Maven --> Lifecycle Mappings. Add lifecycle-mapping-metadata.xml as the following:
<?xml version="1.0" encoding="UTF-8"?>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<versionRange>[0.5,)</versionRange>
<goals>
<goal>prepare-agent</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
Reload the life-cycle mappings file and then Maven --> Update Project
Eclipse now offers a quick fix to disable the warning and save those in user preferences (in Eclipse --> Preferences --> Maven --> Lifecycle Mappings lifecycle-mapping-metadata.xml as noted by #iker-aguayo ) so you don't have to manually create or edit the file. This is useful in cases where you can't update the pom (such as using an open source project where you cannot commit.)
I eventually chose to ignore the plugin and use the CLI mvn command instead for the test with code coverage.
Inside your Eclipse IDE, right click on the red color-marked warning for the
jacoco-maven-plugin. You should three options in the popup to fix the warning, choose to ignore the warning, that would result an automatically generated section in your pom.xml, that started with a line of comments,
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
At a command line, run the mvn command before each checkin, and that should trigger the test goal with coverage,
$mvn clean package
This problem is specific to Eclipse, as outlined on the M2E wiki. Sorry I can't help more than that, as I don't use Eclipse.

How do I include test classes and configuration in my war for integration testing using maven?

I currently have a maven web project that I am attempting to write integration tests for. For the structure of the project, I've defined test stubs under src/test/java, whilst the spring bean definitions for these stubs sit under src/test/resources.
What I would like to do, is that when I build my war artifact I'd like all of the test stub classes to be compiled and included in the war along with the spring bean definition files. I've tried to do it with the maven war plugin but the only things I've been able to copy are the resources. Simply put, I'd like to make use of the test class path and include all these classes in my war file.
It seems the useTestClassPath option with the maven jetty plugin would solve my problem but the current project I'm working on is currently using Tomcat 6.0. Is there another maven plugin or a way I can configure the maven war plugin to achieve my objective?
You can also do it straightforwardly. This will add both test classes and test resources to the WEB-INF/classes:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>process-test-classes</phase>
<configuration>
<target>
<copy todir="${basedir}/target/classes">
<fileset dir="${basedir}/target/test-classes" includes="**/*" />
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I also recommend you place it into separate profile like "integration" and also to override the package name in that profile to not be able to confuse normal war without tests packaged in and the testing war.
The full example with profile is here. You may run mvn clean package to have a war war-it-test.war without tests included, or you may run mvn clean package -Pintegration to have a war war-it-test-integration.war for the war with tests included.
I believe the following configuration for the maven war plugin would do what you want. You copy your test-classes to your WEB-INF/classes folder. You can even filter those resources.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>generate-test-war</id>
<phase>pre-integration-test</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
<configuration>
<warSourceDirectory>${basedir}/src/test/webapp</warSourceDirectory>
<warName>${project.artifactId}-test</warName>
<webappDirectory>${basedir}/target/${project.artifactId}-test</webappDirectory>
<primaryArtifact>false</primaryArtifact>
<webResources>
<resource>
<directory>${basedir}/target/test-classes</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
See http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
You can use the maven build helper plugin to add additional folders to the "normal" class path.
But I would recommend to create an new folder for your integration test (for example src/it/java), and add this folder, but not the "normal" test folder (src/test/java) -- the same for the resources folder.
Instead of using the maven antrun plugin, you could instead use the maven resources plugin and configure it like this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>process-test-classes</phase>
<id>test-classes</id>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>false</overwrite>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/test-classes</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Use Tomcat 7 plugin with additional classpath directories configuration.
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<additionalClasspathDirs>
<additionalClasspathDir>${build.testOutputDirectory}</additionalClasspathDir>
</additionalClasspathDirs>
</configuration>
<executions>
<execution>
<id>start-tomcat</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<fork>true</fork>
</configuration>
</execution>
</executions>
</plugin>
You can do this Configuration in pom.xml file you don't get any errors in pom.xml and adding test classes to our jar or war file.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.7,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>process-test-classes</phase>
<configuration>
<target>
<copy todir="${basedir}/target/classes">
<fileset dir="${basedir}/target/test-classes" includes="**/*" />
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
We need to add the below plugin to the pom.xml in order to add the test cases to jar. Thanks to #IvonSopov
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-test-classes</phase>
<configuration>
<target>
<copy todir="${basedir}/target/classes">
<fileset dir="${basedir}/target/test-classes" includes="**/*" />
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
But after adding this line we got the build succeeded and also able to add the test case classes into the jar.. but the problem is in pom.xml it is showing as error like
Plugin execution not covered by lifecycle configuration:
org.apache.maven.plugins:maven-antrun-plugin:1.8:run
(execution:default, phase: process-test-classes)
In order to remove this error we need to include the below plugin as a separate tag within the build tag. (not inside the plugins which we added earlier.)
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.7,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
Now we can create the jar which includes the test classes without any errors.

How can I generate Flash HTML wrappers in a WAR using Flexmojos and Maven 3?

I have two Maven projects: one to build a SWF and another one to build a WAR that contains it. The WAR needs to contain a Flash wrapper for the SWF. Flexmojos is apparently capable of doing this via the HTML wrapper mojo, but this doesn't work with Maven 3. See here for more details.
Is there a workaround for this?
My SWF POM has the following:
<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-maven-plugin</artifactId>
<version>4.1-beta</version>
<configuration>
<parameters>
<swf>${project.artifactId}-${project.version}</swf>
<title>My application title</title>
<width>100%</width>
<height>100%</height>
<bgcolor>#ffffff</bgcolor>
</parameters>
</configuration>
</plugin>
and my WAR POM has the following:
<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-maven-plugin</artifactId>
<version>4.1-beta</version>
<extensions>true</extensions>
<executions>
<execution>
<id>wrapper</id>
<phase>generate-resources</phase>
<goals>
<goal>wrapper</goal>
</goals>
<configuration>
<wrapperArtifact>
<groupId>${project.groupId}</groupId>
<artifactId>the-swc-project</artifactId>
<version>${project.version}</version>
</wrapperArtifact>
<htmlName>index</htmlName>
</configuration>
</execution>
<execution>
<goals>
<goal>copy-flex-resources</goal>
</goals>
</execution>
</executions>
</plugin>
Upon further investigation, I discovered that the problem here is that Flexmojos tries to reconfigure the WAR plugin, but it does so in a way that doesn't work with Maven 3.
The fairly grim workaround that I found involved manually configuring the WAR plugin as follows:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<warSourceExcludes>index.template.html</warSourceExcludes>
<webResources>
<resource>
<directory>target/war/work/wrapped-template</directory>
</resource>
</webResources>
</configuration>
</plugin>

Resources