Ant task to compile stylus file to normal css file? - ant

I am trying to use stylus in a project, that is using ANT build.
I need to compile the stylus file to normal css before packaging the war.
is there a way to do it?
Note : I am not using node.js, it is a simple java web application

Here is a full example how to use it:
<property name="css.stylus" value="../folder/to/stylus/files"/>
<property name="cmd.node" value="C:\Program Files\nodejs\nodevars.bat"/>
<property name="cmd.stylus" value="C:\Program Files\nodejs\stylus.cmd"/>
<target name="css_compile" description="compile l'ensemble des CSS avec stylus">
<exec executable="${cmd.node}" />
<exec dir="${css.stylus}" executable="${cmd.stylus}" >
<arg value="myCSS.styl"/>
<arg value="-c"/>
<arg value="-o"/>
<arg value="..\cssFolder"/>
</exec>
</target>
This example (I used) transform myCSS.styl in a obfuscate CSS to /cssFolder
nodevars.bat is necessary for launching stylus.
stylus.cmd is stylus itself, because the path not working in ant. To know where is it, (it's in nodejs), you can type "path" in terminal, on Windows.
That's all!

Related

How to execute custom ant target using exec task?

In our project we are using a proprietary product which contains its own custom ant executable to build the deployment artifact.
So, we have two build files 'build-artifcat.xml' and 'build.xml'.
Here,
'build-artifcat.xml' - contains ant targets which can be executed with this products custom ant executable.
'build.xml' - contains ant targets which can be executed by Apache Ant.
We are using Bamboo CICD tool and this tool provides facility to execute only Apache Ant targets.
So, we are planning to use task to invoke ant targets from 'build-artifcat.xml' using this products custom ant executable.
I found below code while checking older projects but here I am not sure how can I specify the target to be executed from from 'build-artifcat.xml'.
Can you please help.
<target name="buildArtifact">
<echo>Running ant exec: ${run.custom.ant.exec}</echo>
<exec dir="${run.ant.exec.dir}" executable="${run.custom.ant.exec}" error="${basedir}/${error.log.file}">
<arg value="-f" />
<arg file="${basedir}/build-artifcat.xml" />
<arg value="-DimportDir=${import.location}" />
<arg value="-DartifactDir=${artifact.dir}" />
<arg value="-data" />
<arg value="${temp.workspace}" />
<arg value="-vmargs" />
<arg value="-Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./crashes/my-heap-dump.hprof -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./crashes/gc.log" />
<arg value="-verbose" />
</exec>
</target>

Apache Ant: Unable to call Composer's vendor/bin scripts

I am using Apache Ant for my builds. I have some composer scripts belonging to several vendors in vendor/bin folder. I have added this folder to the system path and if I run the commands on my command window in works but in the build file i get an error.
Is there anything I should be doing differently? Before is an example:
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="build" basedir=".">
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="phpcpd">
<arg value="--version" />
</exec>
</target>
</project>'
I get this when I run ant phpcpd
phpcpd:
BUILD FAILED
C:\xxxxxx\xxxxxxx\build.xml:96: Execute failed: java.io.IO Exce
ption: Cannot run program "phpcpd": CreateProcess error=2, The system
cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at java.lang.Runtime.exec(Runtime.java:617)
at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Jav
a13CommandLauncher.java:41)
at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:428)
at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:442)
at org.apache.tools.ant.taskdefs.ExecTask.runExecute(ExecTask.java:628)
at org.apache.tools.ant.taskdefs.ExecTask.runExec(ExecTask.java:669)
at org.apache.tools.ant.taskdefs.ExecTask.execute(ExecTask.java:495)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
...
But phpcpd --version works on command prompt
ANT is not aware where phpcpd is as it doesn't share path with your Command Promt.
One way around it is to create a .bat file to run phpcpd
Create a phpcpd.bat with the following:
#echo off
phpcpd --version
Your build scripts to be updated from :
<exec executable="phpcpd">
<arg value="--version" />
</exec>
To: <exec executable="phpcpd.bat"/>
Above assumed with Windows Command Promt
My solution was to use the .phar files of the scripts. That way, the build file became platform independent to a large extent. So
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="phpcpd">
<arg value="--version" />
</exec>
</target>
Became:
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="php">
<arg value="${phpcpd}" />
<arg value="--version" />
</exec>
</target>
Where ${phpcpd} is the path to the phar file
I'm using absolute paths and a configurable executable.properties like this:
build.xml
<project name="build">
<property file="executable.properties" />
<target name="run-phpcd" unless="${phpcpd.skip}">
<exec executable="${phpcpd.executable}"><!-- .. --></exec>
</target>
</project>
executable.dist.properties
phpcpd.skip = no
#phpcpd.executable = C:\path\to\phpcpd.bat
phpcpd.executable = /path/to/phpcpd.sh
Both files can be committed to your VCS, for usage copy the template file (*.dist.properties) and rename it to executable.properties. Add this file to the ignore list of your VCS.

JSCover - Excluding coverage files

Currently trying to get JSCover to exclude js files that are used as libraries. I have a set of ant scripts below which will
start the JSCover server
Run & Generate Json report
Stop the server
Finally, i have a shell command to convert the Json file to LCov so that i can use it with sonarqube. I also get coverage in jscoverage.html but it includes every file under web/ which is something i do not want. Image below
Ant scripts below:
<target name="jstest-start">
<java jar=".../JSCover.jar" fork="true" spawn="true">
<arg value="-ws"/>
<arg value="--report-dir=coverage"/>
<arg value="--document-root=web"/>
<arg value="--port=8082"/>
<!-- Aim is to exclude folder js under web/ as it contains libraries, not source code. Currently does not work -->
<arg value="--no-instrument=web/js/"/>
<arg value="--no-instrument=js/"/>
</java>
<waitfor maxwait="5" maxwaitunit="second" checkevery="250" checkeveryunit="millisecond" timeoutproperty="failed">
<http url="http://localhost:8082/jscoverage.html"/>
</waitfor>
<fail if="failed"/>
</target>
<target name="jstest-run">
<exec dir="/usr/local/CI/phantomjs/bin/" executable="phantomjs" failonerror="true">
<arg line=".../run-jscover-qunit.js http://localhost:8082/index.html"/>
</exec>
</target>
<target name="jstest-stop">
<get src="http://localhost:8082/stop" dest="stop.txt" />
</target>
<target name="jstest" description="Run javascript tests">
<antcall target="jstest-start"/>
<antcall target="jstest-run"/>
<antcall target="jstest-stop"/>
</target>
My folder structure is:
And finally, my sonar standalone analysis settings:
So, what seems to be happening is that JSCover is recursively reading for all js files and i cannot prevent that from sonar or ant.
Can anyone shed some light?
<arg value="--no-instrument=/js/"/>
should work, and to remove the test itself,
<arg value="--no-instrument=/test/"/>
The paths are as seen by the web-server, so the 'web' prefix in:
<arg value="--no-instrument=web/js/"/>
has no effect.
i have resolved my own issue by correcting the shell command which generates an LCOV report.
java -cp JSCover.jar jscover.report.Main --format=LCOV /usr/local/CI/jenkins/workspace/PhantomJS/coverage/phantom/ /usr/local/CI/jenkins/workspace/PhantomJS/web/
Prior to this, the SRC-DIR and REPORT-DIR were the same which was an error on my part. As far as i can understand, SRC-DIR should point to the source folder and REPORT-DIR should point to where the lcov file exists.
I hope this helps someone

Ant: Apply with fileset, but only exec once on one file?

I'm working on something similar to the question here: ANT script to compile all (css) LESS files in a dir and subdirs with RHINO
However, I'm having a hard time customizing this to one particular requirement:
If any .less files in dir.less change: Run LESS on just one file (as it imports the other less files, making a single, combined output).
This is the state of my current build.xml:
<target name="less" description="Compile LESS files">
<echo message="Checking for LESS file changes..."/>
<apply dir="${dir.less}" executable="${tool.less}" parallel="false" failonerror="true">
<fileset dir="${dir.less}" includes="*.less" />
<srcfile/>
<mapper type="glob" from="*.less" to="${dir.css}/*.css"/>
<targetfile/>
<arg value="-compress" />
</apply>
</target>
This currently builds all of the .LESS files and outputs them toe the appropriate location (Which is livable). If I replace the mapper glob with:
<mapper type="glob" from="MainFileThatImportsOthers.less" to="${dir.css}/MainFileThatImportsOthers.css"/>
The fileset directive is effectively reduced to that one file, and changing the other .LESS files in that directory don't cause output from the task.
Can someone point me in the right direction so I can avoid setting this up wrong and recusing through each .LESS file every time?
I worked out a solution that works correctly, I used an upToDate task to set a property to conditionally trigger Exec for the compiler:
<target name="scanLess" description="Scan for LESS file changes">
<echo message="Checking for LESS file changes..."/>
<uptodate property="tool.less.changed" targetfile="${dir.css}/MyFile.css" >
<srcfiles dir="${dir.less}" includes="*.less" />
</uptodate>
</target>
<target name="less" depends="scanLess" unless="tool.less.changed" description="Compile LESS files" >
<echo message="LESS files changed, running lessc" />
<exec executable="${tool.less}" failonerror="true">
<arg value="${dir.less}/MyFile.less" />
<arg value="${dir.css}/MyFile.css" />
<arg value="-compress" />
</exec>
</target>
Investigate how selectors work in ANT

How to execute the JAXB compiler from ANT

I am using JAXB on a project. the attraction of JAXB is that it is bundled with the JDK, I have been to use xjc.exe on the command line to generate the .java files from a schema. I can't seem to find the JAXB ant task, sure there is a download at http://jaxb.java.net however i want to use the JAXB that is bundled into the JDK is there some way to call JAXB from ant what class does the xjc.exe call on?
<target name="generate-jaxb-code">
<java classname="com.sun.tools.internal.xjc.XJCFacade">
<arg value="-p" />
<arg value="com.example"/>
<arg value="xsd/sample.xsd" />
</java>
</target>
Just went hunting in the tools.jar and found the XJCFacade.class in com.sun.tools.internal tested the above code it works it produces the output as xjc.exe It seems that XJC.exe calls this code com.sun.tools.internal.xjc.XJCFacade
One of my key requirements was that the ant file had work within eclipse without having to include a path name to the JDK that way the file would be portable across operating systems. I am assuming that tools.jar is included on the classpath via the installed JRE preferences options.
Here is a helpful link:
https://jaxb.java.net/nonav/2.0.2/docs/xjcTask.html
Java SE 6 does not ship the Ant task (see 7.1.3):
https://jaxb.java.net/guide/Migrating_JAXB_2_0_applications_to_JavaSE_6.html
Essentially they do the following:
<target name="xjc" description="....">
<exec executable="${jdk.dir}/bin/xjc.exe">
<arg value="-d"/>
<arg value="${src.dir}"/>
<arg value="-p"/>
<arg value="com.mydomain.jaxb"/>
<arg value="${etc.dir}/myschema.xsd"/>
</exec>
</target>

Resources