[For super beginners] Ant super introduction

Introduction

This time, I would like to introduce Ant. My article is basically a memorandum, so please forgive me if it is not the Ant information I was looking for.

Ant is a build tool, but nowadays many high-tech build tools such as Maven and Gradle are coming out, so some people may think "what a legacy ...", but after all Sometimes you can't do anything without using Ant.

There are various cases such as not being able to build an environment to run Maven, not being able to connect to a network, and building a local repository is more time-consuming. What to hide Recently, I myself ran into such a case and ended up recreating the environment built with Maven like Ant.

So, in this article, I will introduce how to make a Jar file that works only with pure Java, not a war file of a Web project, which is a typical usage of Ant.

1. What is Ant?

First of all, what is Ant? The answer is a tool for building Java programs. As you can see in the title of this article, if you are a super beginner, you may think that "Eclipse will do the build for you".

That's right. That's right. Even if you don't bother to use a build tool, you can instantly create a Jar file by exporting it to Eclipse.

However, it is not always possible to have Eclipse and build on it. In such a case, it is necessary to be able to build without depending on an integrated development environment such as Eclipse. Also, if you use a tool, you can let the tool do more than simply build a Java program, so you can reduce some effort and mistakes.

So this time, I would like to let Ant do the following.

  1. Build a Java program and create a Jar file
  2. Copy resources such as dependent libraries and configuration files to the given directory
  3. Combine 1 and 2 and compress to zip format

Then, from the next section, I will introduce how to use Ant while actually building a project.

2. Building a project

First, build the target project to be built with Ant. Again, let's build a traditional Hello World in the world of system development and take it as an example. The development environment, which I am familiar with in my article, uses Eclipse.

2.1. Creating a new Java project

First, create a new Java project in Eclipse. In my environment, I'm using Eclipse 4.4 Luna.

I have only one source file, and this time I want a library with dependencies and a config file, so I will use logback as the logging library. The tree of the newly created project looks like this: The project name is HelloWorldAnt and the execution environment is jdk1.8.

■ Project route ├─src │ └─main │ Main.java ├─resources * Added as src folder │ logback.xml └lib * Added as a normal folder logback-classic-1.2.3.jar logback-core-1.2.3.jar slf4j-api-1.7.25.jar build.xml

I'm adding two folders from the default project settings. The resources folder where logback.xml is located and the lib folder where the libraries used are located. I want to pass the resources folder as a classpath at runtime, so I define it as a src folder, and the lib folder is just a place for libraries, so I add it as a normal folder. The libraries in the lib folder are added to the project build path.

In addition, build.xml that describes Ant build settings is placed directly under the project.

As a bonus, I will also describe the settings of logback.xml.

■logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<!-Appender for console output-> %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n

<!-Appender for log files-> <!-Files rotate daily-> logs/log%d{yyyy-MM-dd}.log 3 <!-File output format-> %d{yyyy-MM-dd'T'HH:mm:ss'Z'} - %m%n

<!-Logger definition->

It's not directly related, but I'm adding it for log files that rotate on a daily basis.

2.2. Implementation of processing

Now that the project has been set, let's implement the actual processing. It's just a Hello World except using locback, so it's not particularly difficult to implement.

■ Implementation contents of main.java package main;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**

logger.info ("Hello World at Ant !!");

logger.info ("End of processing"); } }

2.3. Running Hello World So far, you've built a project to build with Ant. Let's try it out. Please execute it as a Java application on Eclipse.

Naturally, I will show Hello World lol I want to compare the results when I execute the module built with Ant later, so I will describe the contents that I executed and displayed.

■ Console display contents 21: 36: 08.032 [main] INFO main.Main --Processing start 21: 36: 08.035 [main] INFO main.Main --Hello World at Ant !! 21: 36: 08.036 [main] INFO main.Main --End of processing

3. Implementation of Ant build

Now that you have confirmed that the project you built works without errors, let's move on to the procedure for building a JAR file using Ant.

3.1. Definition of build.xml

First, define the build script in build.xml placed in the project root. This time, we will define a total of 5 targets. The contents of the definition will be described later, so first describe all the contents of the definition.

■ Definition contents of build.xml

<!-JAR file name->

<!-ZIP file name->

<!-Build artifact directory->

<!-src directory->

<!-Resource file directory->

<!-Lib directory->

<!-Source file encoding->

	<!--

Classpath settings. Pass the path where the required libraries are placed in the classpath. -->

<!-JAR file creation (for development)-> <!-Cleanup of working directory->

<!-Copy required files other than class files->

<!-Delete old files->

<!-Create a new folder->

<!-Copy resource file->

<!-Copy Dependent Files->

<!-Compile->

<!-Create ZIP file for deployment->

<!-Delete build artifacts->

3.2. Details of definition

As mentioned earlier, this time build.xml defines 5 targets. Only the makeJar target is used to build the program, but this time it's all defined separately.

The build script used by Ant can be defined separately for each task you want to do, like dividing the program into functions. In the makeJar target, the targets that are prerequisites for building the JAR file are called from the separately defined build scripts, and the JAR file is built using them.

There is also a way to define the depends attribute in the target tag in order to call the target that is a prerequisite, but this time I wanted to make the script as flexible as possible, so I called it using the \ <antcall > tag.

The target for zipping the build artifact is another target, "makeZip".

4. Build and run

Now let's run an Ant build. Follow the steps below to build and run the program.

  1. Generate JAR file
  2. Execute the generated JAR file
  3. Archive the build artifacts in a ZIP file

4.1. JAR file generation

First, use the makeJar target to generate a JAR file. Right-click build.xml on Eclipse > Display the dialog with Ant build (2) and check makeJar > Click Execute Execute in the order of.

■ Displaying the edit configuration dialog picture1.jpg

■ Check and execute the target I think that the clean target is checked by default, but let's uncheck it this time. picture2.jpg

Click the Run button to run the build script. If "BUILD SUCCESSFUL" is displayed on the console, the build is complete. After refreshing the project in Eclipse, the generated JAR file will appear.

■ Package Explorer after project refresh I think that the target folder is created and the following materials are placed in the folder. -Class file after build -Copy of configuration files and dependent JAR files -Generated JAR file

picture3.jpg

4.2. Execution of the generated JAR file

Now let's run the generated file. Start a command prompt and change to the target directory where you built the program earlier. Start the JAR file with the following command. The JAR file built this time does not specify an entry point and does not include dependent files, so specify the classpath at runtime.

■ Execution command java -cp "./dependency/*;./resources;HelloWorldAnt.jar" main.Main

Then how about it? You should see the same display as when you ran it on Eclipse earlier. This confirms that the program was built as expected.

4.3. Archive build artifacts in a ZIP file

So far, you've done everything from building with Ant to running the built artifacts. Finally, let's archive the built artifacts in a ZIP file.

Some people may think that it is not necessary, but when deploying the created one, it is still a different effort to unzip the ZIP that you brought and bring it individually. .. This time, it is separated as a completely different target, but you can also use \ <antcall > to archive the ZIP file as one procedure.

The introduction has been lengthened, but now let's run the makeZip target using the same steps as when generating the JAR file.

If you refresh the project after building, you should see HelloWorldZip generated in the target folder.

■ Package Explorer after project refresh picture4.jpg

If you unzip this, you will see that all the files that were placed in the target folder are archived. However, you wouldn't normally deploy class files as artifacts, so delete them before archiving them into a ZIP. This time, everything is archived in ZIP.

One thing to note here is that files archived in ZIP using Ant cannot be decompressed with Lhaplus. There seems to be a reason to investigate, but it can be decompressed by using decompression software other than Lhaplus, so please use that.

Finally This time, I introduced how to generate a JAR file of a Java program using Ant build. How was it?

This article also has a strong memorial meaning to the author, but I hope it will be of some help to those who will use Ant in the future.

Recommended Posts

[For super beginners] Ant super introduction
[For super beginners] DBUnit super introduction
[For super beginners] Maven super introduction
[For super beginners] Mirage SQL super introduction
[For super beginners] Struts2 Super Primer --2018 Edition
[For super super beginners] What is object orientation?
Ractor super introduction
Book introduction: Spring Boot Recommended reference book for beginners!
[For super beginners] How to use autofocus: true
[Introduction to Java] Basics of java arithmetic (for beginners)
Let's use Java New FileIO! (Introduction, for beginners)
Groovy super easy introduction
Scraping for beginners (Ruby)
Introduction to Java for beginners Basic knowledge of Java language ①
How to use GitHub for super beginners (team development)
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
More usable Enumerable for beginners
Java for beginners, data hiding
Java application for beginners: stream
Binary Tree Memorandum for Beginners (Part 1)
[For beginners] Summary of java constructor
[Rails] Introduction of Rubocop by beginners
Rock-paper-scissors game for beginners in Java
Environment construction with Docker for beginners
Java for beginners, expressions and operators 1
[For beginners] Run Selenium in Java
Links for creating Android apps (for beginners)
Java for beginners, expressions and operators 2
[Folio LSP] Roughly Docker (for beginners)
Notes for Android application development beginners
[Super Introduction] About Symbols in Ruby
Object-oriented course for beginners sent by beginners
Starting heroku through heroku CLI (for beginners)
Introduction to Programming for College Students: Introduction
Recommended learning method for programming beginners
[For Java beginners] About exception handling
Classes and instances Java for beginners
(For super beginners) Getter / setter and property to think in D language