Personal notes on Java
For other technologies like HTML, CSS, .NET, PHP, etc. check my other blog

Ant

See the Official Ant site.

Apache Ant is a Java-based build tool used to standardize and automate build and run environments for development. So, instead of manually invoking javac, jar, and java commands in a console, you can create an ant script that defines all this tasks and their sequence and then you only need to call "ant" on the shell to execute all this tasks at once. Many IDEs, like Netbeans, already use and create ant scripts to build your projects.

Apache Ant is widely used but there are also some available alternatives like Maven or Rake. Although also widely used, Maven is criticized, by many with experience in both tools, for being hard to manipulate and maintain.

Ant VS Batch files (or other scripts)
A common scheme to automate build and deployment tasks would involve the development of small scripts in platform-specific languages based on the server's operating system. For example, a developer working on an NT machine could create a batch file that performs the compilation tasks and then runs the deployment. However, if the production environment had Unix or Linux, the developer would have to rewrite the script, ensuring that the scripts were in sync. But a Ant script will be cross-platform. [1]

Note: nothing stops you from using both: you can build a batch file that issues ant commands and DOS commands (useful for example when reusing Netbeans generated ant files: see the respective section further down on this article).

A batch file that calls DOS and ant targets to build and deploy a java project:
cls
@ECHO OFF
set batchFolder=%CD%
set FolderBookSearchXML=C:\Programming\ISFINAL\trab1\novo1\BookSearch
set FolderWebService=C:\Programming\ISFINAL\trab2\Trabalho2\Webservice
set FolderWSClient=C:\Programming\ISFINAL\trab2\Trabalho3\WebClient
::==================================== BookSearchXML ===========================
ECHO *
ECHO ******* CRIAR JAR DO BookSearchXML *******
ECHO *

cd %FolderBookSearchXML%
::call ant clean
call ant jar -q
ECHO ******* COPIAR JAR PARA O WEBSERVICE *******
xcopy dist\*.jar %FolderWebService%\lib\ /Y/F
ECHO ----- proximo passo: actualizar webservice -----
pause

::====================================== Webservice ===========================
ECHO *
ECHO ******* GERAR ARTEFACTOS DO WEBSERVICE *******
ECHO *

cd %FolderWebService%
call ant wsgen-BookSearchWS -q

ECHO ******* DEPLOY WEBSERVICE *******
call ant run-deploy -q
ECHO ----- proximo passo: actualizar cliente do webservice -----
pause

::================================ Cliente do webservice ========================
ECHO *
ECHO ******* GERAR ARTEFACTOS DO CLIENTE DO WEBSERVICE *******
ECHO *
cd %FolderWSClient%\src\java
::wsimport -verbose -keep http://localhost:8080/BookSearchWebservice/BookSearchWS?wsdl
wsimport -keep http://localhost:8080/BookSearchWebservice/BookSearchWS?wsdl
del mywspackage\*.class

ECHO ******* DEPLOY DO CLIENTE DO WEBSERVICE *******
cd %FolderWSClient%
::call ant clean
call ant run-deploy -q

::return to starting directory
cd %batchFolder%




Ant and DBMS
A lot of tasks can be achieved with ant. Besides building your projects it can also be used, for example:

  • delete existing sample database tables;
  • recreate the tables, and populate them;
  • create a database;

check the section "Create databases, tables, and populate tables" on that article page for more information and examples.

Ant install/update/configuration

Check this article

Resources:

  • Tutorial: Hello World with Apache Ant: a quick introduction by examples, it stars by showing the tedious work of manually building your projects using only java shell commands (like javac, jar, java, etc.) and then it shows how this tasks can be automated using ant scripts;
  • Install and configure Ant;
  • Official Ant manual, some useful sections are:
     - list of all available ant task;
     - Writing a Simple Buildfile (explains the basic/general concepts);
    NOTE: if you download ant from the official site it already comes with a manual (look for the folder "manual")
  • NetBeans Creating, Importing, and Configuring Java Projects on NetBeans: NetBeans project system is based directly on Ant. All of the project commands, like Clean and Build Project and Debug, call targets in the project's Ant script. You can therefore build and run your project outside the IDE exactly as it is built and run inside the IDE. It is not necessary to know Ant to work with the IDE. You can set all the basic compilation and runtime options in the project's Project Properties dialog box and the IDE automatically updates your project's Ant script. If you are familiar with Ant, you can customize a standard project's Ant script or write your own Ant script for a project.
    Check the "Running the Web Project" section on this article to see how to edit the Ant script created by NetBeans.
    NetBeans notes:
    • You can freely edit your project's standard build.xml script by adding new targets or overriding existing NetBeans-defined targets. The build.xml imports "build-impl.xml" that is placed inside the "nbproject" folder created by NetBeans on your project root folder.  However, you should not edit the build-impl.xml file.
    • You can use NetBeans to build ant scripts with auto-complete by going to: File -> New File -> Other -> Ant Build Script

Ant Concepts:

  • Apache Ant's buildfiles are written in XML;
  • By default Ant uses "build.xml" as the name for a buildfile;
  • Each buildfile contains one project and at least one (default) target.
  • Targets contain task elements: A target is a set of tasks you want to be executed. When starting Ant, you can select which target(s) you want to have executed. When no target is given, the project's default is used. A target can depend on other targets. You might have a target for compiling, for example, and a target for creating a distributable. You can only build a distributable when you have compiled first, so the distribute target depends on the compile target. Ant resolves these dependencies.
  • Tasks: A task is a piece of code that can be executed. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this. A task can have multiple attributes (or arguments, if you prefer). There is a set of built-in tasks, but it is also very easy to write your own. Some examples tasks include creating/deleting directories, compiling your project, build jar/war files etc.

Ant command parameters

  • Check ant syntax and commands
    C:\ ant -help
  • Print all targets of your build.xml file:
    C:\ ant -projecthelp
  • Check version
    C:\ ant -version


Using Netbeans generated ant build files

Netbeans projects use ant internally to build and deploy your projects. Nothing stops you from using their targets from the command line.

Note: Netbeans uses its own ant installation - not your system ant installation - so, if you want to use the Netbeans generated build files from the command line check your ant version (if you get errors running the build files you may need to update ant to a newer version. You can check ant version with: "C:> ant -version"). Check the Ant install and configuration article for information on how to update it.

Netbeans generates ant build files for your projects:
  • YourProjectFolder\nbproject\build-impl.xml;
  • YourProjectFolder\build.xml (this is the file you want to call from the console. Includes the "build-impl.xml" file and so inherits all its targets. This file contains only placeholders in case you want to manually add functionality to the "build-impl.xml");

Inside Netbeans, on the "Files Panel", you can check the existing "build.xml" targets:


If you want you can call this targets from the command line, issuing normal ant commands for the available targets like:
C:\>ant clean

The Netbeans generated ant files includes many targets like:
  • jar: build a jar file
  • clean: undeploy and remove compiled classes;
  • wsgen-yourWebserviceName: if your project has a webservice this target will use the jdk wsgen tool to generate the webservice artifacts;
  • run-deploy: compiles and deploys  your project;
  • other targets - check the build.xml file from inside Netbeans (describe above) to easily find other generated targets;

When working with many different/independent projects, where one includes a jar of the other, when i need to make changes to the included project, I find it easier to just create a simple batch file that uses generated ant files to build the jars and then copy them with normal DOS commands.

This blog article has some tips on this too.

References

Sem comentários:

Enviar um comentário