How use property expansion in a Launch4j config file - ant

I use Launch4j and will use a property ${dist} in its configuration.
It works when the task and it argument directly are in the build.xml file:
<project ...>
<property name="dist" location="/temp/dist" />
<launch4j>
<config headerType="gui" outfile="${dist}/myprogram.exe"
dontWrapJar="false" jarPath="${dist}/myprogram.jar">
...
</config>
</launch4j>
</project>
Launch4j can however use its own xml-configuration file, with <launch4jConfig> as root element:
in ant.xml:
<launch4j configFile="my_launch4j_config.xml" />
in my_launch4j_config.xml:
<launch4jConfig>
<headerType>gui</headerType>
<outfile>${dist}/myprogram.exe</outfile>
<dontWrapJar>false</dontWrapJar>
<jar>${dist}/myprogram.jar</jar>
...
</launch4jConfig>
In this case, ${dist} is not expanded, nor %dist% or everything I tried... Does a solution exist to use properties in an launch4j config file?

The code of launch4j did not accept such replacements of parameters, but I could change this behaviour (modifications to net.sf.launch4j.config.ConfigPersister). I check it in the Sourceforge project when I have enough time for it.

Related

Ant xmlproperty task fails due to validation error

I want to extract an application version from a DITA map file. The ditamap file is valid and looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd">
<map id="user-manual">
<title><ph keyref="product"/> User Manual</title>
<topicmeta>
<prodinfo>
<prodname><keyword keyref="product"/></prodname>
<vrmlist>
<vrm version="4" release="3" modification="0"/>
</vrmlist>
</prodinfo>
</topicmeta>
<!--
[...]
-->
</map>
The information I want to get is in the <vrm> element.
"Easy peasy," I think to myself. So I use Ant's <xmlproperty> task to just load this XML file.
<project default="test">
<!-- notice #validate -->
<xmlproperty file="path/to/user-manual.ditamap" validate="false"/>
<target name="test">
<echo>${map.topicmeta.prodinfo.vrmlist.vrm(version)}</echo>
</target>
</project>
I don't want it to validate because Ant isn't going to find map.dtd.
Loading the file returns an error:
java.io.FileNotFoundException: /home/user/user-manual/map.dtd (No such file or directory)
If I remove the <!DOCTYPE> declaration or add a nested <xmlcatalog> with the path to the DTD, the file loads and I can use the properties from it.
I tested this with Ant 1.7.1 and 1.9.4. Is this a bug with Ant, or am I misunderstanding how Ant loads XML properties and the purpose of the validate attribute?
How can I make Ant obey my will?
I recommend to not use the <xmlproperty> for this. Please have a look at the docs:
For example, with semantic attribute processing enabled, this XML
property file:
<root>
<properties>
<foo location="bar"/>
<quux>${root.properties.foo}</quux>
</properties>
</root>
is roughly equivalent to the following fragments in a build.xml file:
<property name="root.properties.foo" location="bar"/>
<property name="root.properties.quux" value="${root.properties.foo}"/>
So the name of the properties you set is generated using their paths to the root element, so they rely on the structure of your DITA Map. But many elements in DITA may be set at different positions on your DITA Map. That means, if you move your metadata to another parent element, the property name changes and your build fails. This is probably not, what you want.
I'd recommend to grab those values via XSLT and than set the properties. That way, you could, for example, say, "give me the first occurance of that element with a simple //foo[1] XPath selector. Further on, you have the power of XSLT and XPath to slice values, format dates and so on before setting a property.
Update
You can use the oops consultancy Ant xmltask for that. It is very easy to set a property using <copy>:
<copy path="//critdates/created/#date"
property="document.date"
append="false"/>

yguard not updating properties file in the jar

I have jar file having some properties files in it like log4j.properties and config.properties. Following is my ant script for yguard. Everything else is working but the properties file updation.
<target name="yguard">
<taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" classpath="lib/yguard.jar" />
<yguard>
<inoutpairs resources="none">
<fileset dir="${basedir}">
<include name="MyApp.jar" />
</fileset>
<mapper type="glob" from="MyApp.jar" to="MyAppObs.jar" />
</inoutpairs>
<externalclasses>
<pathelement location="lib/log4j-1.2.17.jar" />
</externalclasses>
<rename conservemanifest="true" mainclass="com.amit.Application" >
<adjust replaceContent="true" >
<include name="**/*.properties" />
</adjust>
</rename>
</yguard>
</target>
config.properties file
com.amit.Application.param1 = something
I found some question in stackoverflow but they didn't help. One place it was mentioned that the file (like jsp, xml, properties) should be in the jar file which I already have. But my yguard obfuscated file just get the files copied as it is.
I tried many combinations with rename & adjust tags but nothing worked for me.
Following post I already visited
Is it possible to manage logs through Obfuscation with yGuard?
How to include obfuscated jar file into a war file
Apparently you want yGuard to obfuscate the name of the field param1, because com.amit.Application is obviously your entry point and yGuard excludes the given main class automatically. So basically you want the outcome to be something like
com.amit.Application.AÖÜF = something
This isn't possible, because yGuard can only adjust class names in property files, as state here: yGuard Manual

The prefix "sonar" for element "sonar:sonar" is not bound

I have a build.xml-file that looks something like this:
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml" classpath="/path/sonar-ant-task.jar"/>
<target name="sonar">
<sonar:sonar/>
</target>
And when I run the file I get:
The prefix "sonar" for element "sonar:sonar" is not bound.
Any obvious things I'm missing?
You're missing the namespace declaration in the top project element of your Ant script.
xmlns:sonar="antlib:org.sonar.ant" ought to do it.
In ant you can not use .
try below and if you are setting any properties use key value pare in xml tag.
To allocate value use attributes of xml tags.
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant">
</sonar:sonar>

Ant XSLT task to build on mutiple dependencies

I have an XSL style sheet that merges external documents, this way
<xsl:copy-of select="document('snippets.xml')/snippets/xxxx/form"/>
I would like that the XSLT Ant build task rebuilds if the file or any of its dependencies changed.
The current Ant task looks like this
<xslt basedir="xxxx/pages/${doc.locale}"
destdir="xxxx/dir/${doc.locale}"
includes="*.xml"
excludes="snippets.xml"
style="build/xxxx/${doc.locale}/myStyle.xsl">
<param name="lang" expression="${doc.locale}"/>
<xmlcatalog refid="docDTDs"/>
Basically I would like to rebuild if the snippets.xml document is changed.
Ant has the uptodate task to check if a target is up-to-date, file modtime-wise, from a set of source files. I'm not completely clear on what your dependency is since the XSLT task could create multiple files (resulting in multiple targets), or if it creates a single file. One of your comments imply a single file.
The following is one way to use uptodate. You basically use the task to set a property that can then be put in the unless attribute of a target:
<property name="file.that.depends.on.snippets"
location="some/path"/>
<property name="snippets.file"
location="xxxx/pages/${doc.locale}/snippets.xml"/>
<target name="process-snippets"
unless="snippets.uptodate"
depends="snippets-uptodate-check">
<xslt basedir="xxxx/pages/${doc.locale}"
destdir="xxxx/dir/${doc.locale}"
includes="*.xml"
excludes="snippets.xml"
style="build/xxxx/${doc.locale}/myStyle.xsl">
<param name="lang" expression="${doc.locale}"/>
<xmlcatalog refid="docDTDs"/>
</xslt>
</target>
<target name="snippets-uptodate-check">
<uptodate property="snippets.uptodate"
targetfile="$file.that.depends.on.snippets">
<srcfiles dir="xxxx/pages/${doc.locale}"
includes="*.xml"
excludes="snippets.xml"/>
</uptodate>
</target>
The XSLT task should do this for you by default. It has an optional "force" attribute
Recreate target files, even if they
are newer than their corresponding
source files or the stylesheet
which is false by default. So by default, unless you override with the "force" attribute, dependencies are checked by the XSLT task.

How can I allow an Ant property file to override the value set in another?

I have an ant file that does the following:
<property file="project.properties" description="Project configuration properties"/>
<property file="build-defaults.properties" description="default build configuration."/>
<property file="build.properties" description="local build configuration overrides"/>
I want to have defaults set in build-defaults.properties (which is checked in to SCM) but allow developers to override values in a local build.properties so that they can work with local paths.
The problem is, it doesn't seem to be working; I've set this up, created an override in build.properties, but the value of my path remains the one set in build-defaults.properties. How do I accomplish this?
The initial problem with your set up is that you've got build.properties and build-defaults.properties reversed.
Ant Properties are set once and then can never be overridden. That's why setting any property on the command line via a -Dproperty=value will always override anything you've set in the file; the property is set and then nothing can override it.
So the way you want this set up is:
<property file="build.properties" description="local build configuration overrides"/>
<property file="project.properties" description="Project configuration properties"/>
<property file="build-defaults.properties" description="default build configuration."/>
This way:
Anything set at the command line takes precedence over build.properties
Anything set in build.properties overrides other values
etc. on down the line.
Actually ant properties may be overriden. See the documentation of the property task:
Normally property values can not be changed, once a property is set,
most tasks will not allow its value to be modified.
One of the tasks that are able to override the property value is script. Also any custom task may use this backdoor. Other proposals are in question Ant loadfile override property. This is against the spirit of ant and usually unnecessary. But it's good to know that, because I just had an opposite problem: why the property value changed although it is immutable.
Here is a sample target that uses script task to change the value of a property. It shows the basic methods to work with properties. All methods are described in Ant Api which is not available online. You need to download the Ant Manual. In its api directory there is the api documentation.
<target name="t1">
<property name="a" value="one" />
<script language="javascript">
sProp = project.getProperty("a");
sProp = sProp.replace("e", "ly");
project.setProperty("a", sProp);
project.setNewProperty("a", "new value");
</script>
<property name="a" value="two" />
<echo>a=${a}</echo>
</target>
How to easily setup the script task? Making the script task running with beanshell language is a bit tricky and non-trivial, but it's explained in this answer. However as Rebse noted, using javascript language is supported out of the box in jdk 6.
Ant property can't be overwritten unless using macro and javascript plug-in to do:
Step 1: define a macro function to overwrite property
<!--overwrite property's value-->
<macrodef name="set" >
<attribute name="name"/>
<attribute name="value"/>
<sequential>
<script language="javascript">
<![CDATA[
project.setProperty("#{name}", "#{value}");
]]>
</script>
</sequential>
</macrodef>
Step 2: use the macro in the ant xml
<set
name="your_target_property"
value="your_value" or "${another_property}"
</set>

Resources