Let's say I have a file-system that looks a little something like this:
C:\stuff\build.xml
C:\stuff\myfolder\library1.jar
C:\stuff\myfolder\library2.jar
Inside build.xml, I want to define a path that looks like this:
<path id="some.id">
<fileset dir="myfolder">
<include name="**/*.jar"/>
</fileset>
</path>
Normally that works fine. However, I am calling my own custom Ant Task that will inherit any references (including the path "some.id") and that custom Ant Task will call a build.xml that lives in a different basedir. Therefore, the "dir" attribute in the fileset is no longer valid.
Is there a way to define a "dir" such that it remains valid no matter where the second build.xml lives?
I essentially want to do something like this:
<fileset dir="${expand.current.directory}/myfolder">
So when I call the second build.xml it will understand that the "dir" attribute is the location of:
<fileset dir="c:\stuff\myfolder">
Edit: Furthermore, I want a solution that allows me to copy the "stuff" project from one machine to another without requiring a change to the build. For example, if the "stuff" project is on the C: drive and I copy the project over to a D: drive on another machine, I want the build to continue to work without me having to go into the build and change the letter C to the letter D.
I think you're after the ${user.dir} property - which is the current working directory.
All java System.properties are available as ant properties.
Option 1:
You can define an Ant property.
At the beggining of the file you can define this property:
<property name="myproject.root.path" location="C:/stuff/"/>
And then, use it:
<fileset dir="${myproject.root.path}/myfolder">
Option 2:
You can also define it at an external build.properties file, located at the same folder in wich the build.xml file is.
File C:\stuff\build.properties
myproject.root.path=C:/stuff/
And, to make use of this file you have to add this line at the Ant XML file (recommended before the tasks definition):
<property file="build.properties"/>
Once you have this file included, you can use the properties along the project, the same way as seen at option 1:
<fileset dir="${myproject.root.path}/myfolder">
You can add more than one properties file.
Note that paths are defined using slashes, and not back-slashes.
Related
I have a requirement where I want to clean only those file in my destDir that are no longer present in sourceDir.
Ex: Assume I have a class: Student.java and College.java in my sourceDir. Running ant task for the first time would generated Student.class and College.class.
Now, if I delete Student.java from sourceDir and add another class 'Employee.java and run ant-javac task, I see a new file, 'Employee.class' generated in destDir, however the old file i.e 'Student.class' is still there even though 'Student.java' no longer exists in sourceDir.
My requirement is to delete all the files from destDir which are no longer in source dir.
PS: I already know that ant clean will work as it will clean my destDir before compiling, but due to few limitations wrt my application, I cannot clean the destDir before running javac
Bit unusual like you point out, typical use case is to clean destDir directory berfore compiling... However you may do some specific clean by explicitly selecting resources that are no longer present in sources directory. You may use Selectors to select which resources to delete. Following snippet should properly clean your destDir if run before javac task :
<delete>
<!-- select all classes that do not have a related source file -->
<fileset dir="${build.dir}" includes="**/*.class">
<not>
<present targetdir="${src.dir}">
<!-- use a regex mapper to handle inner classes -->
<regexpmapper from="^([^\$]*)(\$.*)?.class" to="\1.java"/>
</present>
</not>
</fileset>
</delete>
i was writing a custom task for ant in java and my idea was that i can give someone the .jar which contains the java files like the classes and the libraries and the build.xml for ant and he can use it.
If i export my java project the .jar (antTask.jar) contains :
a folder for the compiled classes, one for the libraries, meta-inf folder and .classpath .project files
The ant build.xml looks like this:
<?xml version="1.0" ?>
<project name="repair" basedir="." default="repairTask">
<taskdef name="antTask" classpath="antTask.jar" classname="def.RepairTask"/>
<target....
i don't really understand all this classpath stuff, so can someone tell me what i have to add in my build file so it will work only with this .jar file without the java code sources?
right now i am getting an error that ant can't find one of the libraries i use in the java code with this error (but the antTask.jar contains this lib as another .jar):
taskdef A class needed by class def.RepairTask cannot be found: org/apache/commons/...
using the classloader AntClassLoader[C:...\AntTask\antTask.jar]
i am trying for hours but i just can't figure out how i have to edit my build.xml so i just have to point to this single .jar file and it works..
Thank you guys
All a taskdef does is associate a task name to a classfile that contains the code to execute that task. However, in order to find that classfile, you need to tell <taskdef/> where to find the jar that contains it. That's all classpath does is.
You don't have to define a classpath with the <taskdef/> task. Ant by default looks for all jars that contain code for the <taskdef/> tasks in $ANT_HOME/lib. If you copy your jar to that folder, you could simply define that task this way:
<taskdef name="antTask" classname="def.RepairTask"/>
No need for the classpath. However, I actually don't recommend doing that. Instead, I recommend putting that jar file into your project, so other developers can use your project without having to install that task jar into their $ANT_HOME/lib folder:
<taskdef name='antTask' classname="def.RepairTask">
<classpath>
<fileset dir="${basedir}/antlib/antjar"/>
</classpath>
</taskdef>
Now, when a developer checks out the project that requires the optional task jar, that task jar comes with the project, so they can simply do their build.
There are two ways to define tasks. One is to give a task a name, and then tell <taskdef/> what classfile is associated with that jar as you did above. However, you can also define a resource that also will associate task names with their classes. Here's a common way to include the Ant-Contrib ant tasks:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<fileset dir="${basedir}/antlib/antcontrib"/>
</classpath>
</taskdef>
If I expand the antcontrib jar, I'll see it contains a net/sf/antcontrib/antcontrib.properties1 file inside the jar. That file looks something like this:
...
# Logic tasks
if=net.sf.antcontrib.logic.IfTask
foreach=net.sf.antcontrib.logic.ForEach
throw=net.sf.antcontrib.logic.Throw
trycatch=net.sf.antcontrib.logic.TryCatchTask
switch=net.sf.antcontrib.logic.Switch
outofdate=net.sf.antcontrib.logic.OutOfDate
runtarget=net.sf.antcontrib.logic.RunTargetTask
timestampselector=net.sf.antcontrib.logic.TimestampSelector
antcallback=net.sf.antcontrib.logic.AntCallBack
antfetch=net.sf.antcontrib.logic.AntFetch
assert=net.sf.antcontrib.logic.Assert
relentless=net.sf.antcontrib.logic.Relentless
# Math Tasks
math=net.sf.antcontrib.math.MathTask
...
All it does is define each task with a classfile for that task. I would recommend you do something similar with your custom ant task. This way, if you decide to include other tasks, you can simply modify this one file, and developers won't have to change their <taskdef/> definition in their jars, or add in multiple ones.
By the way, you should make good and sure that your class doesn't clash with another class that someone else may use. You might want to give your classname a full path that includes a unique prefix:
<taskdef name='antTask' classname="com.vegicorp.anttasks.RepairTask">
Assuming you work for VegiCorp...
1 Ant contrib tasks contain two such files. One is XML format and the other is in properties format. I always use the XML format, and that's what your suppose to use when you define Ant Task resources. I used the properties file because it's a simpler format and easier to see what's going on.
I am trying to run PMD from Ant in Eclipse when I build the project.
This is my build.xml file:
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
<target name="check_pmd">
<pmd rulesetfiles="C:\Users\Nikolay\ProjectName\lib\rulesets\java\basic.xml">
<formatter type="html" toFile="pmd_report.html" toConsole="true"/>
<fileset dir="C:\Users\Nikolay\ProjectName\src">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>
It works well for basic.xml, but I want to run for all rulesets in java folder (It has around 20 rulesets) So I have tried:
<pmd rulesetfiles="C:\Users\Nikolay\ProjectName\lib\rulesets\java\*.xml">
<pmd rulesetfiles="C:\Users\Nikolay\ProjectName\lib\rulesets\java\*">
But both of them fail when I try to run. Is there a way to specify folder, not a single file without specifying list of files manually?
For future readers to configure Ant PMD under Eclipse:
Download pmd-bin.zip from official website
Unpack pmd.jar, jaxen.jar and asm.jar
Add jars above to Window - Preferences - Ant - Runtime - Ant Home Entries - Add External JARs
Unpack rulesets folder
Reference location of ruleset from <pmd rulesetfiles=...>
(expanding answer from coolfan for ant task)
The documentation of PMD rulesetfiles says it is comma separated list of files.
rulesetfiles A comma delimited list of ruleset files
('rulesets/basic.xml,rulesets/design.xml'). If you write your own
ruleset files, you can put them on the classpath and plug them in
here. Yes, unless the ruleset nested element is used
Ant provides a way to convert fileset into such a format. The task is pathconvert
here is an example from website
<fileset dir="${src.dir}" id="src.files">
<include name="**/*.java"/>
</fileset>
<pathconvert pathsep="," property="javafiles" refid="src.files"/>
Maybe the param doesn't support wildcard, as the document suggests.
A quick look over its source code also supports my guess, see RuleSetReferenceId.java, line 194.
So, it takes a property which contains a "list" using , as delimiter, like:
"rule1,rule2,rule3,path-to-rule-file4"
The workaround could be scanning the directory, list all the rule-xml files, and build a property in the comma-delimited format and then pass it to <pmd> task.
Unfortunately, I don't know any ant task which can do this. So you may have to write some code.
I can come up with two ways:
write a ant task; there are many Q&As about this for Java, like this.
write groovy inside a <groovy> task; also many Q&As.
EDIT:
Jayan suggests <pathconvert> task, which should be the right answer.
In the pmd library jar there is an all-java.xml where all the rule sets have been included.
Try to use the following:
<pmd rulesetfiles="rulesets/internal/all-java.xml">
This is a really silly question that I can't fine a difinitive answer to.
Background.
I'm using Eclipse (with one of the ANT plugins, on an XP terminal).
I have just started playing with ANT, in the [jar] directive I am setting the location of my finished JAR file and I get the following when I 'unzip' the file
META-INF/MANIFEST.MF
MyMainFile.class
which is consistent with that found on the oracle web site for the internal structure.
(here http://docs.oracle.com/javase/tutorial/deployment/jar/view.html )
But when I try to run my file I get a 'main class not found' error ?
I have seen some other posts where people have 'unzipped' the JAR file and ended up with a structure of the following...
META-INF/MANIFEST.MF
dtvcontrol/DTVControlApp.class
(from here http://www.coderanch.com/t/528312/java/java/standalone-application)
So should I get a structure where my class files are in a directory that reflects the name of the package... eg
META-INF/MANIFEST.MF
MyPackage/MyMainFile.class
and if so, why am I getting the incorrect structure using ANT, or why are there 2 different 'correct' internal structures? (how to specifify main-class and classpath for each / control what I get)
Also in case you are interested, in the MANIFEST file states (build using ANT)
[attribute name="Main-Class" value="MyPackage.MyMainFile"/]
Also the directory structure of the package under development is as follows...
/JavaDev/MyTestPackage/src (contains the source files)
//JavaDev/MyTestPackage/bin (contains the class files from eclipse, or from the ANT JAVAC task, have I named it incorrectly? should I have called it build ? )
Further to this, when I create the jar I am not naming it 'MyTestPackage.jar' but simply 'test.jar' could this be causing a problem? I assume not as if I have well understood that is what the [main-class] definition stuff is all about.
Further to all this...
'MyTestPackage' is actualy a small visual error messaging library that I use elsewhere, and I have a second file that has a main class to use for testing. As such it points to various libraries (do I need to copy all the SWT libraries to a specified directory?)
I have read somewhere that if I load libraries into my main class (which I obviously do to open the window) then trying to run the program will fail on a 'main class not found' if I use them, same is also true for adding in any 'static final' members (which I use for the loggin of errors).
'Static Final' problem...
I tried to adjust the classpath in ANT, and I get a load of other errors for the connection to Log4J and my 'wrapper' that I use (to do with it being a bad import), but the libraries exist where they should as set in the classpath).
I feel like I am almost there.... but not quite...
I'm doing this for the small 'library projects' that I am creating with the intention of using MAVAN for the main outer package that will connect them all together... for now however I just want to get this going so as it works.
I can supply the full source, or any other info as required.
Thanks in advance...
David
It's simple when you know where to look. Say your META-INF/MANIFEST.MF contains the line:
Main-Class: mypackage.MyMainFile
then the structure of the jar needs to be
META-INF/MANIFEST.MF
mypackage/MyMainFile.class
where MyMainFile has to be in the proper package:
package mypackage;
public class MyMainFile {
public static void main(String[] args) {
...
Your error message is caused by MyMainFile being in the wrong place.
Edit: it's been a while since the last time i did that with ant, but i think you need something like this: a source file structure that reflects the package struture, say
src/main/java/mypackage/MyMainFile.java
and a directory to put the compiled class file into, say
target
(I'm using maven conventions here, ant doesn't care and you can use the (rightclick)->properties->Java Build path->Sources tab in eclipse to set the source dir to src/main/java and the target to target/classes). Then in ant, have a compile target that compiles from source to target:
<target name="compile">
<mkdir dir="target/classes"/>
<javac srcdir="src/main/java" destdir="target/classes"/>
</target>
so that after ant compile you should see the class file in the target
target/classes/mypackage/MyMainFile.class
Then have a ant jar task that packages this:
<target name="jar" depends="compile">
<jar destfile="target/MyJarFile.jar" basedir="target/classes">
<manifest>
<attribute name="Main-Class" value="mypackage.MyMainFile"/>
</manifest>
</jar>
</target>
After saying ant compile jar you should have a file MyJarFile.jar inside target and
java -jar MyJarFile.jar
should run the main method.
I would like to make an ant dependency where the target file depends on a source file. How do you describe this in ant?
For example, convert this Make target to ant
data.txt: header1.txt body.txt footer.txt
cat header1.txt body.txt footer.txt > data.txt
You might be able to do something like this, but it's starting to sound like scripting. Ant isn't a scripting language. If you have a lot of "if/then/else" logic in mind you're probably doing it wrong.
Please describe "other data". Are we talking about copying files? Is this a devl/test/prod environment issue? In that case, you can certainly pass in a parameter specifying environment name and using conditional tests to decide which set to copy. Read this to see how.
If you're just wanting to bring files in one directory up-to-date with respect to your source tree,
you might use the sync task. Here's a basic example from the docs:
<sync todir="site">
<fileset dir="generated-site"/>
</sync>
overwrites all files in site with
newer files from generated-site,
deletes files from site that are not
present in generated-site.
If you need to determine which resources need update,
in order to carry out a more complex operation than a sync,
you might use the ant-contrib outofdate task. For example
<outofdate property="compile.needed" outputsourcespath="sources.for.recompile">
<sourcefiles>
<fileset dir="${src}" includes="*.c"/>
</sourcefiles>
<mapper type="glob" dir="${src}" from="*.c" to="${obj}/*.o"/>
</outofdate>
will set compile.needed to true if any object files are out-of-date compared to source,
and also set the path sources.for.recompile with a list of just the sources that need recompile -
you can then compile for just those sources.
(The assumption here is that a single file in the build output area is directly related to one source.)
My simple solution right now is to manually add a test for the source/dest file age in my shell script called from exec task in ant.