본문 바로가기

PROGRAMING/ECLIPSE

Eclipse의 빌드시 환경 변수 알아내기

힘든 과정이었다.

Eclipse 자바 프로젝트를 새로 생성해서 빌드시키려고 했는데 run configuration을 다시 생성해야했다.

maven build를 이용해서 기존에 작성해 놓은 run configuration을 재 사용해보고 싶었다.


그래서 run configuration의 Base Directory를 이클립스에서 제공하는 variables을 이용하여 공통으로 쓰고 싶었다.



이때 두가지 의문점이 들었다. Eclipse에서 제공해주는 variables는 어떤 값들 일까?

그리고 어떻게 그것을 사용할수 있을까 ? 였다.


처음엔 java단에서 해당 variables들을 찍어주려고 했다.

System.getProperties(),System.getenv()등을 사용하여 해당 variables들을 찍어주려고 했다.

탐구를 해보았지만 문득 eclipse의 variables들은 해당 함수로 찍어 줄수 없을것이라는 생각이 들었다.

왜냐하면 eclipse의 variables들은 컴파일 할때마다 변경되기 컴파일시점에서의 환경과 관련이 있을것 같다는 생각을 하게되었다.

그래서 eclipse에서 console 명령으로 해당 변수들을 알수있지 않을 까 생각하게 되었다. 최신 웹브라우저에 있는 

web console 처럼 말이다.

여러 방법을 찾아보았지만 eclipse 명령을 내릴수 있는 shell 툴을 찾지못했다. spring roo를 설치해보았지만 내가 생각한

그런 기능은 있지 않았다.


두번째 도전은 ant였다. ant를 이용하면 echo 명령으로 설정변수를 찍어줄수 있으니 console 출력창에서 확인 할수 있을 것이란 기대였다. 그러나 실제로 eclipse의 환경변수를 찍어봤을 때 아무값도 나오지 않았다.

ant로는 eclipse의 빌드 환경 변수를 찍어 줄수 없었다.


포기를 하려고 했을 때 인터넷에서 External Toolshttp://anster.egloos.com/v/2178807 ) 에 대한 글을 보게 되었다.


이 포스트는 dos용 bat파일을 이용해 어떤 코드를 만들어내는 일을 하는 것이었다.


eclipse build variables 를 알아내기 위한 방법으로 난 이것을 이용하였다.

print_build_variable.bat라는 파일을 만들고 batch 파일에 인자로 eclipse environment variables를 넘기고 

bat파일에서 그 인자값을 찍는 방법을 이용했다.


print_build_variables.bat의 내용


print_build_variable.zip



@ECHO OFF

echo "===========START"

setlocal enabledelayedexpansion

REM 결과 확인을 하기위해서는 사실 아래 FOR문 3줄만 있으면 된다.

FOR %%A IN (%*) DO (

echo "    =  %%A"

)

set  arrayline[0]=${build_files}

set  arrayline[1]=${build_type}

set  arrayline[2]=${bundle_loc}

set  arrayline[3]=${bundle_state_loc}

set  arrayline[4]=${bundle_version}

set  arrayline[5]=${container_loc}

set  arrayline[6]=${container_name}

set  arrayline[7]=${container_path}

set  arrayline[8]=${eclipse_home}

set  arrayline[9]=${project_loc}

set  arrayline[10]=${project_name}

set  arrayline[11]=${project_path}

set  arrayline[12]=${resource_loc}

set  arrayline[13]=${resource_name}

set  arrayline[14]=${resource_path}

set  arrayline[15]=${selected_resource_loc}

set  arrayline[16]=${selected_resource_name}

set  arrayline[17]=${selected_resource_path}

set  arrayline[18]=${target.arch}

set  arrayline[19]=${target.nl}

set  arrayline[20]=${target.os}

set  arrayline[21]=${target.ws}

set  arrayline[22]=${target_home}

set  arrayline[23]=${unique_id}

set  arrayline[24]=${workspace_loc}


goto :comment1    

REM멀티주석 효과를 주기위해 goto문 사용

REM 따옴표를 해주지 않으면 값이 없을때 공백이 되어 어느 변수가 값이 없는지 확인을 할수 없었다. 

REM 그래서 따옴표가 있는 버전과 없는 버전 두가지를 테스트 해보았다. 아래는 따옴표없는버전

${build_files}

${build_type}

${bundle_loc}

${bundle_state_loc}

${bundle_version}

${container_loc}

${container_name}

${container_path}

${eclipse_home}

${project_loc}

${project_name}

${project_path}

${resource_loc}

${resource_name}

${resource_path}

${selected_resource_loc}

${selected_resource_name}

${selected_resource_path}

${target.arch}

${target.nl}

${target.os}

${target.ws}

${target_home}

${unique_id}

${workspace_loc}

#${project_classpath} 는 값이 너무 길어서 출력 시도시 오류가 난다.

${project_classpath}


REM 따옴표있는 버전 위와 동일

REM ${eclipse_home}의 경우 따옴표를 해주면 에러가 발생하여 따옴표를 하지 않았다.

REM 아래 것을 복사해서 External Tools의 Arguments에 넣는다.

"${build_files}"

"${build_type}"

"${bundle_loc}"

"${bundle_state_loc}"

"${bundle_version}"

"${container_loc}"

"${container_name}"

"${container_path}"

${eclipse_home}

"${project_loc}"

"${project_name}"

"${project_path}"

"${resource_loc}"

"${resource_name}"

"${resource_path}"

"${selected_resource_loc}"

"${selected_resource_name}"

"${selected_resource_path}"

"${target.arch}"

"${target.nl}"

"${target.os}"

"${target.ws}"

"${target_home}"

"${unique_id}"

"${workspace_loc}"


:comment1


::read it using a FOR /L statement

for /l %%n in (0,1,24) do (

echo !arrayline[%%n]!

)

echo "===========END"

External Tools을 이용하여 location 디렉토리에 작성한 print_build_variable.bat 파일을 지정하고

Arguments에는 bat 파일에 있는 따움표로 둘러싸인 것들을 복사해서 붙여넣는다.



이렇게 한다음에 Run을 누르면 아래와 같이 내용이 출력된다.

"===========START"

"    =  """

"    =  none"

"    =  """

"    =  """

"    =  """

"    =  D:\MyFolder\WorkSpace\spring\HelloWeb_SPRING_MEVEN_VERSION"

"    =  HelloWeb_SPRING_MEVEN_VERSION"

"    =  \HelloWeb_SPRING_MEVEN_VERSION"

"    =  C:\eclipse-jee-juno-SR1-win32-x86_64\eclipse\"

"    =  D:\MyFolder\WorkSpace\spring\HelloWeb_SPRING_MEVEN_VERSION"

"    =  HelloWeb_SPRING_MEVEN_VERSION"

"    =  \HelloWeb_SPRING_MEVEN_VERSION"

"    =  D:\MyFolder\WorkSpace\spring\HelloWeb_SPRING_MEVEN_VERSION\print_build_variable.bat"

"    =  print_build_variable.bat"

"    =  \HelloWeb_SPRING_MEVEN_VERSION\print_build_variable.bat"

"    =  D:\MyFolder\WorkSpace\spring\HelloWeb_SPRING_MEVEN_VERSION\print_build_variable.bat"

"    =  print_build_variable.bat"

"    =  \HelloWeb_SPRING_MEVEN_VERSION\print_build_variable.bat"

"    =  x86_64"

"    =  ko_KR"

"    =  win32"

"    =  win32"

"    =  C:\eclipse-jee-juno-SR1-win32-x86_64\eclipse"

"    =  27249242680140"

"    =  D:\MyFolder\WorkSpace\spring"

${build_files}

${build_type}

${bundle_loc}

${bundle_state_loc}

${bundle_version}

${container_loc}

${container_name}

${container_path}

${eclipse_home}

${project_loc}

${project_name}

${project_path}

${resource_loc}

${resource_name}

${resource_path}

${selected_resource_loc}

${selected_resource_name}

${selected_resource_path}

${target.arch}

${target.nl}

${target.os}

${target.ws}

${target_home}

${unique_id}

${workspace_loc}

"===========END"


엑셀을 이용해서 잘 정리하면 아래와 같이 나온다.


${build_files} ""
${build_type} none
${bundle_loc} ""
${bundle_state_loc} ""
${bundle_version} ""
${container_loc} D:\MyFolder\WorkSpace\spring\HelloWeb_SPRING_MEVEN_VERSION
${container_name} HelloWeb_SPRING_MEVEN_VERSION
${container_path} \HelloWeb_SPRING_MEVEN_VERSION
${eclipse_home} C:\eclipse-jee-juno-SR1-win32-x86_64\eclipse\
${project_loc} D:\MyFolder\WorkSpace\spring\HelloWeb_SPRING_MEVEN_VERSION
${project_name} HelloWeb_SPRING_MEVEN_VERSION
${project_path} \HelloWeb_SPRING_MEVEN_VERSION
${resource_loc} D:\MyFolder\WorkSpace\spring\HelloWeb_SPRING_MEVEN_VERSION\test.bat
${resource_name} test.bat
${resource_path} \HelloWeb_SPRING_MEVEN_VERSION\test.bat
${selected_resource_loc} D:\MyFolder\WorkSpace\spring\HelloWeb_SPRING_MEVEN_VERSION\test.bat
${selected_resource_name} test.bat
${selected_resource_path} \HelloWeb_SPRING_MEVEN_VERSION\test.bat
${target.arch} x86_64
${target.nl} ko_KR
${target.os} win32
${target.ws} win32
${target_home} C:\eclipse-jee-juno-SR1-win32-x86_64\eclipse
${unique_id} 25304313435103
${workspace_loc} D:\MyFolder\WorkSpace\spring


빌드시 쓸만한것은 ${project_loc} 뿐인것 같다.

아래와 같이 Base directory를 ${project_loc}로 설정하여 다른 프로젝트도 빌드하였다.