1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Java: Ant で war ファイルを作成

Last updated at Posted at 2022-11-23

こちらのページを参考にしました。
Antの動作確認

ソースコード

ツリー

$ tree
.
├── helloworld.xml
└── src
    └── HelloWorld.java
src/HelloWorld.java
import java.util.Date;
import java.text.SimpleDateFormat;

class HelloWorld {
	public static void main(String str[]) {
		System.out.println("Hello, World");
		System.out.println("これはテストです。");
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SZ");
		System.out.println(sdf.format(date));
	}
}
helloworld.xml
<project name="MyProject" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
  </target>

<target name="war" depends="compile">
        <war destfile="HelloWorld.war" webxml="helloworld.xml">
            <classes dir="build"/>
            <lib dir="${dist}/lib"/>
        </war>
    </target>


  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

コンパイル

ant -buildfile helloworld.xml

コンパイル後のツリー

$ tree
.
├── build
│   └── HelloWorld.class
├── dist
│   └── lib
│       └── MyProject-20221123.jar
├── helloworld.xml
└── src
    └── HelloWorld.java

プログラムを実行

$  java -classpath dist/lib/MyProject-20221123.jar HelloWorld
Hello, World
これはテストです。
2022-11-23T15:27:00.556+0900

war の作成

ant -f helloworld.xml war

実行結果

$ ant -f helloworld.xml war
Buildfile: /home/uchida/tmp/nov23/example01/helloworld.xml

init:

compile:
    [javac] /home/uchida/tmp/nov23/example01/helloworld.xml:20: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

war:

BUILD SUCCESSFUL
Total time: 0 seconds

war ファイルのチェック

$ jar tf HelloWorld.war 
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/web.xml
WEB-INF/classes/
WEB-INF/classes/HelloWorld.class
WEB-INF/lib/
WEB-INF/lib/MyProject-20221123.jar

war ファイルを解凍して実行

jar xf HelloWorld.war

フォルダー構造

$ tree
.
├── HelloWorld.war
├── META-INF
│   └── MANIFEST.MF
└── WEB-INF
    ├── classes
    │   └── HelloWorld.class
    ├── lib
    │   └── MyProject-20221123.jar
    └── web.xml

実行結果

$ java -classpath WEB-INF/lib/MyProject-20221123.jar  HelloWorld
Hello, World
これはテストです。
2022-11-23T17:05:25.765+0900

確認したバージョン

$ java -version
openjdk version "18.0.2-ea" 2022-07-19
OpenJDK Runtime Environment (build 18.0.2-ea+9-Ubuntu-2)
OpenJDK 64-Bit Server VM (build 18.0.2-ea+9-Ubuntu-2, mixed mode, sharing)

$ ant -version
Apache Ant(TM) version 1.10.12 compiled on July 11 2022
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?