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.
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.
Then, from the next section, I will introduce how to use Ant while actually 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.
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->
<!-Appender for log files->
<!-Logger definition->
It's not directly related, but I'm adding it for log files that rotate on a daily basis.
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;
/**
@author tarosa0001 */ public class Main { / ** Logger * / private static Logger logger = LoggerFactory.getLogger(Main.class);
/**
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
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.
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)->
<!-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->
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".
Now let's run an Ant build. Follow the steps below to build and run the program.
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
■ Check and execute the target I think that the clean target is checked by default, but let's uncheck it this time.
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
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.
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
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