AccessDeniedException when copying maven application to Raspberry Pi with ant - java

I'm trying to run my Maven program from my Windows PC on my Raspberry Pi through Ant in Eclipse. Full disclosure, i have basically no experience with Raspberry, Linux and Ant.
I was following this guide and the code is from there.
The code for the build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="Der_HFBot" default="remote-run" basedir="."
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<!-- Setup RASPBERRY PI properties -->
<property name="raspberrypi" value="cencored" />
<property name="raspberryfolder" value="~" />
<property name="username" value="cencored" />
<property name="password" value="cencored" />
<!-- <path id="maven-ant-tasks.classpath" path="${ant.libs.dir}/maven-ant-tasks-2.1.3.jar" /> -->
<path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.3.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant" classpathref="maven-ant-tasks.classpath" />
<typedef
resource="org/apache/maven/artifact/ant/antlib.xml"
uri="antlib:org.apache.maven.artifact.ant"
classpathref="maven-ant-tasks.classpath" />
<!-- Add maven install target to be run before deploy -->
<target name="maven-install">
<artifact:mvn pom="pom.xml">
<arg value="install"/>
</artifact:mvn>
</target>
<!-- Locate the project jar and transfer via scp to RASPBERRY PI -->
<target name="transfer" depends="maven-install">
<first id="jars">
<fileset dir="target" includes="**/*-SNAPSHOT-jar-with-dependencies.jar" />
<!--<fileset dir="target" includes="**/*.jar" /> -->
</first>
<pathconvert pathsep="," property="jar.path" refid="jars" />
<basename file="${jar.path}" property="jar.filename" />
<echo>">>> Found application ${jar.path}"</echo>
<echo>">>> Copying application to ${raspberrypi}:${raspberryfolder}/${jar.filename}"</echo>
<scp
localfile="${jar.path}"
todir="${username}:${password}#${raspberrypi}:${raspberryfolder}"
trust="true" />
</target>
<!-- Run java -->
<target name="remote-run" depends="transfer">
<echo>">>> Starting ${raspberrypi}:${raspberryfolder}/${jar.filename}"</echo>
<sshexec
host="${raspberrypi}"
username="${username}"
password="${password}"
trust="true"
failonerror="true"
usepty="true"
command="java -jar ${jar.filename}" />
</target>
<!-- Run java in debug mode and keep waiting for execution -->
<target name="remote-debug" depends="transfer">
<echo>">>> Starting ${raspberrypi}:${raspberryfolder}/${jar.filename} in debug mode"</echo>
<sshexec
host="${raspberrypi}"
username="${username}"
password="${password}"
trust="true"
failonerror="true"
usepty="true"
command="java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=y -jar ${jar.filename}" />
</target>
</project>
The project builds succesfully, but fails at the transfer with the following message:
transfer:
[echo] ">>> Found application "
[echo] ">>> Copying application to censored_IP:~/Der_HFBot"
[scp] Connecting to censored_IP:22
BUILD FAILED
C:\Users\Paddy\Desktop\Google Drive\Telegram Bots\Der_HFBot\build.xml:47: java.nio.file.AccessDeniedException: C:\Users\Paddy\Desktop\Google Drive\Telegram Bots\Der_HFBot
Total time: 3 seconds
I've tried running Eclipse as admin and moving the project to a folder outside of Drive.
edit: It seems like ${jar.path} doesn't return a path? It's supposed to echo "Found application ${jar.path}" but in the console it returns "Found application ".
edit2: When i enter the path manually the transfer works, so the problem is in the code that finds the jar path. Since this is a personal project i have no problem enterin the path manually, if your fingers are itching to fix the code feel free though.

AccessDeniedExceptions usually come from a lack of file permission. Use chmod 777 filename on both the program itself and all other applicable files, this will allow everything to read/write/execute on these files, so make sure you have a safe development environment.

Related

How to run testng.xml using command prompt

I was trying to run testng.xml from command prompt but not able to figure out the following error:
Error: Could not find or load main class org.testng.TestNG
or either
Error: Could not find or load main class java.
While using two different method.
Please provide a prominent method to do the same.
Thanks guys, but i have solve this problem using Apache ant.
By using following - build.xml
<project name="name" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<!-- set global properties for build -->
<property name="basedir" value="." />
<property name="lib" value="${basedir}/lib" />
<property name="src" value="${basedir}/src" />
<property name="bin" value="${basedir}/bin" />
<property name="report-dir" value="${basedir}/Test-Report" />
<property name="testng-report-dir" value="${report-dir}/TestNGreport" />
<!-- ====== Set the classpath ==== -->
<path id="classpath">
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="*.jar" />
</fileset>
</path>
<!-- Delete directories -->
<target name="delete-dir">
<delete dir="${bin}" />
<delete dir="${report-dir}" />
</target>
<!-- Creating directories -->
<target name="create" depends="delete-dir">
<mkdir dir="${bin}" />
<mkdir dir="${report-dir}" />
</target>
<!-- Compile the java code from ${src} into ${bin} -->
<target name="compile" depends="create">
<javac srcdir="${src}" classpathref="classpath" includeAntRuntime="No" destdir="${bin}" />
<echo> /* Compiled Directory Classes */ </echo>
</target>
<!-- Runs the file and generates Reportng report for TestNG-->
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="classpath" />
<target name="testng-execution" depends="compile">
<mkdir dir="${testng-report-dir}" />
<testng outputdir="${testng-report-dir}" classpathref="classpath" useDefaultListeners="true">
<xmlfileset dir="${basedir}" includes="testng.xml" />
</testng>
</target>
</project>
And then by typing: ant testng-execution on command prompt
Paths are separated using : under Unix-like systems and not ; as in Windows:
java -cp ./src/lib/*:./bin org.testng.TestNG testng.xml
If you are using bash under Windows, you should use ; and any other place use :.
The ; character means end of statement to a Unix shell, so what you are probably attempting to execute is:
java -cp ./src/lib/*
./bin org.testng.TestNG testng.xml
I use testNg on windows, in the windows command prompt the execution, I use the command: java -cp bin;libs/* org.testng.TestNG testng.xml
Result: test run successfully
always on windows: I use git bash command, the command does not execute successfully
Try to run via cmd prompt, like this:
1. Go to project folder
2. Then: `java -cp [projectpath]\lib\*;[projectpath]\target org.testng.TestNG testng.xml`

Issue while creating war using ant with Jenkins

I have a ant build script which creates a war file. The file content are as follows.
<?xml version="1.0" encoding="UTF-8"?>
<project name="TestProj" default="war" basedir=".">
<property name="project-name" value="${ant.project.name}" />
<property name="builder" value="IaasTeam" />
<property name="war-file-name" value="${project-name}.war" />
<property name="source-directory" value="src" />
<property name="classes-directory" value="build/classes" />
<property name="web-directory" value="WebContent" />
<property name="web-xml-file" value="WebContent/WEB-INF/web.xml" />
<property name="lib.dir" value="WebContent/WEB-INF/lib" />
<property name="catalina.home" value="../../outside/project/lib"/>
<tstamp prefix="build-info">
<format property="current-date" pattern="d-MMMM-yyyy" locale="en" />
<format property="current-time" pattern="hh:mm:ss a z" locale="en" />
</tstamp>
<property name="build-directory" value="build" />
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<fileset dir="${catalina.home}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="src" destdir="build/classes" classpathref="classpath" />
</target>
<target name="war" depends="clean,compile">
<mkdir dir="${build-directory}" />
<delete file="${build-directory}/${war-file-name}" />
<war warfile="${build-directory}/${war-file-name}" webxml="${web-xml-file}">
<classes dir="${classes-directory}" />
<fileset dir="${web-directory}">
<!-- Need to exclude it since webxml is an attribute of the war tag above -->
<exclude name="WEB-INF/web.xml" />
</fileset>
<manifest>
<attribute name="Built-By" value="${builder}" />
<attribute name="Built-On" value="${build-info.current-date}" />
<attribute name="Built-At" value="${build-info.current-time}" />
</manifest>
</war>
</target>
I am using Jenkins as a build server (this is hosted on different machine kind of DEV environment).
I also use Gitlab as a repository and after pushing the latest code I have a hook for Jenkins job which gets triggered automatically and calls this build.xml.
Now the issues here is that when I run this script on my local machine everything works well but when Jenkins execute this it fails during the compilation phase giving me below error.
compile:
[mkdir] Created dir: /app/infra/jenkins/workspace/TestProj/build/classes
[javac] Compiling 49 source files to /app/infra/jenkins/workspace/TestProj/build/classes
BUILD FAILED
/app/infra/jenkins/workspace/TestProj/build.xml:27: /app/infra/jenkins/outside/project/lib does not exist.
The reason for this issue is the build server does not have any directoy called outside/project/lib.
The only reason of adding this directory in my build.xml is to have the container specific jar files ready for compiling.
How can I fix this issue?
Do I need to copy container specific jars on my build server? Or is there any way to tell Jenkins that not to copy this external jars but just use them for compilation.
Where would Jenkins find the jars? They need to be accessible otherwise your build will fail. If you don't want to have the files checked in (which is very sensible), you could use Apache Ivy to download them for you.
This is the most common way of handling the situation you're having. Using a dependency management framework like Ivy (or Maven, or similar) will save you a lot of headaches down the line. I recommend you have a look at their tutorial. After you set it up, your ant build will take care of downloading the files you need.

unable to read system properties inside jar which is in classpath of another java project

My project has a jar in classpath with some utilities in it. When I run the program using ant on Red Hat linux, it's unable to read the system properties using System.getProperty(key), but it works perfectly fine when ran on Ubuntu.
Any suggestion to why this might be happening is appreciated.
ant target
<target name="test">
<property environment="env"/>
<echo message="PATH_ROOT set => ${env.PATH_ROOT}" />
<echo message="CUSTOM_REPORT_PATH set => ${env.CUSTOM_REPORT_PATH}" />
<testng classpathref="compile.classpath" haltOnfailure="false" outputDir="${testng.output.dir}/xxx">
<sysproperty key="PATH_ROOT" value="${env.PATH_ROOT}"/>
<sysproperty key="CUSTOM_REPORT_PATH" value="${env.CUSTOM_REPORT_PATH}"/>
<xmlfileset dir="src/com/xxx" includes="suite.xml" />
</testng>
</target>
================
Guys it was a silly mistake. The framework was not executing the file which had the statements to fetch system variables as the listener was missing in testng suite which is a must for testng to invoke that

Ant script in Eclipse uses the incorrect version of Java

I'm working on an older project that requires to use Java 1.5, but when I run the build script, I get the error:
java.lang.UnsupportedClassVersionError: Bad version number in .class file
I believe this error refers to a newer version of Java being used even though I only want to use Java 1.5. I've altered the settings under Windows->Preferences->Java->Installed JREs and under Run As->External Tool Configuration->JRE such that both are pointing to Java 1.5. Under Run As->External Tool Configuration->Environment, I added variables JAVA_HOME and JRE_HOME pointing to the older version of Java. When adding this line to the build script:
<echo message="ant.java.version: ${ant.java.version}"/>
Eclipse returns this value:
[echo] ant.java.version: 1.5
but the above error is still present. The only way I have been able to get the project to build is by altering my JAVA_HOME and JRE_HOME environment variables to point to the older versions of Java. Are there any other settings in Eclipse which I can change to build the project without changing my system environment variables? (i.e. Ant builds the project using Java 1.5 but the system variables still point at Java 1.7).
The code for build.xml:
<project name="osd-bits" basedir="." default="deploy">
<!-- Project settings -->
<!-- TODO -->
<!-- This needs to be set individually by each developer to the correct Tomcat path -->
<property name="tomcat.path" value="C:\Tomcat 6\apache-tomcat-6.0.35" />
<property name="project.title" value="OSD BITS"/>
<property name="project.name" value="osd-bits"/>
<property name="project.version" value="2.0"/>
<property name="project.deploypath" value="${tomcat.path}\\webapps"/>
<property name="project.workdir" value="${tomcat.path}\\work\Catalina\localhost"/>
<!-- Test Server Settings -->
<property name="project.deploypathtest" value="T:\Program Files\jakarta-tomcat-5.0.28\webapps"/>
<property name="project.workdirtest" value="T:\Program Files\jakarta-tomcat-5.0.28\work\Catalina\localhost"/>
<!-- Staging Server Settings -->
<property name="project.deploypathstage" value="S:\Program Files\jakarta-tomcat-5.0.28\webapps"/>
<property name="project.workdirstage" value="S:\Program Files\jakarta-tomcat-5.0.28\work\Catalina\localhost"/>
<!-- Production Server Settings -->
<property name="project.deploypathprod" value="P:\Program Files\jakarta-tomcat-5.0.28\webapps"/>
<property name="project.workdirprod" value="P:\Program Files\jakarta-tomcat-5.0.28\work\Catalina\localhost"/>
<!-- Path settings -->
<property name="path.binary" value="${basedir}/../../baja2/bin"/>
<property name="path.webroot" value="${basedir}/webroot"/>
<property name="path.conf" value="${basedir}/conf"/>
<property name="path.src" value="${basedir}/src"/>
<property name="path.build" value="${basedir}/bin"/>
<property name="path.cmdlib" value="${basedir}/cmd/lib"/>
<property name="path.buildlib" value="${tomcat.path}\\lib"/>
<property name="path.webinf" value="${path.webroot}/WEB-INF"/>
<property name="path.lib" value="${path.webinf}/lib"/>
<property name="path.classes" value="${path.webinf}/classes"/>
<property name="path.classpath" value="${path.lib}/baja.jar;
${path.lib}/struts.jar;
${path.buildlib}/fop.jar;
${basedir}/lib/servlet.jar;
${path.buildlib}/velocity-dep-1.4.jar;
${basedir}/lib/jxl.jar;
${path.lib}/display.jar;
${path.buildlib}/commons-validator.jar;
${path.buildlib}/commons-beanutils.jar;
${path.lib}/quartz-1.6.3.jar"/>
<!-- Check timestamp on files -->
<target name="timecheck">
<tstamp/>
</target>
<!-- Remove classes for clean build -->
<target name="clean" description="Prepare for clean build">
<delete dir="${path.build}/com"/>
<delete file="${path.lib}/${project.name}.jar"/>
<delete file="${path.lib}/${project.name}-config.jar"/>
</target>
<!-- jar up config files -->
<target name="config">
<jar jarfile="${path.lib}/${project.name}-config.jar" basedir="${path.conf}"/>
</target>
<!-- Full build of java and config files-->
<target name="build" depends="timecheck,clean,config">
<echo message="ant.java.version: ${ant.java.version}" />
<javac srcdir="${path.src}" destdir="${path.build}" classpath="${path.classpath}" debug="true" target="1.5" includeantruntime="false"/>
<jar jarfile="${path.lib}/${project.name}.jar" basedir="${path.build}"/>
</target>
<!-- Builds everything in src directory and deploys .war to deployment directory -->
<target name="deploy" depends="build" description="Deploys project to deployment directory">
<delete file="${project.deploypath}\${project.name}.war"/>
<delete dir="${project.deploypath}\${project.name}"/>
<delete dir="${project.workdir}\${project.name}"/>
<copy todir="${project.deploypath}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>
<!-- Builds everything in src directory and deploys to test server deployment directory -->
<target name="deploytotest" depends="build" description="Deploys project to test deployment directory">
<delete file="${project.deploypathtest}\${project.name}.war"/>
<delete dir="${project.deploypathtest}\${project.name}"/>
<delete dir="${project.workdirtest}\${project.name}"/>
<copy todir="${project.deploypathtest}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>
<!-- Builds everything in src directory and deploys to staging server deployment directory -->
<target name="deploytostage" depends="build" description="Deploys project to stage deployment directory">
<delete file="${project.deploypathstage}\${project.name}.war"/>
<delete dir="${project.deploypathstage}\${project.name}"/>
<delete dir="${project.workdirstage}\${project.name}"/>
<copy todir="${project.deploypathstage}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>
<!-- Builds everything in src directory and deploys to production server deployment directory -->
<target name="deploytoprod" depends="build" description="Deploys project to production deployment directory">
<delete file="${project.deploypathprod}\${project.name}.war"/>
<delete dir="${project.deploypathprod}\${project.name}"/>
<delete dir="${project.workdirprod}\${project.name}"/>
<copy todir="${project.deploypathprod}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>
<target name="movejsps" description="Moves all JSP files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\jsp" preservelastmodified="false" >
<fileset dir="${path.webroot}/jsp" />
</copy>
</target>
<target name="movejs" description="Moves all JS files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\js" preservelastmodified="false" >
<fileset dir="${path.webroot}/js" />
</copy>
</target>
<target name="movecss" description="Moves all CSS files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\css" preservelastmodified="false" >
<fileset dir="${path.webroot}/css" />
</copy>
</target>
<target name="movetemplates" description="Moves all Report Template files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\templates" preservelastmodified="false" >
<fileset dir="${path.webroot}/templates" />
</copy>
</target>
<target name="copyjar" description="Moves latests baja.jar into demos webinf lib">
<copy file="${path.binary}/baja.jar" todir="${path.lib}" preservelastmodified="true" />
</target>
<!-- Builds the command line distribution -->
<target name="deploycmd" depends="build" description="Deploys project to test deployment directory">
<delete file="${path.cmdlib}\${project.name}.jar"/>
<delete file="${path.cmdlib}\${project.name}-config.jar"/>
<copy file="${path.lib}/${project.name}.jar" todir="${path.cmdlib}" preservelastmodified="true" />
<copy file="${path.lib}/${project.name}-config.jar" todir="${path.cmdlib}" preservelastmodified="true" />
</target>
There might be multiple causes:
you use a library that was compiled for java 1.6
inside the ant script, you can specify the java version you want to compile to. It might be a different version, than the version, that ant itself runs in.
inside the eclipse settings, you can select an ant installation. The ant installation might be compiled for java 1.6 (not very propable). You might add(?)/select another ant installation
if ant does not compile your source but uses the class files, that the eclipse autocompilation already produced, you need to change the project's java compiler version and the projects JRE in the project settings (obviously there ;-)
Go to the library with all the jars. If you can run a bash script, do this:
#!/bin/bash
for jarfile in `ls *.jar`
do
directory=${jarfile%.jar}
if [ -d $directory ]
then
rm -r $directory
fi
mkdir $directory
cd $directory
jar xf ../$jarfile
cd ..
for classfile in `find ./ -name *.class`
do
file $classfile | grep -v "version 46" | grep -v "version 47" | grep -v "version 48" | grep -v "version 49"
done
done
That should spit out any class that is not version 46 (Java 1.2), version 47 (Java 1.3), 48 (Java 1.4) or 49 (Java 1.5). The quartz jar has a class that's 45.3, which is Java 1.1. Maybe that's an issue? If not (Googling suggests JVM 1.5 can run 1.1 classes), then exclude "45.3" as well:
file $classfile | grep -v "version 45.3" | grep -v "version 46" | grep -v "version 47" | grep -v "version 48" | grep -v "version 49"
If you are not using eclipse. Then you can change the ant java property directly on the file as mentioned here. Works for me.
http://pissedoff-techie.blogspot.in/2014/09/ant-picks-up-incorrect-java-version.html

Android Ant Build

I would like to build two versions of my Androidapplication using an Apache ant file. The problem is, that both versions are identical except the advertisement in the lite version. I read about using Configurations with ant to build debug versions.
The following class defines some constants that can be referenced within the application.
public class Config {
// Whether or not to include logging in the app.
public final static boolean LOGGING = true;
}
And here is an example on how to use this constants to determine if logging is enabled or not.
if (Config.LOGGING) {
Log.d(TAG, "[onCreate] Success");
}
Now i can enable and disable logging in my properties file.
# Turn on or off logging.
config.logging=true
That does not work, because before using this config I have to create a second config file and use filterset and copy.
public class Config {
// Whether or not to include logging in the app.
public final static boolean LOGGING = #CONFIG.LOGGING#;
}
That's pretty easy, but how I could use this to build two versions of my application with and without advertisement. And how could I change the package names using ant, so the android market would accept both packages (Full and Lite).
Thank you, for your suggestions, but I still have some problems.
I managed to write some basic targets that cleanup my builds and copy all files needed to build the application in two folders /full and /lite. So I have two directories with the same content. Now I rename all matches of the applications package name in all *.java files and the AndroidManifest file (target prepare).
To really build two different version I would now have to include the code from my first post. But how do I have to do this and how can I build both versions in the release target and write the resulting *.apk files into the build directoy?
Finally ... Would that be all I have to do to build running *.apks that would be accepted by the android market?
<?xml version="1.0" encoding="UTF-8"?>
<project name="my.application" default="help" basedir=".">
<!-- Load the custom property files -->
<property file="build.properties" />
<property file="passwords.properties" />
<!-- Set global properties for this build -->
<property name="my.application.pkg" value="my.application"/>
<property name="my.application.pkg.full" value="my.application.full"/>
<property name="my.application.pkg.lite" value="my.application.lite"/>
<property name="my.application" location="."/>
<property name="my.application.build" location="build"/>
<property name="my.application.src" location="src"/>
<property name="my.application.res" location="res"/>
<property name="my.application.gen" location="gen"/>
<property name="my.application.full" location="full"/>
<property name="my.application.full.src" location="full/src"/>
<property name="my.application.full.res" location="full/res"/>
<property name="my.application.full.gen" location="full/gen"/>
<property name="my.application.full.build" location="full/build"/>
<property name="my.application.lite" location="lite"/>
<property name="my.application.lite.build" location="lite/build"/>
<property name="my.application.lite.src" location="lite/src"/>
<property name="my.application.lite.res" location="lite/res"/>
<property name="my.application.lite.gen" location="lite/gen"/>
<!-- Create and update the local.properties file -->
<loadproperties srcFile="local.properties" />
<!-- Load the ant.properties file -->
<property file="ant.properties" />
<!-- Load the project.properties file -->
<loadproperties srcFile="project.properties" />
<!-- Quick check on sdk.dir. -->
<fail
message="sdk.dir is missing."
unless="sdk.dir" />
<!-- Version-tag: 1 -->
<import file="${sdk.dir}/tools/ant/build.xml" />
<target name="release" depends="report, prepare">
<echo>Building the target!</echo>
</target>
<target name="prepare" depends="cleanup" >
<!-- Copy the Manifest.xml to the full copy -->
<copyfile src="${my.application}/AndroidManifest.xml"
dest="${my.application.full}/AndroidManifest.xml" />
<!-- Copy the source files to the full copy -->
<copy todir="${my.application.full.src}" overwrite="true">
<fileset dir="${my.application.src}" />
</copy>
<!-- Copy the resources to the full copy -->
<copy todir="${my.application.full.res}" overwrite="true">
<fileset dir="${my.application.res}" />
</copy>
<!-- Copy the generated to the full copy -->
<copy todir="${my.application.full.gen}" overwrite="true">
<fileset dir="${my.application.gen}" />
</copy>
<!-- Replace the package name in the full manifest file -->
<replaceregexp file="${my.application.full}/AndroidManifest.xml"
match='package="(.*)"'
replace='package="${my.application.pkg.full}"'
byline="false" />
<!-- Change the package name in all Java files -->
<replaceregexp flags="g" byline="false">
<regexp pattern="${my.application.pkg}" />
<substitution expression="${my.application.pkg.full}" />
<fileset dir="${my.application.full.src}" includes="**/*.java" />
</replaceregexp>
<!-- Copy the Manifest.xml to the lite copy -->
<copyfile src="${my.application}/AndroidManifest.xml"
dest="${my.application.lite}/AndroidManifest.xml" />
<!-- Copy the source files to the lite copy -->
<copy todir="${my.application.lite.src}" overwrite="true">
<fileset dir="${my.application.src}" />
</copy>
<!-- Copy the resources to the lite copy -->
<copy todir="${my.application.lite.res}" overwrite="true">
<fileset dir="${my.application.res}" />
</copy>
<!-- Copy the generated to the lite copy -->
<copy todir="${my.application.lite.gen}" overwrite="true">
<fileset dir="${my.application.gen}" />
</copy>
<!-- Replace the package name in the lite manifest file -->
<replaceregexp file="${my.application.lite}/AndroidManifest.xml"
match='package="(.*)"'
replace='package="${my.application.pkg.lite}"'
byline="false" />
<!-- Change the package name in all Java files -->
<replaceregexp flags="g" byline="false">
<regexp pattern="${my.application.pkg}" />
<substitution expression="${my.application.pkg.lite}" />
<fileset dir="${my.application.lite.src}" includes="**/*.java" />
</replaceregexp>
</target>
<!-- Deletes all directories, not needed anymore after compiling the source files -->
<target name="cleanup">
<!-- Delete the full version build dir -->
<delete dir="${my.application.full}"/>
<!-- Delete the lite version build dir -->
<delete dir="${my.application.lite}"/>
<!-- Delete the *.apk file -->
<delete file="my.application.full.apk"/>
<!-- Delete the *.apk file -->
<delete file="my.application.lite.apk"/>
</target>
</project>
There are a number of ways in which you could achieve what you require.
Here are a couple of ideas that I have used in the past,
1) Have two application 'heads' that pull in a common Android library.
Each head initializes static data that sets up the library to behave as either the lite or the full version of your application.
This has the advantage that you can perform the build from Eclipse projects as well as with Ant.
2) Have two seperate build targets that share common build targets to create two seperate apk files.
In the Ant build script have it build two versions of the APK.
One that is the full version and then the other which builds the lite version.
The difference between the two targets are that they build using slightly different files (either by copying, directing to diferent directories or modifying with scripts).
This can all be done in Ant using targets and properties.
If at the top level of your build you have a release target depending on two other targets.
e.g.
<target name="release"
depends="release-Full, release-Lite">
</target>
<target name="release-Full">
<ant antfile="thisbuild.xml" inheritAll="true" target="full">
<property name="MyCustomProperty" value="Full" />
</ant>
</target>
<target name="release-Lite">
<ant antfile="thisbuild.xml" inheritAll="true" target="lite">
<property name="MyCustomProperty" value="Lite" />
</ant>
</target>
You can then use these targets and properties to modify your build to do whatever you require to build each of the APK files.

Categories