Where should the fileset property be placed within the ant build file? - ant

Here is an example of my beginning build file, I do not know where to put the fileset property. Right now I have a build.properties file that defines src as my directory with the current src. Ex(src= C:\workspace\project\src)
My fileset is defined like so
<?xml version="1.0" encoding="UTF-8"?>
<project name="Hello World Project" default="info">
<fileset dir="${src}"
<include name="**/*.java"/>
</fileset>
<target name="info">
<echo>packaging the .java files...</echo>
</target>
</project>
I don't really understand where the .java files are supposed to be put. this is only for my understanding and a demo. can I include them in a random folder?? Is the fileset located in the proper placement or should it be inside the target task?
Thanks

When running your build file the following error is thrown.
$ ant -p
Buildfile: build.xml
build.xml:6: Element type "fileset" must be followed by either attribute specifications, ">" or "/>".
This not related to the placement of the fileset tag, but rather a message telling you your xml is not well formed.
Fix the XML as follows:
<fileset dir="${src}">
<include name="**/*.java"/>
</fileset>
Note the missing ">" character.

Related

How can PMD HTML Report's Error Description Link Show Local Folder

I am using PMD source code analyzer (PMD) for my java web project through ant task. The computer is offline (not connected to the Internet). Part of ant task is as follows:
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask">
<classpath>
<fileset dir="E:/pmd-bin-6.41.0/lib">
<include name="*.jar"/>
</fileset>
</classpath>
</taskdef>
<pmd shortfilenames="true" cachelocation="pmd.cache" encoding="UTF-8">
<ruleset>web/resources/category/java/bestpractices.xml</ruleset>
<formatter type="html" tofile="report.html">
</formatter>
<fileset dir="src/java/">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>
When I run pmd target, report.html file is generated ok. The html file basically lists <fileName, lineNumber, description> triplets.
e.g.
foo.java...43...The initializer for variable "tempIDNo" is never used (overwritten on lin 67)
The description in this html file has a link as file:///E:ws/project/${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#unusedassignment which does not work. E:ws/project/ is the folder where my project resides.
As a matter of fact, I have all the necessary html files (such as pmd_rules_java_bestpractices.html) unzipped in E:/pmd-doc-6.41.0 folder.
Could you please help me how to set up description link in html file to show local folder?
Thank you.
Here is the solution I have come up with:
(Using suggestion from (How can I create a link to a local file on a locally-run web page?)) Before ant pmd target define property pmd.website.baseurl
<propery name="pmd.website.baseurl" value="file:///E:/pmd-doc-6.41.0"/>
(Using usage/suggestion from (https://ant.apache.org/manual/Types/filterchain.html#expandproperties), ANT replacing strings in specified files using file with properties ) Change inside target as follows
...
<pmd ...>...
</pmd>
<copy file="report.html" tofile="report2.html">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.ExpandProperties"/>
</filterchain>
</copy> ...
Run the ant target.

is there a way to make a jar with the Eclipse generated ant script?

To my surprise the build.xml file generated by Eclipse (Neon) for Java has no element containing an invocation of a jar task. As often is the case with code generation I think you have to use it and make no edits so that you can regenerate - or - avoid code generation completely. A comment in the generated file suggests it might be possible to avoid edits by extending the capabilities by importing.
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. -->
I thought I would be able to use the <?eclipse.ant.import?> element in an second file called export.xml. In ant scripting there is supposed to be one project per buildfile so now there is a second project with a dependency on a target in the first project.
Regenerating build.xml reveals that it contains an "import" as expected.
<import file="export.xml"/>
Unfortunately this does not work. Running ant, which I do from the command line, just seems to result in the export/jar project being ignored.
The generated script with the import element (nested on the 7th line)...
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="ohana1">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.8"/>
<property name="source" value="1.8"/>
<import file="export.xml"/>
<path id="ohana1.classpath">
<pathelement location="bin"/>
<pathelement location="../export/ohana1/commons-collections-3.2.1.jar"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="ohana1.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
</project>
The export.xml file meant to make a jar...
<?eclipse.ant.import?>
<project basedir="." default="export" name="ohana1Export">
<target depends="build,make-jar" name="export"/>
<target name="make-jar">
<jar destfile="../export/ohana1/${ant.project.name}.jar" basedir="bin"/>
</target>
</project>
Note that the Eclipse Ant editor complains about this export.xml file because the target named build, which is a dependency, does not exist in this project/buildfile. The build target is in the generated build.xml. That error might be coming from a "dumb" editor so I went ahead to do a run of ant. Invoking ant from the command line I find that there is no jar file made.
Should I conclude that Eclipse's ant script generator is useless if you need to export a .jar file and that a human should maintain the ant script that meets all the requirements?
Yes, in my opinion the exported build.xml is useless, as of Eclipse Neon, if the intention is to make a .jar.
Specifically do the following.
Manually write the trivial ant script that exports a .jar. The link at the bottom of this post has verbatim text on what the script might look like. You can use the built-in Xml Editor via New > Other > XML > XML File to create this new file which might be called makeJar.xml and save it. If the icon shown in the Package Explorer is still a plain XML file icon refreshing the project may change the icon to an Ant file icon. In the future, you can use Open With to get the Ant Editor instead of the XML Editor. This script will replace the manual exporting of a .jar that the user would otherwise perform via Eclipse.
This script can be added to Project > Properties > Builders. It would be placed second in the list of Builders. First in the list of Builders is the Java Builder which should already exist. When an Eclipse build is invoked the entire list of Builders will be processed in the order shown in the list of Builders. Thus not only will .class files be generated but also the .jar.
What is achieved is greater automation since the .class generation and .jar generation are now integrated, which arguably was the point of using the exported build.xml in a failed attempt to generate the .jar.
Here is the dialog at Project > Properties > Builders that you can use to create a new Builder. Select New then select Ant Builder. I gave the name makeJar to the new Builder.
Here is the dialog for the new Ant Builder that will allow you to browse to your buildfile which is your manually written Ant script that creates a .jar file. In this example the script is makeJar.xml. It also allows you to browse to the base directory to be used when the script is run.
After setting up the new Builder, a project "clean" or project "build" will create .class files and also the .jar.
Eclipse's documentation on this subject is at the link. Note that it seems impossible to link the exact page that contains the instructions so you have to browse down the documentation tree to the section about "Ant buildfiles as project builders".
Link to Eclipse and Ant

Apache Ant create new fileset from existing fileset with filenames renamed

I have the following fileset in my Apache Ant build file (BuildFilesetXML.xml):
<fileset id="XMLFileset" dir="mydir/myxml">
<include name="**/1.xml"/>
<include name="**/2.xml"/>
</fileset>
I want to create dynamically in a different build file (BuildFilesetLog.xml) (do not know the contents of "XMLFileset" in my 2nd build file) a new fileset named "LOGFileset" that will have the same contents of "XMLFileset" but with names renamed to .log. So, in runtime it will have the same structure as the following fileset:
<fileset id="LOGFileset">
<include name="**/1.log"/>
<include name="**/2.log"/>
</fileset>
Can this be done in Ant?
Thanks
No, but some tasks support mappers. For an example see the following answer:
Change directory of FileList when referencing it

Ant build script not seeing refid

I have a build.xml file that includes a common.xml file that defines some refid values. However, my task cannot see the refid value. I have not been able to find a solution on the web and am looking for some help.
I call the genbeans target in the build.xml file. It fails on the xmlbean taskdef with the message Reference my_classpath_jars not found.
build.xml
----------------------------
[includes common.xml]
**my_classpath_jars fails to be seen at this point - defined in common.xml**
<taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean">
<classpath refid="my_classpath_jars"/>
</taskdef>
<!-- Generate the XMLBeans java code from our source XSD file(s) -->
<target name="genbeans" description="Generate XML Bean files" depends="build_my_jar_cpath">
<mkdir dir="${lib}"/>
<xmlbean destfile="${lib}/${appname}Beans.jar" failonerror="true">
<classpath refid="my_classpath_jars"/>
<fileset dir="src/XSD Files" includes="*.xsd, *.wsdl"/>
</xmlbean>
</target>
common.xml
-----------------------------
<target name="build_my_jar_cpath">
<path id="my_classpath_jars">
<fileset dir="${jardir}" includes="**/*.jar" />
</path>
<pathconvert pathsep="${path.separator}" property="myjar.clpath" refid="my_classpath_jars"/>
</target>
When in doubt, use the ant -d switch when calling your target. You'll see a ton of output. Save it to a file and parse through it.
Do that, and the first thing you'll notice in the output is that it's defining your taskdefbefore you have defined your my_classpath_jars. That my_classpath_jars refid is only set when you call that greenbeans target. Your <taskdef> is executed before any of your targets are called.
Either take the definition of my_classpath_jars out of the target greenbeans, or put your <taskdef> in there.

A zip file cannot include itself - Ant build error

I have error at the first line of the following code while building with Ant builder,
<war warfile="${wartemp.dir}/${name}.war" basedir="${wartemp.dir}" webxml="${wartemp.dir}/WEB-INF/web.xml">
<include name="*"/>
<include name="scripts/**"/>
<include name="styles/**"/>
<include name="images/**"/>
<include name="WEB-INF/*.*"/>
<include name="WEB-INF/lib/**"/>
<include name="WEB-INF/views/**"/>
<include name="WEB-INF/classes/**"/>
<include name="WEB-INF/jsp/**"/>
<include name="WEB-INF/resources/**"/>
<include name="WEB-INF/spring/**"/>
<include name="WEB-INF/messages/**"/>
<include name="WEB-INF/layouts/**"/>
<exclude name="WEB-INF/web.xml"/>
<exclude name="**/.*"/>
</war>
The error message is:
"... /WEB-INF/build.xml:67: A zip file cannot include itself"
line 67 is the first line of the snippet posted above.
I am beginner to Spring framework. I am using Spring version 3 with springsource toolsuite. How to fix this?
thanks.
Your basedir is the same dir as where you are sending the outputted war file. This is not a problem in itself, the problem is you are including * as input which will include the output file.
To fix this you could either exclude the output file from the included files, e.g.:
<exclude name="${name}.war"/>
or you could write the war file to a different directory structure than you are reading from, e.g.:
<mkdir dir="${war.output.dir}" />
<war warfile="${war.output.dir}/${name}.war" ...>
I guess I found another cause of the "A zip file cannot include itself" problems in any "zip-alike" Ant tasks (zip, jar ...):
Remember, setting the "basedir" attribute is already the first set of files to include! You need to explicitly exclude the zip file being created at this level (with "excludes" attribute. Or, starting with Ant 1.7, with nested "excludes" element).
The "fileset" nested element is another "set" for the zip task. You should ensure that the zip "itself" will be excluded from the set also with another explicit exclude. And so on...

Resources