how to remove java System.out.println from ant script - java

how to remove java System.out.println from ant script?
(when we compile the code from ant we should remove the existing java class system.out.println & compiled classes should not have the Sys.out.println)

In build.xml file, preparation phase, before the mkdir commands,
provide:
<javac srcdir="exe" includes="SysOutRemove.java"/>
<java fork="true" classname="SysOutRemove" dir="exe" failonerror="true"/>
where SysOutRemove.java is in the exe package of your project.
SysOutRemove.java should iterate through the list of directories and files in them, store the contents of each file to a reader or something, find sysout statements and replace.

We can use this in the ant file.
<replaceregexp match="System.out.println(.*);" replace="" flags="g" byline="true">
<fileset dir="${src.dir}" includes="**/*.java"/>
</replaceregexp>

Related

Copy external docs (from xyz-javadoc.jar) into JavaDocs

I want to create a .jar file with Ant, which contains all JavaDocs of my library and its dependencies. I did a lot of searching yesterday afternoon/evening and this morning, but none of the solutions work for me.
My first solution:
<!-- Generate JavaDoc -->
<javadoc sourcepath="${src}" destdir="${doc}" windowtitle="${ant.project.name}">
<classpath path="${lib}/nv-websocket-client-2.9-javadoc.jar"/>
<classpath path="${lib}/gson-2.8.6-javadoc.jar"/>
</javadoc>
My second solution:
<!-- Generate JavaDoc -->
<javadoc sourcepath="${src}" destdir="${doc}">
<classpath>
<fileset dir="${lib}">
<include name="gson-2.8.6-javadoc.jar"/>
<include name="nv-websocket-client-2.9-javadoc.jar"/>
</fileset>
</classpath>
</javadoc>
In both cases, however, only the JavaDoc for my own code is produced. The libraries are completely ignored. In the log of the Ant-Task there are errors, that the classes from the libraries were not found.
I don't know if this is the best aproach, but for me it's easier to use "jar" from command line.
Then all you have to do is indicate where are your files located:
jar uf0 path_to_your_jar\your_jar_file.jar path_to_your_files\*.*
jar uf0 path_to_your_jar\your_jar_file.jar path_to_your_other_files\*.*
If the libraries you want to add are already packed in a jar file, I would extract them first in the root directory, so the path of every file is correct. If you execute the previous commands, you'll have all your files in the "your_jar_file.jar" file.
if you type jar --help from command line you'll see more options. I hope it helps.

Using Ant to build multiple jars

I have multiple Java eclipse projects. Each of them has "jardesc" file for building jar. It's nice - double click -> finish and jar file is made. But when i have to export several jars it's a pain - i have to repeat procedure several times.
Please tell me, can i use Ant script to run several "jardesc" files at once (and get several jars according to each jardesc file)? How to do it?
You could use the jar target to make the jars for you:
<jar destfile='destination.jar' basedir='source\dir\' />
so your build.xml would look a little like this:
<project default="makejars">
<target name="makejars">
<jar destfile="app1.jar" basedir="app1\src\" />
<jar destfile="app2.jar" basedir="app2\src\" />
<jar destfile="app3.jar" basedir="app3\src\" />
</target>
</project>
then just run ant in the same directory as build.xml, and the jars should be made.
Take a look at subant task in ant. You can create ant-file which would call other files to.
<subant target="create_jar1">
<fileset dir="." includes="jar2.xml"/>
</subant>
<subant target="create_jar2">
<fileset dir="." includes="jar1.xml"/>
</subant>
You can use some loops to create ant parameters however there is no way to loop to create multiple jars (even with ant-commons extension), a copy & paste is the only viable solution unless you want to write an ant plugin (which doesn't really take that much 2 hours reading docs + write simple plugin)

Ant scripting creating 2 class files

I am not sure If I am asking a very simple question but I have a project in which the developer has used "XSD" files to create java source codes. The input to the project is an XML message which will be parsed into java object.
The project is working fine if I export the class files into a jar file (using eclipse options to export class files into jar files).
But if I use "ant" script to compile and create a jar file, I always get "JAXBException". After Checking I found that the main java file is compiled into 2 class files by ant script, whereas when I use eclipse to compile there is only one class file.
For example -
Main Java file - MpgProcessor.java
Compile using eclipse - MpgProcessor.class
Compile using ant - MpgProcessor.class and MpgProcessor$1.class.
I am using following command in ant to compile the java files -
<target name="compile" depends="clean-build-files" >
<echo message="STEP 2 = COMPILING JAVA FILES " />
<mkdir dir="${classDir}" description="Ensure launch directory created" />
<javac srcdir="${src.dir}" destdir="${classDir}" classpathref="build.classpath" debug="on" compiler="javac1.6"/>
<javac srcdir="${src.generated}" destdir="${classDir}" classpathref="build.classpath" debug="on" compiler="javac1.6" verbose="off"/>
</target>
Can anyone please help?

Compiling java files in all subfolders? [duplicate]

This question already has answers here:
javac option to compile all java files under a given directory recursively
(10 answers)
Closed 9 years ago.
How to compile all java files in all subfolders on Unix, using javac?
On Windows...
Create a batch file:
for /r %%a in (.) do (javac %%a\*.java)
...then execute it in the top-level source folder.
On Linux...
javac $(find ./rootdir/* | grep .java)
Both answers taken from this thread...
http://forums.oracle.com/forums/thread.jspa?threadID=1518437&tstart=15
But as others suggested, a build tool would probably prove helpful.
Use a build tool such as Ant or Maven. Both lets you manage dependencies in a much better way than can be accomplished using e.g. the find UNIX tool. Both And and Maven also lets you define custom tasks to be performed in addition to compilation. Maven furthermore comes with conventions for managing external dependencies in remote repositories, as well as conventions for running unit tests and features that support continuous integration.
Even if you just need to compile your source files once in a while, you'll probably find that setting up a simple Ant build.xml file can be a big time saver in the end.
Finally, most of the popular IDE and code editor applications has some kind of integration with Ant build scripts, so you can run all the Ant tasks from within the editor. NetBeans, Eclipse, IDEA and more also has built-in support for Maven.
Read this first, if you're new to Ant. Below is the example build file from the link:
<project name="MyProject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
Once you're familiar with Ant, you'll find it easier to move to Maven.
I don't know if it is the best way, but this should work :
find . -name "*.java" | xargs javac
Use Ant to write a script to compile as many source folders as you want.
Use Maven (as a more modern alternative to Ant).
Use an IDE, like Eclipse (all IDEs I know will happily compile multiple source folders for you)
Another (less flexible) way, if you know how much folder levels there are:
javac *.java */*.java */*/*.java */*/*/*.java */*/*/*/*.java ...
Depending on your shell, you may have to set it to expanding non-matching patterns to nothing, in bash with shopt -s nullglob. For example, I'm using the following shell function to find text in my java files:
function jgrep ()
{
(
shopt -s nullglob
egrep --color=ALWAYS -n "$#" *.tex *.java */*.java */*/*.java */*/*/*.java */*/*/*/*.java */*/*/*/*/*.java
)
}
jgrep String
But really, use an build tool, as the others said.

Output of java task in Ant

I have a .java file and am compiling it using javac in ant. The .class file goes to output directory. A.class when ran, produces a.txt.
How to run the ant ´java´ task and where will the a.txt go, when ran? I mean which directory? Can I specify the direc. where the output files of java task should go?
Take a look at this for reference:
http://ant.apache.org/manual/Tasks/java.html
It contains an example of using the Java task to run a specific class, e.g:
<target name="run">
<java classname="A">
<classpath>
<pathelement location="output"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
</target>
It really depends on where you are writing the file out to from A.java. If it is in the current directory, e.g:
File f = new File("./test.txt");
f.createNewFile();
then it will output the file relative to where you ran the build file from.
Hope that helps.

Categories