Ant Build Java Project

Source: Internet
Author: User
Tags echo message

Ant build Java project

we create the HelloWorld project, which sends the source file. Java to SRC, compiled with bytecode. Class is placed in the bin and the corresponding jar package is placed in the EXE directory.
start by using the command line to build the project:
1. Create src directory
MD src
2. The source file Helloworld.java is stored in the SRC directory, and the package name of Class HelloWorld is Com.tghz.test
Then create the package name path, CD src,md com,cd com,md tghz,cd tghz,md test, and then store Helloworld.java into the current directory
Helloworld.java public   class helloworld{public      static void Main (string[] args) {     System.out.println (" Hello World ");  }   }

3. Create the bin directory and store the compiled bytecode Helloworld.class in the directory
MD Bin
switch to the project directory,
Javac-sourcepath src\com\tghz\test-d bin Src\com\tghz\test\helloworld.java (command format java-sourcepath source file directory-D destination text Directory source files. java)
The corresponding directory can be generated automatically under the Bin directory according to the Helloworld.java package name
4. Build the jar package and store it in the EXE directory.
switch to the bytecode. Class directory, which is the bin directory.
First we create the configuration file for the jar package, which describes the main class to execute, in the format Main-class: the space package name. The main class name enter a newline, and the file is in the same level directory as the package file.
Jar Package configuration file: Mainconfig.txt
Main-class:com.tghz.test.helloworld
   
Execute JAR-CVFM Helloworld.jar (generated jar file name) Mainconfig.txt (Jar package profile) Com.tghz.test (package name)
This generates the jar file.
If you have more than one. class file, that requires us to create the manifest file Mymanifest, using the manifest file, to archive all the files in the bin directory to Classes.jar. That is, jar CVFM Classes.jar mymanifest-c bin.
After successful execution, the jar package is generated and the jar package configuration is visible in the MANIFEST.MF in the Meta-inf folder within the jar.
   
let's use ant to build a Java project
The build script file for Ant is written in the XML language, with the default of Build.xml. The build.xml contains a root node project, which represents a project. Each build file allows only one project node element to be included.
The project node defines one or more target, which represents a different module. And Target has a collection of tasks, each of which is an executable code.
using ant to build a simple Java project consists of three parts:
1.clean Delete previously generated bytecode. class files and jar files so that we do not conflict when generating new files
2. Compile the source file to generate the. class file
3. Package Build jar File
4. Run the Java Project
There are four target modules under project Build.xml of our build file: Clean,compile,jar,run.
Here is a detailed analysis of the Build.xml file:
<?xml version= "1.0" encoding= "UTF-8"? ><project name= "HelloWorld" default= "Run" basedir= "." > <!--property is generally the variable to be used later--<property name= "src.dir" value= "src"/><property name= "Bin.dir" Valu E= "Bin"/><property name= "Jar.dir" value= "EXE"/><property name= "Main-class" value= "HelloWorld"/>< !--"clean" is used to clear the previously generated bytecode and jar files, so that when the new generation is not repeated--><target name= "clean" > <delete dir = "${bin.dir}"/>< Delete dir = "${jar.dir}"/><echo message = "Clean the bin and exe"/></target><!--"compile" to compile the source. java file --><target name= "Compile" depends= "clean" > <mkdir dir = "${bin.dir}"/><javac srcdir= "${src.dir}" Destdir= "${bin.dir}"/><echo message= "Compile done!" /></target><!--"Jar" is used to package the build Jar--><target name= "jar" depends= "compile" > <mkdir dir= "${ Jar.dir} "/><jar destfile =" ${jar.dir}/helloworld.jar "basedir=" ${bin.dir} "> <!--Use manifest node to configure JAR package--&    Gt <manifest>    <attribute name= "Main-class" value= "${main-class}"/></manifest></jar><echo message = "Created Jar file "/></target><!--" Run "to complete: compile, package build jar, run Jar--><target name=" Run "depends=" jar "> < java classname = "Com.tghz.test.HelloWorld" classpath= "${jar.dir}/helloworld.jar"/></target></project >
when the source file in Src is compiled, Com/tghz/test/helloworld.class is automatically generated in the bin directory based on the package name in the code. javac srcdir= "${src.dir}" (source file directory) destdir= "${bin.dir}" (destination file directory)
When you package the build jar, use the manifest node to configure the jar package to indicate the name of the main class. Each jar contains a manifest file that specifies the properties of the jar.
jar destfile = "${jar.dir}/helloworld.jar" (the name and relative path of the generated jar file) basedir= "${bin.dir}" (indicates the current base path)
Create a jar file named DestFile and add all the files in the Basedir directory to it. when running the specified jar file, the full class name path and the jar package name and path are namedjava classname = "Com.tghz.test.HelloWorld" (class name) classpath= "${jar.dir}/helloworld.jar" (Jar package name)
we go from the command line to the directory where the Build.xml file is located, execute ant, and the build.xml file is searched and executed by default. Sometimes it is more convenient to use a makefile with a different name, in which caseyou need to use-buildfile <file> parameters for Ant (-F <file> is its abbreviated form). We can also specify that some of the modules under project will be executed without executing them all. such as Ant compile, so the run is not executed.


Eclipse's support for Ant
Open Source project Eclipse provides a lot of support for Ant, at the heart of Eclipse's Ant editor. we can open the Build.xml file in Ant Editor and execute it directly, then the script in the XML is executed.


ant automatically compiles and packs Android projects

Ant support for an existing Android project

go to the Sdk/tools directory, execute Android Update Project--name anttest (project name)--path F:\workspace2\AntTest1 (project path) so that the Android project will Generate Build.xml and local.properties. the directory of our Android SDK is indicated in local.properties.

Build.xml is the most important script built by Ant. Open will find that in fact there are few lines of useful, that is because the generated Build.xml script refers to the internal Android SDK built script,<import file= "${sdk.dir}/tools/ant/build.xml"/>, the specific directory in the {SDK directory}/tools/ant/build.xml.
This allows our project to support the ant build package.
Enter our project directory, execute ant Debug, generate a beta apk, default to use Debug key to sign, the generated apk is placed in the bin directory.
executing ant release will generate an unsigned apk (your_prject_name-release-unsigned.apk).


Other library projects are referenced in the project:

If you only refer to a third-party jar package in your project, you can simply place the jar in the Libs folder, and the third-party jar will be automatically added to the ant compilation package. However, if our Android project references other library projects, we should initially have one more parameter when executing the android update command: subprojects

The first thing we need to do is make our reference to the library project also support ant compile package, execute Android update lib-project--path (project path), note that the parameter is lib-project at this time.
then go back to the original project, enter the command "Android Update Project--name project name-path project Path--subprojects", this is OK.


third-party jar package in user library
Normally, you can perform a normal build with ant debug directly in the project root directory. The default classpath will include all jar files in the Libs directory. However, if the user LIBRARY is used in the project, or if the external jar file is referenced, there may be problems in the compilation .
because these jar files, such as the user library, are not automatically included in the Classpath, expand the ant's Path variable and add its own jar file to the classpath.
By looking at the build.xml compilation script provided by the SDK, you can see that the CLASSPATH definition used by Javac is as follows:
<path id= "Project.javac.classpath" >    <path refid= "Project.all.jars.path" ></path>    < Path refid= "Tested.project.classpath" ></path></path><javac encoding= "${java.encoding}"        Source= "${java.source}" target= "${java.target}"        debug= "true" extdirs= "" Includeantruntime= "false"        Destdir = "${out.classes.absolute.dir}"        bootclasspathref= "Project.target.class.path"        verbose= "${verbose}"        classpathref= "Project.javac.classpath"        fork= "${need.javac.fork}" >    <src path= "${ Source.absolute.dir} "></src>    <src path=" ${gen.absolute.dir} "></src>    < Compilerarg line= "${java.compilerargs}" ></compilerarg></javac>

where Project.all.jars.path contains all the jar files, we can introduce additional jar files by redefining this variable in the Build.xml of the project directory.
For example, in my project, Ormlite is referenced in this ORM library, in order to be able to use "attach source" in the development of the source, the jar file can not be placed in the Libs directory,because Eclipse does not allow "attach source" to the jar file in the Libs directory. so I put this file in the Libs/ormlite directory, in order to be able to add these two jar files to the classpath, it is necessary to redefine project.all.jars.path this element.
The basic idea is to redefine the target of-pre-compile, in which the value of Project.all.jars.path is redefined.
where is this code written? The custom_rules.xml in the current directory is referenced in the build.xml of each project, so we create a custom_rules.xml under the project root directory, as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><project name= "Custom_rules" default= "release" ><target name= "- Pre-compile "><echo message=" Jarpath=${tostring:project.all.jars.path} "></echo><echo message=" Jarpath=${jar.libs.dir} "></echo>    <property name=" Ormlite.dir "value=" ${jar.libs.dir}/ormlite " > </property>    <path id= "Ormlite.lib" >        <path path= "${tostring:project.all.jars.path}" ></path>        <pathelement location= "${ormlite.dir}/ormlite-android-4.41.jar" ></pathelement >         <pathelement location= "${ormlite.dir}/ormlite-core-4.41.jar" ></pathelement>    </path >    <path id= "Project.all.jars.path" >        <path refid= "Ormlite.lib" ></path>    </path >    <echo message= "Jarpath=${tostring:project.all.jars.path}" ></echo></target></ Project>

signature and channel package
based on Ant Auto-compilation, you can package your existing Android project by adding signature information and automatically packaging the channel pack when your ant packs your app.
Add signature information
in the project root directory to build a ant.properties file, enter the following, where the keystore password and alias password can not be specified (anti-leakage), then in the process of command execution will ask you to enter.
#keystore的路径, you must use a forward slash  key.store= "E:/wp_android_sample/me.key" #keystore的密码  #key. store.password=*****# Alias name  key.alias=me#alias password  


Running the ant release command under the project root will help you generate a signed and aligned APK, the generated apk (your_project_name-release.apk) in the bin directory.


Automatic packaging of channel packs
implementing bulk cycle wrapping requires a function similar to for loops, where there is no related for loop task in the ant core package, that is, the for loop is not supported, but Ant supports third-party expansion packs to support additional features.
so we're going to download the appropriate extension package that supports the for loop. You can use the Open source Ant-contrib package. : http://ant-contrib.sourceforge.net/.
The downloaded jar files are placed into the Ant's Lib directory. Next we can package the channel package, the specific method is:
(1) First add attribute Market_channels (channel list, comma-separated) in Ant.properties file, version (application version name)
#渠道市场列表  market_channels=91,360,wandoujia,baidu  #版本号  version=1.2.1  

(2) Add the following code to the Build.xml of our project:
<!--channel Pack packaging script ant deploy--> <taskdef resource= "Net/sf/antcontrib/antcontrib.properties" > <classpath > <pathelement location= "Lib/ant-contrib-1.0b3.jar"/> </classpath> </taskdef> <t Arget name= "Deploy" > <foreach target= "modify_manifest" list= "${market_channels}" param= "Channel" delimiter= "," > </foreach> </target> <target name= "Modify_manifest" > <replaceregexp flags= "G" by Line= "false" > <!--match is android:value= "* * * * * * * *" android:name= "Umeng_channel"-<regexp Pat Tern= ' android:value= ' (. *) "android:name=" Umeng_channel "'/> <!--match and replace it with android:value=" channel name "Android:name = "Umeng_channel"--<substitution expression= ' android:value= "${channel}" android:name= "UMENG_CHANNEL" '/&G         T <!--regular expressions need to match files of Androidmanifest.xml---<fileset dir= "" includes= "Androidmanifest.xml"/> &lt ;/replaceregexp> <property name= "out.release.file" location= "${out.absolute.dir}/${ant.project.name}_${channel}.apk"/> <!--Pack--<antcall target= "release"/> <!--output channel package to bin/out directory--<copy tofile= "${o ut.absolute.dir}/out/${ant.project.name}v${version}-${channel}.apk "file=" bin/${ant.project.name}-release.apk "/ > </target>
running the ant Deploy command at the root of the project will help you to sign the packages for each channel (in order to automate the process, the KeyStore password in the Ant.properties file can be specified,This does not require a password to be entered manually during execution), in the Out directory of the bin directory.



Ant Build Java Project

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.