Echo file name during ant process - javascript

I am currently using YUI compressor during an ant build process to minify CSS and JavaScript files. Whilst is it minifying each file, I would like it to output the name of the file it is currently trying to apply the executable to so that if an error occurs, I know which file has cause the error. For instance:
[echo] Minifying JS files...
[echo] Trying to minify file1.js...
[echo] Trying to minify file2.js....
Every solution I have seen seems to just echo all of the filename within the fileset after the the apply instruction has been applied to all of the files.
My ant build currently looks like this:
<target name="minifyJS" depends="overwriteCSSWithMinified">
<echo message="minifying js files and saving them to fileName-min.js" />
<apply executable="java" parallel="false" dest="${toWebHome}">
<fileset dir="${toWebHome}">
<exclude name="**/*.min.js" />
<include name="**/*.js"/>
</fileset>
<arg line="-jar"/>
<arg path="yuicompressor-2.4.7.jar" />
<arg line="-v"/>
<srcfile/>
<arg line="-o"/>
<mapper type="glob" from="*.js" to="*-min.js"/>
<targetfile/>
</apply>
</target>
Maybe there is another way of doing this so rather than using a fileset, use an instruction that loops through every file one at a time and performs the apply on the file?

For this you will need to include ant-contrib. Then you can do this:
<target name="minifyJS" depends="overwriteCSSWithMinified">
<echo message="minifying js files and saving them to fileName-min.js" />
<foreach target="yui" param="jsFile">
<fileset dir="${toWebHome}">
<exclude name="**/*.min.js" />
<!-- should this be -min.js instead of .min.js ? -->
<include name="**/*.js"/>
</fileset>
</foreach>
</target>
<target name="yui">
<echo message="${jsFile}"/>
<exec executable="java">
<arg value="-jar"/>
<arg value="yuicompressor-2.4.7.jar"/>
<arg value="-v"/>
<arg value="-o"/>
<arg value="'.js$:-min.js'"/>
<arg value="${jsFile}" />
</exec>
</target>

Related

How to include external JS file in JSP file in Spring WebMVC framework?

I have tried below answer but it didn't work..
How include an external JS file in a JSP page
I have been searching on google but couldn't find anything useful...my JS file is at same level as that of WEB-INF..any help would be appreciated...
Below is the code that I'm using to include my JS file in JSP :-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.18.0/jquery.validate.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/formValidation.js"></script>
below code is in web.xml :-
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
Below code is in dispatcher-servlet.xml file :-
<context:component-scan base-package="com.programcreek.helloworld.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
You might want to add ResourceHandler to resolve your static resources like js/css directory
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/js/**")
.addResourceLocations("/js/")
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
This method apply for java configuration class extends WebMvcConfigurerAdapter
The xml version should look like this
<mvc:resources mapping="/js/**" location="/js/"/>
This will resolve any .js file under /webapp/js/ directory, using something like below in .jsp file
<script src="js/custom.js"></script>
for help you can see this github link click here
I hope it will help you
and this is the project structure

Cant find CSS & JS files with excluded templates JAVA + Spring

i am using spring for my project and i excluded all template-files to another "html-Project". I did it because i can do changes without any redeployment and for me its ways clearer. it works fine and the spring application find all templates. But now, the template cant find any css or js files. The template try to search the files in my spring-project but they are included in the html project. How i can fix it? I need something like a relative path i think.
Projects
SpringApp
---- lib
---- src
-------- main
------------ java
---------------- com.example.test
-------------------- controller
------------------------ ShowIndex.java
------------ resources
------------ webapp
-------- test
HTML-Templates
---- lib
---- src
-------- main
------------ java
------------ resources
------------ templates
---------------- css
-------------------- bootstrap.min.css
---------------- js
-------------------- bootstrap.min.js
---------------- images
---------------- index.ftl
---------------- test.ftl
-------- test
Content of ShowIndex.java
#RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(ModelMap model) {
model.put("prename", "Hans-Peter");
return "index";
}
HeaderContent of index.ftl
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="./css/bootstrap.min.css">
<link rel="stylesheet" href="./css/zeus.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Any ideas?
I use:
JAVA
Spring WebMVC
Freemarker as TemplateEngine
Put your css, js, images to the src/main/webapps folder, separate from the templates ftl files.
EDIT
With Servlet 3.0 you have another choice, but maybe not work in all containers...
create a META-INF/resources folder
put all you resources js, css, images and etc to this folder
and 'jar' it.
for example I created a assembly.xml file for automate this work
<assembly>
<id>bin</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>**</exclude>
</excludes>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
<excludes>
<exclude>**/DefaultController.class</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>WebContent</directory>
<outputDirectory>META-INF/resources</outputDirectory>
<includes>
<include>/WEB-INF/jsp/**</include>
</includes>
</fileSet>
<fileSet>
<directory>WebContent</directory>
<outputDirectory>META-INF/resources</outputDirectory>
<includes>
<include>js/**</include>
<include>plugins/**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
This file works when use maven building mvn package.
PS:
In maven pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>

Custom Cordova plugin iOS index.js dosent recognise my .js file

I have written a custom Cordova plugin that brings have as a dependency "Media" plugin.
When i run "cordova plugin add "myPlugin"" the plugin is installed to the sample project however when in my index.js i try to init my plugin it does not recognise it and if i try to create "Media" object it does.
Here is my plugin .xml file :
<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="cordova-plugin-MyPlugin"
version="1.0.0">
<name> MyPlugin </name>
<description>Cordova MyPlugin Plugin</description>
<author> MyPlugin </author>
<license>MIT</license>
<keywords> MyPlugin </keywords>
<dependency id="org.apache.cordova.media"/>
<js-module src="www/MyPlugin.js" name="MyPlugin">
<clobbers target="window.plugins.MyPlugin" />
</js-module>
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="MyPlugin">
<param name="ios-package" value="CDVMyPlugin" />
</feature>
</config-file>
<!-- Sources -->
<header-file src="src/ios/CDVMyPlugin.h"/>
<source-file src="src/ios/CDVMyPlugin.m"/>
<!-- iOS Frameworks -->
<framework src="libz.dylib" />
</platform>
</plugin>
Thanks for the help.
The problem apparently was in my "MyPlugin.js" file at the bottom of the file the line :
module.exports = MyPlugin;
Needs to be added then in the Index.js file (or the file of your choosing) add the following :
//Get a reference to the custom .JS file.
myCustomPlugin = cordova.require("cordova-plugin-MyPlugin.MyPlugin");
myPluginInstance = new myCustomPlugin(someParams,onSuccess,onError,null);
Now you can access the methods of your .JS file.
Credit to https://stackexchange.com/users/366553/marat-strelets for helping with the solution.

Exclude directory and js minification with Ant

I want to use ant for js and css minification at time of building the project. Please find below directory structure of my project:
Here, scripts directory has multiple directories.
I want to exclude directory named "Libraries" in scripts dirctory from ant task.
The dirctories other than "Libraries" has js files. And I want to include these js files in ant minification task.
Please find below my build.xml:
<project name="ITV" default="doMinification">
<property name="webroot.dir" value="webapp/" />
<target name="doMinification" depends="minifyjs" />
<tstamp>
<format property="TODAY_MY" pattern="yyyyMMdd-HHmmss" locale="en,UK" />
</tstamp>
<!-- Minify JS -->
<target name="minifyjs">
<apply executable="java" parallel="false" dest="webapp/scripts/" verbose="true">
<fileset id="jsFiles" dir="webapp/scripts/">
<exclude name="webapp/scripts/Libraries/" />
<include name="**/*.js"/>
</fileset>
<arg line="-jar"/>
<arg path="yuicompressor-2.4.7.jar"/>
<srcfile/>
<arg line="-o"/>
<mapper type="glob" from="**/*.js" to="*-min-${TODAY_MY}.js" />
<targetfile/>
</apply>
</target>
</project>
How to exclude a specific directory and include other directories with its files in ant javascript minification task ?
You may want to check similar question here
There is this answer:
<exclude name="**/dir_name_to_exclude/**" />
For your case:
<exclude name="**/Libraries/**" />
eg:
<target name="minifyjs">
<apply executable="java" parallel="false" dest="webapp/scripts/" verbose="true">
<fileset id="jsFiles" dir="webapp/scripts/">
<exclude name="**/Libraries/**" />
<include name="**/*.js"/>
</fileset>
<arg line="-jar"/>
<arg path="yuicompressor-2.4.7.jar"/>
<srcfile/>
<arg line="-o"/>
<mapper type="glob" from="**/*.js" to="*-min-${TODAY_MY}.js" />
<targetfile/>
</apply>
</target>
I hope this helps.

Is it possible to render js files with Velocity code inside with Spring MVC and Velocity-Toolbox?

I'm writing a webapp with Spring MVC, Velocity and AngularJS. I face a problem with I18n strings.
In html output sometimes translated tags are static so i can use a custom velocity call to put a translated text from a properties files, something like this:
<h2>#springMessageText('main.section.title')</h2>
But sometimes the text in render dinamically in client using javascript so i have many translations_xx_XX.js files to store those translations:
var _i18n = {
'lang': 'gl-ES',
'lang.short': 'gl',
'serverError.message':'Erro no servidor.',
'yes': 'Sí',
'no': 'Non',
...
}
So i load those translations in client with whith a javascript function when needed:
myCtrl.i18n['serverError.message'];
All works fine but i face the problem that many labels are duplicated in both *.properties files and translations_xx_XX.js files and it's hard to maintain.
So my question: Is there a way to render inside js files using velocity tags, so the translations_xx_XX.js file is filled with translations from *.properties files? Somthing like this:
var _i18n = {
'lang': '#springMessageText("lang")',
'lang.short': '#springMessageText("lang.short")',
...
}
Doing this i have to deal only with one source for translation tags.
Edited:
I found where the problem is. Velocity was configured along with Spring MVC in webapp-config.xml:
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/view/"/>
<property name="velocityProperties">
<props>
<prop key="input.encoding">utf-8</prop>
<prop key="output.encoding">utf-8</prop>
</props>
</property>
</bean>
<!-- bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".html"/>
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="contentType" value="text/html; charset=UTF-8"/>
<property name="toolboxConfigLocation" value="/WEB-INF/velocity-toolbox.xml" />
</bean>
So only html files under /view folder are parsed by Velocity. My question is if there is a way to add the same Velocity behaviour to *.js files under /resources folder? Can i define two velocityConfig and two viewResolver?
Possibly you can move the translations_xx_XX.js into the Velocity template folder. And the translations_xx_XX.js needs to be imported in the .vm file.
P.S. This solution assumes that the view is generated using Velocity View Servlet.
Edited: 05:22AM 28 Aug,2015 IST
As mentioned in your post, <property name="resourceLoaderPath" value="/view/"/>, here view is Velocity template folder/directory. You will have to move the translations_xx_XX.js file(s) into this folder. Now you need to use the .vm to import the .js so that velocity macros could be parsed. e.g.
page.vm:
<script>
#parse("translations_xx_XX.js")
</script>
Can i define two viewResolver?
Yes, all you need to do is add <property name="order" value="<order_no>"/>, lower order value has a higher priority, e.g.
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/spring-views.xml</value>
</property>
<property name="order" value="0" />
</bean>
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="spring-views" />
<property name="order" value="1" />
</bean>
Finally i managed to make it possible.
First of all, you need two view resolvers configured in the Spring web configuration file:
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/view/"/>
<property name="velocityProperties">
<props>
<prop key="input.encoding">utf-8</prop>
<prop key="output.encoding">utf-8</prop>
</props>
</property>
</bean>
<bean id="htmlViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="order" value="0" />
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".html"/>
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="contentType" value="text/html; charset=UTF-8"/>
<property name="toolboxConfigLocation" value="/WEB-INF/velocity-toolbox.xml" />
</bean>
<!-- Resolver velocity for js files to be rendered on server side. Should be included in /view/ to avoid problems with other static content inside /resources/ for example. -->
<bean id="jsViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="order" value="1" />
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".js"/>
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="contentType" value="text/javascript; charset=UTF-8"/>
<property name="toolboxConfigLocation" value="/WEB-INF/velocity-toolbox.xml" />
</bean>
It's very important to notice that you must put the js you want to render inside the resourceLoaderPath defined in the VelocityConfigurer. Also notice that this must not clash with the mvc:resources path to avoid Spring serve your js file statically.
Then create your js file as a normal velocity file (in this case inside the /views/ forder). In my case it declares a hash of translations depending on server locale:
var _i18n = {
'lang': '#springMessageText("lang")',
'lang.short': '#springMessageText("lang.short")',
'serverError.message': '#springMessageText("serverError.message")',
'entities.displayName': '#springMessageText("entities.displayName")',
...rest of translations...
};
Finally you need to do that that spring web render this view with Velocity so you need to add to a controller one mapping like this:
#RequestMapping("/translations.js")
public String getTranslationsJs(HttpServletRequest request, Locale locale) {
return "/translations";
}
This way, when making a reference to this js file on your html:
<script type="text/javascript" src="/translations.js"></script>
Spring web renders the file using velocity returning a content like this:
var _i18n = {
'lang': 'en-US',
'lang.short': 'en',
'serverError.message': 'Server error.',
'entities.displayName': 'Company name'
...
};

Categories

Resources