How can I stop a default plugin execution? - javascript

I'm using a javascript maven plugin. I like the war-package goal and want to use it.
However it also comes with a jsunit goal bound to the test phase. This part of the plugin isn't great so I want to just turn it off. How can I do this?
Plugin xml is below.
Edit : Have tried adding executions with phase none, but the goals still run.
<plugin>
<groupId>org.codehaus.mojo.javascript</groupId>
<artifactId>javascript-maven-plugin</artifactId>
<version>1.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>copyDependenciesForJasmine</id>
<phase>compile</phase>
<goals>
<goal>war-package</goal>
</goals>
<configuration>
<webappDirectory>${project.build.directory}/javascript</webappDirectory>
<scriptsDirectory>src</scriptsDirectory>
<libsDirectory>../dependencies</libsDirectory>
</configuration>
</execution>
<execution>
<id>jsunit</id>
<phase>none</phase>
</execution>
<execution>
<id>prepare-tests</id>
<phase>none</phase>
</execution>
</executions>
</plugin>

A similar question was already asked here. You may just specify execution phase to none on the specific plugin.

Related

How to copy individual files with Maven Resource Plugin

I know it's possible to copy the content of directory and apply filters, but is it possible to specify individual files in arbitrary directory to be copied using Maven Resource Plugin, or should I resort plugins like antrun?
EDIT: problem solved, read my answer
This configuration did it for me
<execution>
<id>copy-html-and-images-to-dist</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/frontend/dist</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${basedir}/src/main/frontend/src</directory>
<includes>
<include>index.html</include>
<include>images</include>
<include>favicon.ico</include>
</includes>
</resource>
</resources>
</configuration>
</execution>

How can I change the lifecycle mapping in javascript-maven-plugin

Now, I using javascript-maven-plugin to develop my project.
In the documents, the lifecycle mapping is as follows:
http://mojo.codehaus.org/javascript-maven-tools/javascript-maven-plugin/extension-information.html
I want to import other library of files installed in repository of maven.
I expected that jslint-maven-plugin lifecycle was called after js-import-maven-plugin lifecycle.
However, the result turn out to be error, because the real lifecycle is as follows:
validate: Not defined
initialize: Not defined
generate-sources: Not defined
process-sources: org.codehaus.mojo:jslint-maven-plugin:1.0.1:jslint, org.codehaus.mojo:js-import-maven-plugin:1.0.1:import-js
generate-resources: org.codehaus.mojo:js-import-maven-plugin:1.0.1:generate-html
process-resources: org.apache.maven.plugins:maven-resources-plugin:2.5:resources
compile: Not defined
process-classes: Not defined
generate-test-sources: Not defined
process-test-sources: org.codehaus.mojo:jslint-maven-plugin:1.0.1:test-jslint, org.codehaus.mojo:js-import-maven-plugin:1.0.1:test-import-js
generate-test-resources: org.codehaus.mojo:js-import-maven-plugin:1.0.1:test-generate-html
process-test-resources: org.apache.maven.plugins:maven-resources-plugin:2.5:testResources
test-compile: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile
process-test-classes: Not defined
test: org.apache.maven.plugins:maven-surefire-plugin:2.9:test
prepare-package: org.codehaus.mojo:webminifier-maven-plugin:1.0.1:minify-js
package: Not defined
pre-integration-test: Not defined
integration-test: Not defined
post-integration-test: Not defined
verify: Not defined
install: org.apache.maven.plugins:maven-install-plugin:2.3.1:install
deploy: org.apache.maven.plugins:maven-deploy-plugin:2.5:deploy
Now, I want to change the lifecycle mapping by editing the pom file like,
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jslint-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>default-jslint</id>
<phase>process-resources</phase>
</execution>
</executions>
</plugin>
However, it doesn't work.
How can I change the lifecycle mapping?
I’m not sure, but when you wanna change the phase, perhaps you must also specify the goal. Try this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jslint-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>default-jslint</id>
<phase>process-resources</phase>
<goals>
<goal>jslint</goal>
</goals>
</execution>
</executions>
</plugin>

Compressing everything not just the source directory with Maven

I am using maven to compress my javascript files using YUI compressor.
I have the aggregation work ing and only combining what in want. However i need YUI to compress one folder in my web apps folder. I have specified the source folder but this seems to be ignored. Instead every .js file in src/main/webapps seems to be compressed. I need to leave existing files intact and only compress my files in the directory.
Here is my code:
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<preProcessAggregates>true</preProcessAggregates>
<aggregations>
<aggregation>
<insertNewLine>true</insertNewLine>
<output>${project.basedir}/target/umadesktop/angular/app/single.js</output>
<inputDir>${project.basedir}/src/main/webapp/angular/app</inputDir>
<includes>
<include>**/*.js</include>
</includes>
<excludes>
<exclude>**/*abc.js</exclude>
<exclude>**/compressed.css</exclude>
</excludes>
</aggregation>
</aggregations>
<excludes>
<exclude>**/scripts/**/*.js</exclude>
</excludes>
<jswarn>false</jswarn>
<nosuffix>false</nosuffix>
<sourceDirectory>${project.basedir}/src/main/webapp/angular/app</sourceDirectory>
<outputDirectory>${project.basedir}/target/umadesktop/angular/app/</outputDirectory>
</configuration>
<executions>
<execution>
<id>compress_js_css</id>
<phase>process-resources</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
</plugin>
You should take a look to Minify Maven Plugin which sounds just like the thing you need.
Please let me know if you need any help configuring it.

How to test jQuery UI Dialog during headless execution

Our app uses jQuery dialogs for modal popups. We're trying to write some unit tests for our javascript, and part of this is the creation of the dialogs. We're using Jasmine and the Jasmine Maven plugin. When running the tests in a browser, it all works fine. However, when we trying to run a headless execution of the tests (mvn jasmine:test), the tests fail on the lines creating the dialog. There error looks something like this:
* TypeError: Cannot find function dialog in object [object Object].
The line this happens on is simply like this:
$(element).dialog(popupProperties);
If it helps, our pom.xml has this for the plugin:
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.2.0.0</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<preloadSources>
<source>${project.basedir}/src/main/webapp/resources/jquery.js</source>
<source>${project.basedir}/src/main/webapp/resources/jquery-ui.js</source>
<source>${project.basedir}/src/main/webapp/resources/angular.js</source>
<source>${project.basedir}/src/test/javascript/lib/angular-mocks.js</source>
<source>${project.basedir}/src/test/javascript/lib/jasmine-fixture.js</source>
</preloadSources>
<jsSrcDir>${project.basedir}/src/main/webapp/resources/</jsSrcDir>
<jsTestSrcDir>${project.basedir}/src/test/javascript/unit/</jsTestSrcDir>
</configuration>
</plugin>
Is there something we can do to get the headless execution working with the dialog stuff?
We figured out what the problem was. Basically the preloadSources from our src directory were getting added twice to the spec runner. It would do all the preoloadSources first, and then add everything from the src directory. So, jquery.js got added a 2nd time under the 2nd jquery-ui.js, and thus it didn't know what dialog was. We fixed it by changing our pom.xml to the following:
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.2.0.0</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<preloadSources>
<source>${project.basedir}/src/main/webapp/resources/${ivplugin.client.version}/jquery.js</source>
<source>${project.basedir}/src/main/webapp/resources/${ivplugin.client.version}/jquery-ui.js</source>
<source>${project.basedir}/src/main/webapp/resources/${ivplugin.client.version}/angular.js</source>
<source>${project.basedir}/src/test/javascript/lib/angular-mocks.js</source>
<source>${project.basedir}/src/test/javascript/lib/jasmine-fixture.js</source>
</preloadSources>
<jsSrcDir>${project.basedir}/src/main/webapp/resources/${ivplugin.client.version}/</jsSrcDir>
<jsTestSrcDir>${project.basedir}/src/test/javascript/unit/</jsTestSrcDir>
<sourceExcludes>
<exclude>**/jquery.js</exclude>
<exclude>**/jquery-ui.js</exclude>
<exclude>**/angular.js</exclude>
</sourceExcludes>
</configuration>
</plugin>
The sourceExcludes prevents it from getting added the 2nd time. This all seems like a bug with the plugin, but this allowed us to get around it.

How to minify a filtered javascript file using maven?

I have a javascript file that is used to track events using Google analytics. I have created different accounts for the staging and production environment. The script to invoke GA code has a place holder for my account Id. The account Ids have been specified in the filter files. Using the webResources element in the maven-war plugin, we are able to successfully replace the property in the final WAR file.
Now, we are also using maven-yuicompressor plugin to minify and aggregate all our javascript files. The problem is that if I attach the execution of minify goal to the package phase, the WAR is created before the Javascript has been minified. If I attach the minify goal to anything earlier, the filter has been not applied till the time of minification producing an invalid file.
So, I am looking for a way to run the minify goal between the WAR plugin copying the filtered JS files and creating the WAR file. Here are the relevant portions of my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp/js</directory>
<targetPath>js</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<linebreakpos>-1</linebreakpos>
<nosuffix>true</nosuffix>
<force>true</force>
<aggregations>
<aggregation>
<output>${project.build.directory}/${project.build.finalName}/js/mysite-min.js</output>
<inputDir>${project.build.directory}/${project.build.finalName}/js</inputDir>
<includes>
<include>jquery-1.4.2.js</include>
<include>jquery-ui.min.js</include>
<include>ga-invoker.js</include>
</includes>
</aggregation>
</aggregations>
</configuration>
</plugin>
Great question, great anwser by artbristol, which i voted up. It helped me a lot. For future reference i post a complete solution:
Put your Javascript files into src/main/resources/js (or whatever fits your needs)
Put your CSS files into src/main/resources/css (or whatever fits your needs)
Resources plugin is filtering your javascript resources and copies them to target/classes/js/
Yui is picking up the files at target/classes/js/ and aggregates them to src/main/webapp
You can test your setup with jetty:run. Jetty is picking up the compressed and aggregated files in src/main/webapp
war plugin at package phase is picking up your files under src/main/webapp
optional put a SCM ignore file like .svnignore or .cvsignore file into src/main/webapp to exclude all.js from your favourite SCM tool.
The excludes section is necessary to avoid compressing anything in src/main/resources and src/main/webapp. yui should just pickup already filtered files for aggregation.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>*/*</exclude>
</excludes>
<force>true</force>
<nosuffix>true</nosuffix>
<removeIncluded>true</removeIncluded>
<aggregations>
<aggregation>
<insertNewLine>true</insertNewLine>
<output>${project.basedir}/src/main/webapp/js/all.js</output>
<includes>
<include>${project.build.directory}/classes/js/file1.js</include>
<include>${project.build.directory}/classes/js/file2.js</include>
</aggregation>
<aggregation>
<insertNewLine>true</insertNewLine>
<output>${project.basedir}/src/main/webapp/css/all.css</output>
<includes>
<include>${project.build.directory}/classes/css/file1.css</include>
<include>${project.build.directory}/classes/css/file2.css</include>
</aggregation>
</aggregations>
</configuration>
</plugin>
Why would you want filtering of css files?
i use it to reference my images like this
div.header {
background-image: url(/img/background.${project.version}.png)
}
Maven filters my project version into my css files. Any incoming request on our imageserver is filtered like this
protected void doHttpFilter ( HttpServletRequest request, HttpServletResponse response, FilterChain chain ) throws IOException, ServletException
{
String uri = request.getRequestURI();
if (isStaticRequest(uri))
{
String uriWithoutVersion = stripVersionString(uri);
String versionRequested = getVersionString(uri);
if (version.equals(versionRequested))
{
response.setHeader(CACHE_CONTROL_HEADER_NAME, CACHE_CONTROL_HEADER_VALUE);
response.setHeader(EXPIRES_HEADER_NAME, EXPIRES_HEADER_VALUE);
}
RequestDispatcher dispatcher = request.getRequestDispatcher(uriOhneVersionsnummer);
dispatcher.forward(request, response);
} else {
chain.doFilter(request, response);
return;
}
}
this way the browser caches each file forever. but when i upload a new version all file reference have a new version number, so the browser is fetching the image or css again.
this helped us a lot to reduce traffic.
Can you put the filtered js files in src/main/resources/filtered-js, use the resources plugin instead, which binds to process-resources, then point the webresources config of the maven-war-plugin to target/classes/filtered-js?
I am using Maven Minify Plugin to compress the javascript and css files. It also merges these files into one single file.This is working fine
I have used maven-timestamp plugin to put a unque suffix to these file as a browser cache solution.This is also working fine
The thing i am stuck with is how to replace all the reference of the js and css with the ones newly created at the build execution time.
I am looking into maven-replace-plugin for the same. It can remove all instances.It can replace with new values. But I need to keep one instance of the newly merged-minified js and css.
<plugins>
<plugin>
<groupId>com.keyboardsamurais.maven</groupId>
<artifactId>maven-timestamp-plugin</artifactId>
<version>1.0</version>
<configuration>
<propertyName>timestamp</propertyName>
<timestampPattern>ddMMyyyyHHmm</timestampPattern>
</configuration>
<executions>
<execution>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</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/resources/themes</outputDirectory>
<resources>
<resource>
<includes>
<include>**/*.vm</include>
</includes>
<directory>${basedir}/src/main/resources/themes</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>maven-minify-plugin</artifactId>
<version>1.3.5</version>
<executions>
<execution>
<id>default-minify</id>
<phase>process-resources</phase>
<configuration>
<cssSourceDir>../resources/themes/XXXX/default/template-resources/stylesheet</cssSourceDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssFinalFile>style.css</cssFinalFile>
<jsSourceDir>../resources/themes/XXXX/default/template-resources/js</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>*.js</jsSourceInclude>
</jsSourceIncludes>
<jsSourceFiles>
<jsSourceFile>/libs/json.js</jsSourceFile>
</jsSourceFiles>
<jsFinalFile>script.js</jsFinalFile>
<suffix>-${timestamp}</suffix>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>target/resources/themes/XXXX/default/templates/velocity/**/*.vm</include>
</includes>
<replacements>
<replacement>
<token><![CDATA[<link rel="stylesheet" href="/storefront/template-resources/stylesheet/]]>([a-zA-Z0-9\-\_]*.css)<![CDATA[" type="text/css"/>]]>([])</token>
<value></value>
</replacement>
<replacement>
<token><![CDATA[<link rel="stylesheet" href="/storefront/template-resources/stylesheet/powerreviews]]>([a-zA-Z0-9\-\_]*.css)<![CDATA[" type="text/css"/>]]></token>
<value></value>
</replacement>
<replacement>
<token><![CDATA[<script type="text/javascript" src="/storefront/template-resources/js/]]>([a-zA-Z0-9\-\_\.]*.js)<![CDATA["></script>]]></token>
<value></value>
</replacement>
<replacement>
<token><![CDATA[<script type="text/javascript" src="/storefront/template-resources/js/libs/]]>([a-zA-Z0-9\-\_\.]*.js)<![CDATA["></script>]]></token>
<value></value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>dist</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Categories