Eclipce with Ant for compiling, packaging, transferring, running

Source: Internet
Author: User
Tags scp file

When built with ant, the build path can only be single-level, such as the default SRC, if it is similar to basepath/jsr253, when running the ant build will error, saying that jsr253 can not be found.
(This article is an example of an eclipse on Windows, as exemplified by ant1.7.1.)

There are several ways to build ant, and here's a less-effort approach (using Ant in Eclipse)
1. Generate Build.xml
Right-click on project name--export--> general--> Ant buildfiles--> next--> Finish,
You can see that there is a build.xml file in the root of the project, the file has an ant icon, this file has been automatically generated according to your engineering structure of the corresponding XML script, but currently only contains the functionality of the compilation.

2. Add the ability to build the jar
Double-click Open Build.xml, add a new <target> tag to the <project></project> tab
<target name= "jar" depends= "Build" >
<delete file= "E:/test.jar"/>
<jar jarfile= "E:/test.jar" >
<fileset dir= "Bin" includes= "**/*.*"/>
Name: Add a name to target and can be named arbitrarily.
Depends: The name of the target to rely on, because the jar package can be generated after compiling, so the build in the example is
The target name of the translation function.
Delete: The value of the file after deletion is the specific path to the files that need to be deleted.
Jar: Generate jar package, Jarfile is used to specify the path of the generated jar package, Fileset specifies the required to build the jar package
Contains the class file, dir Specifies the directory of the class file, and includes specifies the specific file that is contained.

Whether the test function is implemented:
Modify the default property in the <project> tag and change its value to "jar", which is the value of the name attribute in the <target> tag above, and save it. Right-click Build.xml,run as--> Ant Build. Check to see if the console output is the last hint of whether the Test.jar is generated under the path specified in build Seccessfull,jarfile, and if so, indicates success. The following functions can be verified in a similar way.

3. Add the ability to generate LAR packages/perform an application
Double-click Open Build.xml, add a new <target> tag to the <project></project> tab
<target name= "lar" depends= "jar" >
<delete file= "E:/test.lar"/>
<exec executable= "E:/largen" >
<arg value= "E:/test.jar"/>
EXEC: Perform an application. The value of executable is the specific path to the application that you want to start. The path to the jar package that is required to generate the Lar package is passed in Arg.

4. Add the ability to copy files to the remote server with SSH
Double-click Open Build.xml, add a new <target> tag to the <project></project> tab
<target name= "SCP" depends= "lar" description= "Copy to remote server" >
&LT;SCP file= "E:/test.lar" trust= "true"
todir= "[Email Protected]:/home/tester]
password= "xxxxx" >
Description: The description of target is equivalent to the function of annotations.
SCP: The local file is updated synchronously to the remote server; file is the local path of the files; Todir is the address of the server that is transmitted to the remote, including the user name, IP, and the location of the remote server; password is the password to access the remote server. The Trust property must be set to true, which means it can be trusted, and false by default may be wrong.

This feature is cumbersome compared to the first two features and requires some of the following:
1) Download Jsch.jar, put it under the Lib folder of the ant directory, then click window--> preferences--> ant--> runtime--> Classpath in Eclipse, and in the ant Add Jsch.jar to the Home Entries (Default). Jsch.jar there is a version matching problem, so if you choose the wrong version of the download, even if added, when the runtime will also error.
2) (If you are using a virtual machine version of Linux may need to perform this step) Start Linux, click vm--> settings--> hardware--> Network Adapter, on the right of the network Connection select "Bridged", click "OK" to confirm the operation, the Linux network shutdown reconnect, open the terminal, enter the command "Ifconfig", enter, view IP is the IP of the extranet
3) There is also a sshexec in a remote task similar to the SCP. The primary role of this task is to execute the SSH command remotely, so it needs to be installed on Linux as well. Installation steps: Open the terminal on Linux, enter the command "Sodo apt-get install openssh-server" carriage return, the terminal will automatically download the installation. When installed, enter the command "PS–EF | grep sshd "Enter to see if the sshd has been started.

5. Add the ability to run test cases locally
Double-click Open Build.xml, add a new <target> tag to the <project></project> tab
<target name= "Run" depends= "jar" >
<java classname= "test. Testmidlet "failonerror=" true "fork=" yes ">
<classpath refid= "Jsr253.classpath" ></classpath>
Java: running Java programs; classname is the addition of the startup class in the format "package name. Class name";
The FailOnError can control whether the build is terminated when the task fails, and the default is true;
Classpath: The path where the compiled class file is stored.

6. Add the ability to copy files to a remote server with FTP
<target name= "ftp" depends= "lar" >
<ftp server= "" userid= "root" password= "remotedir="/root ">
<fileset dir= "E:/test" ></fileset>
Server: The IP address of the remote machine that needs to be connected.
Remotedir: Access to the FTP remote directory, FTP defaults to its root directory, as indicated above for the transfer to the root directory under remote FTP.
Fileset: The directory where the files are to be uploaded locally. As indicated above, upload all the files under the E-Drive test folder to the remote.

This feature, like SSH, requires adding a jar package to ant, as follows:
1) Download Commons-net-ftp-2.0.jar, put it under the Lib folder in the Ant directory, and then click Window--> preferences--> ant--> runtime--> in eclipse Classpath, add Commons-net-ftp-2.0.jar to Ant Home Entries (Default).

Ant official User's Manual:

    • Jsch-0.1.43-jdk14.jar (182.6 KB)
    • Download number of times: 10
    • Commons-net-ftp-2.0.jar (KB)
    • Download number of times: 34
    • Ant-contrib-1.0b3.jar (219 KB)
    • Download number of times: 9

Eclipce with Ant for compiling, packaging, transferring, running

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: 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.