Maven YUI Compressor: compress after aggregation - javascript

Can I setup Maven YUI Compressor for compression files after aggregation, because I want to exclude "sources" directory, which contain files for aggregation and then compress aggregated files.
For example I have some .js-files inside of /js/sources/mylib/ and all this files merged into one file /js/mylib.js while yui-compressor aggregation stage. In pom.xml I configure yui-compressor to exclude whole /js/sources/ and compress files only within /js library, but yui-compressor execute "compress" goal before aggregation, so I have uncompressed merged files. And I need to fix this somehow

Can we turn it around and say: Aggregate after compression? If so, I had the same issue and with the following it worked out for me.
The trick seemed to be to put the output file in the target dir instead of in the source. That way you can include files that got minified in the aggregation.
Files that were already minified I then approach through the basedir var.
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/*-min.js</exclude>
<exclude>**/*.min.js</exclude>
<exclude>**/*.css</exclude>
</excludes>
<removeIncluded>false</removeIncluded>
<jswarn>false</jswarn>
<aggregations>
<aggregation>
<insertNewLine>true</insertNewLine>
<removeIncluded>false</removeIncluded>
<includes>
<!-- these exist only in target dir. the original was json2.js & bootstrap.tabs.js -->
<include>**/bootstrap-tabs-min.js</include>
<include>**/json2-min.js</include>
<!-- use originals for the ones already minified -->
<include>${basedir}/src/main/webapp/js/backbone-min.js</include>
</includes>
<output>${project.build.directory}/${project.build.finalName}/js/test.js</output>
</aggregation>
</aggregations>
</configuration>

Related

How to generate JavaScript file from Java Properties with Maven in Eclipse?

In an old Java web application there are many keys in properties files for internationalization. JavaScript functions also use this key to show some messages.
Actually this file is inline in HTML, dynamicly generated on JSPs, causing an increase of approximately 400 KB on each page request.
To improve performance slightly I generated JavaScript files with key|value with content like this:
var b = {
keys: {
"key.one": "value.one",
"key.two": "value.two"
}
};
The JavaScript file path look like this:
The message properties files are in the directory like this:
I need to read the message property files in the build to create the JavaScript files.
To do this, I am using exec-maven-plugin, set up this way:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<arguments>
<argument>${project.build.directory}/${project.build.finalName}</argument>
</arguments>
<mainClass>com.brunocesar.build.GenerateI18NJSFiles</mainClass>
</configuration>
</plugin>
GenerateI18NJSFiles reads the properties files and generates the JavaScript files, but only when running maven build (e.g.: mvn clean compile war:exploded).
I need to generate them also in Eclipse.
SO, I set up the Eclipse lifecycle-mapping plugin like this:
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<versionRange>[${exec-maven-plugin.version},)</versionRange>
<goals>
<goal>java</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>false</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
But it still is not generating the files in the Eclipse build, but only by Maven.
Is there is a way to set up Eclipse (maybe in m2e-wtp, maybe using another plugin) to generate the files and deploy in a container (such as JBoss, Tomcat, etc.)?

Disable javascript facet in a m2e project

I'd like to disable the Javascript Facet (and remove the Javascript builder) from a Maven-driven Eclipse project.
Is there any way I can configure the maven-eclipse-plugin to do so?
First of all, clean the configuration with mvn eclipse:clean, it will remove all configuration from the .project file.
Then make a new one with mvn eclipse:eclipse, just with the configuration and dependencies on your pom.xml.
EDIT:
And configure the project natures that you need:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<projectnatures>
<projectnature>org.eclipse.jdt.core.javanature</projectnature>
<projectnature>org.eclipse.wst.common.modulecore.ModuleCoreNature</projectnature>
</projectnatures>
<additionalProjectnatures>
[... the ones you need or empty...]
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
</configuration>
</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.

jasmine-maven-plugin load source files and their dependencies

I've got a bunch of javascript files I'd like to test, located in src/main/webapp/js, but when I try to build on the command line, I get a bunch of errors due to the missing library files that each of my source scripts depend upon. I am trying to include them within the "sourceIncludes" tag, but this seems to do nothing. I can, however, place them all, one at a time, in the "preloadSources" tags, but I'd rather not do this.
Also, some of my sources files have functions that interact with the DOM, and including the .html file that contains the elements that these functions need just gives me:
"net.sourceforge.htmlunit.corejs.javascript.EcmaError: ReferenceError: "XML" is not defined"
my pom.xml below:
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<jsSrcDir>src/main/webapp/js</jsSrcDir>
<jsTestSrcDir>src/test/javascript/js</jsTestSrcDir>
<sourceIncludes>
<include>src/main/webapp/js/lib/**/*.js</include>
<include>**/*.js</include>
</sourceIncludes>
<preloadSources>
<preloadSource>src/main/webapp/js/lib/jquery/jquery-1.7.2.js</preloadSource>
</preloadSources>
</configuration>
</plugin>
I hate having to list my dependencies in order. That's why I use a dependency loader like Require.js. I wrote a quick post summarizing my experience with jasmine-maven-plugin, but using require.js to load dependencies. It keeps my pom.xml clean, and I let my JavaScript files load their own dependencies. jasmine+maven+requirejs+code coverage

jasmine-maven-plugin and require-js result in path issues

I am using require-js to model dependencies in my java script project. I also use jasmine to write BDD styled test cases and the jasmine-maven-plugin:1.1.0 to execute these tests in headless mode.
This is my source project structure
project/
| src/main/javascript
| | api/*.js
| | main.js
| src/test/javascript
| | spec/*spec.js
This is my webapp pom jasmine configuration:
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>generateManualRunner</goal>
<goal>resources</goal>
<goal>testResources</goal>
<goal>test</goal>
<goal>preparePackage</goal>
</goals>
</execution>
</executions>
<configuration>
<specRunnerTemplate>REQUIRE_JS</specRunnerTemplate>
<scriptLoaderPath>lib/require-jquery.js</scriptLoaderPath>
<sourceIncludes>
<include>lib/require-jquery.js</include>
<include>lib/underscore.js</include>
<include>lib/backbone.js</include>
<include>lib/d3.js</include>
<include>lib/d3.layout.js</include>
<include>lib/bootstrap.js</include>
<include>main.js</include>
</sourceIncludes>
<preloadSources>
<source>lib/require-jquery.js</source>
<source>lib/underscore.js</source>
<source>lib/backbone.js</source>
<source>lib/d3.js</source>
<source>lib/d3.layout.js</source>
<source>lib/bootstrap.js</source>
<source>main.js</source>
</preloadSources>
<browserVersion>FIREFOX_3_6</browserVersion>
</configuration>
</plugin>
During the maven build all js sources are copied to target/jasmine/src. The runner.html that refers to my main.js is at target/jasmine.
Now the problem is that my require-js modules define their dependencies relative to the main.js, whereas in the maven test run, the runner assumes them to be relative to target/jasmine/runner.html. Since my modules are not (and must not be) aware of the additional jasmine/src folder, IMHO that is an issue.
I receive the following error message when I run mvn clean install:
Failed to execute goal
com.github.searls:jasmine-maven-plugin:1.1.0:test (default) on project
mywebapp: The jasmine-maven-plugin encountered an
exception: java.lang.RuntimeException:
org.openqa.selenium.WebDriverException:
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
java.lang.RuntimeException: java.io.FileNotFoundException:
C:\Jenkins\jobs\job1\workspace\mywebapp\target\jasmine\api\data-util.js
(The system cannot find the path specified)
Anybody out there having some idea to work around or configure that?
I faced the same problem:
java.io.FileNotFoundException
with requireJS and jasmine
but you don't have to use the maven-resources-plugin and hack the js files to a second output directory.
I used 'srcDirectoryName'
you can find all possible params at https://github.com/searls/jasmine-maven-plugin/blob/master/src/main/java/com/github/searls/jasmine/mojo/AbstractJasmineMojo.java
and advanced examples under https://github.com/searls/jasmine-maven-plugin/tree/master/src/test/resources/examples
In the meantime I found a workaround or a fix (not sure) by configuring the maven-resources-plugin
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-js-files</id>
<phase>generate-test-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/jasmine</outputDirectory>
<resources>
<resource>
<directory>src/main/javascript</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/webapp</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/test/webapp</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
I put that configuration in a separate profile which I activate during testing.
I see that you've got lib/require-jquery.js, but your file tree doesn't show where "lib" is located. is loaded relative to , but you don't have one. Maybe that's your problem. I wrote a quick post summarizing my experience with the jasmine-maven-plugin and using Require.js. In it I've got my pom.xml. My pom.xml seems quite a bit smaller than yours but the plugin works fine for me. Maybe my post will help. Jasmine+Maven+Requirejs+Coverage

Categories