본문 바로가기
IT-개발/JAVA

java - Apache Ant 빌드 사용

by 로데안 2024. 5. 14.

개미가 커졌어요 - 리사이징한 ANT 로고

prologue
이건 왜 다시 정리 한건가요?
> 엉망으로 쓰고 있던걸 고친 게 많았어요, 물론 다시 안 쓸 거라고 생각하지만 사람일은 모르니..
2023.04.24 - [개발/JAVA] - 아주 오래된 소스 Ant build 하면서

 

아니..
Ant를 썼는데 배포마다 결과물이 다르죠? (클레임 먹음)
...
결과물의 class파일이 eclipse에서 빌드한 파일이 들어 갔다구...??

 
 

Ant 다운로드

 
공식 사이트 : https://ant.apache.org/

 

Apache Ant - Welcome

Welcome Apache Ant™ Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications.

ant.apache.org

 

 
** ant release 별 필요한 Java 버전

버전 Java require(최소 버전)
1.9.x releases Java5
1.10.x releases Java8

 
제가 개발하던 환경은 JDK 7을 이용해야 하는 곳이기 때문에 1.9.X 버전을 사용해야 했었습니다.
binary를 받고 압축을 풀어서 사용해야 하는 건 다들 알고 있겠죠? (생략)
 
 

Ant Setting

 
Ant 또한 시스템 환경변수 JAVA_HOME에 영향을 받습니다.
저는 그래서 Ant를 호출하기 전 자바의 환경 변수를 매번 jdk 1.7 경로로 지정해 주는 건 번거로우므로 ant1.7j.bat를 생성..
이미지와 처럼 생성 후에 사용하면 편리합니다
또한 환경변수 PATH에 {ANT_PATH}\bin을 넣어주시면 사용하기 편하겠죠?
 

 

build.xml Setting

 
기존과 변경된 예제 build.xml

<project default="dist" name="Create Runnable Jar for Project agent with libraries in sub-folder">
	<!--this file was created by Eclipse Runnable JAR Export Wizard-->
	<!--ANT 1.7 is required                                        -->
	<!--define folder properties-->
	<property name="dir.workspace" value="." />
	<property name="dir.src" value="${dir.workspace}/src" />
	<property name="dir.lib" value="${dir.workspace}/lib" />
	<property name="dir.build" value="${dir.workspace}/build" />
	<property name="dir.output" value="${dir.workspace}/output" />
	<property name="app.version" value="1.0.1" />

	<target name="clean">
		<delete dir="${dir.build}" />
		<mkdir dir="${dir.build}" />
	</target>

	<target name="compile" depends="clean">
		<javac srcdir="${dir.src}" destdir="${dir.build}" encoding="utf-8" >
			<classpath>
				<fileset dir="${dir.lib}" includes="*.jar" />
			</classpath>
		</javac>
	</target>
	
	<fileset id="path.lib" dir="${dir.lib}" includes="*.jar" ></fileset>
	
	<target name="dist" depends="compile">
		<jar destfile="${dir.output}/agent.jar">
			<manifest>
				<attribute name="Main-Class" value="com.test.agent.Agent" />
				<attribute name="Class-Path" value=".
					lib/common-lang3.jar
                    lib/commons-codec-1.10.jar
					<!-- ... 생략 -->" />
			</manifest>
			<fileset dir="${dir.build}" />
		</jar>

		<delete dir="${dir.output}/config" />
		<copyfile src="${dir.workspace}/resources/agent.conf.sample" dest="${dir.output}/config/agent.conf.sample" />
		<copyfile src="${dir.workspace}/resources/agent.conf.sample.euckr" dest="${dir.output}/config/agent.conf.sample.euckr" />

		<delete dir="${dir.output}/doc" />
		<copydir src="${dir.workspace}/doc" dest="${dir.output}/doc">
			<include name="*.docx" />
		</copydir>

		<copy file="${dir.workspace}/service.bat" tofile="${dir.output}/service.bat" />
		<copy file="${dir.workspace}/service.sh" tofile="${dir.output}/service.sh" />

		<copydir src="${dir.workspace}/output" dest="${dir.workspace}/agent" />
		<zip destfile="${dir.workspace}/agent_${app.version}.zip" basedir="${dir.workspace}" includes="agent/**" />

		<delete dir="${dir.workspace}/agent" />
	</target>
	
</project>

 
이런 식으로 작성한 후에

ant1.7j -f build.xml #-f build.xml은 생략가능

 
하면 clean > build > dist(ribution)를 순차적으로 진행하면서 zip 파일을 생성하도록 수정했었습니다.
다시 하지는 않을 것 같은.. 하지만 하게 될 수 도 있는.. 또 내용을 쓸 것 같지 않은...
Apache Ant는 여기서 마치겠습니다.

반응형